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
27 #include <net/if_arp.h>
28 #include <linux/if_link.h>
33 #define IFF_LOWER_UP 0x10000
40 struct connman_ipconfig {
43 enum connman_ipconfig_type type;
45 struct connman_ipconfig *origin;
47 const struct connman_ipconfig_ops *ops;
50 enum connman_ipconfig_method method;
51 struct connman_ipaddress *address;
52 struct connman_ipaddress *system;
54 struct connman_ipconfig *ipv6;
57 struct connman_ipdevice {
79 struct connman_ipconfig *config;
81 struct connman_ipconfig_driver *driver;
82 struct connman_ipconfig *driver_config;
85 static GHashTable *ipdevice_hash = NULL;
86 static GList *ipconfig_list = NULL;
88 struct connman_ipaddress *connman_ipaddress_alloc(int family)
90 struct connman_ipaddress *ipaddress;
92 ipaddress = g_try_new0(struct connman_ipaddress, 1);
93 if (ipaddress == NULL)
96 ipaddress->family = family;
97 ipaddress->prefixlen = 0;
98 ipaddress->local = NULL;
99 ipaddress->peer = NULL;
100 ipaddress->broadcast = NULL;
101 ipaddress->gateway = NULL;
106 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
108 if (ipaddress == NULL)
111 g_free(ipaddress->broadcast);
112 g_free(ipaddress->peer);
113 g_free(ipaddress->local);
114 g_free(ipaddress->gateway);
118 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
127 mask = inet_network(netmask);
130 /* a valid netmask must be 2^n - 1 */
131 if ((host & (host + 1)) != 0)
135 for (; mask; mask <<= 1)
141 static gboolean check_ipv6_address(const char *address)
143 unsigned char buf[sizeof(struct in6_addr)];
146 err = inet_pton(AF_INET6, address, buf);
153 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
154 const char *address, const char *gateway,
155 unsigned char prefix_length)
157 if (ipaddress == NULL)
160 if (check_ipv6_address(address) == FALSE)
163 if (check_ipv6_address(gateway) == FALSE)
166 DBG("prefix_len %d address %s gateway %s",
167 prefix_length, address, gateway);
169 ipaddress->prefixlen = prefix_length;
171 g_free(ipaddress->local);
172 ipaddress->local = g_strdup(address);
174 g_free(ipaddress->gateway);
175 ipaddress->gateway = g_strdup(gateway);
180 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
181 const char *address, const char *netmask, const char *gateway)
183 if (ipaddress == NULL)
186 ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
188 g_free(ipaddress->local);
189 ipaddress->local = g_strdup(address);
191 g_free(ipaddress->gateway);
192 ipaddress->gateway = g_strdup(gateway);
195 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
197 if (ipaddress == NULL)
200 ipaddress->prefixlen = 0;
202 g_free(ipaddress->local);
203 ipaddress->local = NULL;
205 g_free(ipaddress->peer);
206 ipaddress->peer = NULL;
208 g_free(ipaddress->broadcast);
209 ipaddress->broadcast = NULL;
211 g_free(ipaddress->gateway);
212 ipaddress->gateway = NULL;
215 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
216 struct connman_ipaddress *source)
218 if (ipaddress == NULL || source == NULL)
221 ipaddress->family = source->family;
222 ipaddress->prefixlen = source->prefixlen;
224 g_free(ipaddress->local);
225 ipaddress->local = g_strdup(source->local);
227 g_free(ipaddress->peer);
228 ipaddress->peer = g_strdup(source->peer);
230 g_free(ipaddress->broadcast);
231 ipaddress->broadcast = g_strdup(source->broadcast);
233 g_free(ipaddress->gateway);
234 ipaddress->gateway = g_strdup(source->gateway);
237 static void free_address_list(struct connman_ipdevice *ipdevice)
241 for (list = ipdevice->address_list; list; list = list->next) {
242 struct connman_ipaddress *ipaddress = list->data;
244 connman_ipaddress_free(ipaddress);
248 g_slist_free(ipdevice->address_list);
249 ipdevice->address_list = NULL;
252 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
253 unsigned char prefixlen, const char *local)
257 for (list = ipdevice->address_list; list; list = list->next) {
258 struct connman_ipaddress *ipaddress = list->data;
260 if (g_strcmp0(ipaddress->local, local) == 0 &&
261 ipaddress->prefixlen == prefixlen)
268 static const char *type2str(unsigned short type)
273 case ARPHRD_LOOPBACK:
286 static const char *scope2str(unsigned char scope)
298 static void free_ipdevice(gpointer data)
300 struct connman_ipdevice *ipdevice = data;
302 connman_info("%s {remove} index %d", ipdevice->ifname,
305 if (ipdevice->config != NULL)
306 connman_ipconfig_unref(ipdevice->config);
308 free_address_list(ipdevice);
309 g_free(ipdevice->ipv4_gateway);
310 g_free(ipdevice->ipv6_gateway);
311 g_free(ipdevice->pac);
313 g_free(ipdevice->address);
314 g_free(ipdevice->ifname);
318 static GSList *driver_list = NULL;
320 static gint compare_priority(gconstpointer a, gconstpointer b)
322 const struct connman_ipconfig_driver *driver1 = a;
323 const struct connman_ipconfig_driver *driver2 = b;
325 return driver2->priority - driver1->priority;
329 * connman_ipconfig_driver_register:
330 * @driver: IP configuration driver
332 * Register a new IP configuration driver
334 * Returns: %0 on success
336 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
338 DBG("driver %p name %s", driver, driver->name);
340 driver_list = g_slist_insert_sorted(driver_list, driver,
347 * connman_ipconfig_driver_unregister:
348 * @driver: IP configuration driver
350 * Remove a previously registered IP configuration driver.
352 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
354 DBG("driver %p name %s", driver, driver->name);
356 driver_list = g_slist_remove(driver_list, driver);
359 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
363 DBG("ipconfig %p", ipdevice->config);
365 if (ipdevice->config == NULL)
368 switch (ipdevice->config->method) {
369 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
370 case CONNMAN_IPCONFIG_METHOD_OFF:
371 case CONNMAN_IPCONFIG_METHOD_FIXED:
372 case CONNMAN_IPCONFIG_METHOD_MANUAL:
374 case CONNMAN_IPCONFIG_METHOD_DHCP:
378 if (ipdevice->driver != NULL)
381 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
382 if (ipdevice->driver_config == NULL)
385 for (list = driver_list; list; list = list->next) {
386 struct connman_ipconfig_driver *driver = list->data;
388 if (driver->request(ipdevice->driver_config) == 0) {
389 ipdevice->driver = driver;
394 if (ipdevice->driver == NULL) {
395 connman_ipconfig_unref(ipdevice->driver_config);
396 ipdevice->driver_config = NULL;
400 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
402 DBG("ipconfig %p", ipdevice->config);
404 if (ipdevice->config == NULL)
407 if (ipdevice->driver == NULL)
410 ipdevice->driver->release(ipdevice->driver_config);
412 ipdevice->driver = NULL;
414 connman_ipconfig_unref(ipdevice->driver_config);
415 ipdevice->driver_config = NULL;
417 connman_inet_clear_address(ipdevice->index, ipdevice->config->address);
418 connman_inet_clear_ipv6_address(ipdevice->index,
419 ipdevice->driver_config->address->local,
420 ipdevice->driver_config->address->prefixlen);
423 static void update_stats(struct connman_ipdevice *ipdevice,
424 struct rtnl_link_stats *stats)
426 if (stats->rx_packets == 0 && stats->tx_packets == 0)
429 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
430 stats->rx_packets, stats->rx_bytes);
431 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
432 stats->tx_packets, stats->tx_bytes);
434 if (ipdevice->config == NULL)
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(ipdevice->config,
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)
473 ipdevice = g_try_new0(struct connman_ipdevice, 1);
474 if (ipdevice == NULL)
477 ipdevice->index = index;
478 ipdevice->ifname = connman_inet_ifname(index);
479 ipdevice->type = type;
481 ipdevice->address = g_strdup(address);
483 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
485 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
486 index, type, type2str(type));
491 update_stats(ipdevice, stats);
493 if (flags == ipdevice->flags)
496 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
503 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
504 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
505 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
506 (IFF_RUNNING | IFF_LOWER_UP))
508 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
512 ipdevice->flags = flags;
514 str = g_string_new(NULL);
519 g_string_append(str, "UP");
521 g_string_append(str, "DOWN");
523 if (flags & IFF_RUNNING)
524 g_string_append(str, ",RUNNING");
526 if (flags & IFF_LOWER_UP)
527 g_string_append(str, ",LOWER_UP");
529 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
532 g_string_free(str, TRUE);
534 for (list = g_list_first(ipconfig_list); list;
535 list = g_list_next(list)) {
536 struct connman_ipconfig *ipconfig = list->data;
538 if (index != ipconfig->index)
541 if (ipconfig->ops == NULL)
544 if (up == TRUE && ipconfig->ops->up)
545 ipconfig->ops->up(ipconfig);
546 if (lower_up == TRUE && ipconfig->ops->lower_up)
547 ipconfig->ops->lower_up(ipconfig);
549 if (lower_down == TRUE && ipconfig->ops->lower_down)
550 ipconfig->ops->lower_down(ipconfig);
551 if (down == TRUE && ipconfig->ops->down)
552 ipconfig->ops->down(ipconfig);
556 __connman_ipconfig_lower_up(ipdevice);
558 __connman_ipconfig_lower_down(ipdevice);
561 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
563 struct connman_ipdevice *ipdevice;
566 DBG("index %d", index);
568 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
569 if (ipdevice == NULL)
572 update_stats(ipdevice, stats);
574 for (list = g_list_first(ipconfig_list); list;
575 list = g_list_next(list)) {
576 struct connman_ipconfig *ipconfig = list->data;
578 if (index != ipconfig->index)
581 ipconfig->index = -1;
583 if (ipconfig->ops == NULL)
586 if (ipconfig->ops->lower_down)
587 ipconfig->ops->lower_down(ipconfig);
588 if (ipconfig->ops->down)
589 ipconfig->ops->down(ipconfig);
592 __connman_ipconfig_lower_down(ipdevice);
594 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
597 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
599 const struct connman_ipaddress *addr1 = a;
600 const struct connman_ipaddress *addr2 = b;
602 if (addr1->prefixlen != addr2->prefixlen)
603 return addr2->prefixlen - addr1->prefixlen;
605 return g_strcmp0(addr1->local, addr2->local);
608 void __connman_ipconfig_newaddr(int index, int family, const char *label,
609 unsigned char prefixlen, const char *address)
611 struct connman_ipdevice *ipdevice;
612 struct connman_ipaddress *ipaddress;
615 DBG("index %d", index);
617 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
618 if (ipdevice == NULL)
621 ipaddress = connman_ipaddress_alloc(family);
622 if (ipaddress == NULL)
625 ipaddress->prefixlen = prefixlen;
626 ipaddress->local = g_strdup(address);
628 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
629 check_duplicate_address)) {
630 connman_ipaddress_free(ipaddress);
634 ipdevice->address_list = g_slist_append(ipdevice->address_list,
637 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
638 address, prefixlen, label);
640 if (ipdevice->config != NULL) {
641 if (family == AF_INET6 && ipdevice->config->ipv6 != NULL)
642 connman_ipaddress_copy(ipdevice->config->ipv6->system,
645 connman_ipaddress_copy(ipdevice->config->system,
649 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
652 if (g_slist_length(ipdevice->address_list) > 1)
655 for (list = g_list_first(ipconfig_list); list;
656 list = g_list_next(list)) {
657 struct connman_ipconfig *ipconfig = list->data;
659 if (index != ipconfig->index)
662 if (ipconfig->ops == NULL)
665 if (ipconfig->ops->ip_bound)
666 ipconfig->ops->ip_bound(ipconfig);
670 void __connman_ipconfig_deladdr(int index, int family, const char *label,
671 unsigned char prefixlen, const char *address)
673 struct connman_ipdevice *ipdevice;
674 struct connman_ipaddress *ipaddress;
677 DBG("index %d", index);
679 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
680 if (ipdevice == NULL)
683 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
684 if (ipaddress == NULL)
687 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
690 connman_ipaddress_free(ipaddress);
692 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
693 address, prefixlen, label);
695 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
698 if (g_slist_length(ipdevice->address_list) > 0)
701 for (list = g_list_first(ipconfig_list); list;
702 list = g_list_next(list)) {
703 struct connman_ipconfig *ipconfig = list->data;
705 if (index != ipconfig->index)
708 if (ipconfig->ops == NULL)
711 if (ipconfig->ops->ip_release)
712 ipconfig->ops->ip_release(ipconfig);
716 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
717 const char *dst, const char *gateway)
719 struct connman_ipdevice *ipdevice;
721 DBG("index %d", index);
723 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
724 if (ipdevice == NULL)
727 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
731 if (family == AF_INET6) {
732 g_free(ipdevice->ipv6_gateway);
733 ipdevice->ipv6_gateway = g_strdup(gateway);
735 g_free(ipdevice->ipv4_gateway);
736 ipdevice->ipv4_gateway = g_strdup(gateway);
739 if (ipdevice->config != NULL &&
740 ipdevice->config->system != NULL) {
741 g_free(ipdevice->config->system->gateway);
742 ipdevice->config->system->gateway = g_strdup(gateway);
745 for (list = ipdevice->address_list; list; list = list->next) {
746 struct connman_ipaddress *ipaddress = list->data;
748 g_free(ipaddress->gateway);
749 ipaddress->gateway = g_strdup(gateway);
752 for (config_list = g_list_first(ipconfig_list); config_list;
753 config_list = g_list_next(config_list)) {
754 struct connman_ipconfig *ipconfig = config_list->data;
756 if (index != ipconfig->index)
759 if (ipconfig->ops == NULL)
762 if (ipconfig->ops->ip_bound)
763 ipconfig->ops->ip_bound(ipconfig);
767 connman_info("%s {add} route %s gw %s scope %u <%s>",
768 ipdevice->ifname, dst, gateway,
769 scope, scope2str(scope));
772 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
773 const char *dst, const char *gateway)
775 struct connman_ipdevice *ipdevice;
777 DBG("index %d", index);
779 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
780 if (ipdevice == NULL)
783 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
787 if (family == AF_INET6) {
788 g_free(ipdevice->ipv6_gateway);
789 ipdevice->ipv6_gateway = NULL;
791 g_free(ipdevice->ipv4_gateway);
792 ipdevice->ipv4_gateway = NULL;
795 if (ipdevice->config != NULL &&
796 ipdevice->config->system != NULL) {
797 g_free(ipdevice->config->system->gateway);
798 ipdevice->config->system->gateway = NULL;
801 for (list = ipdevice->address_list; list; list = list->next) {
802 struct connman_ipaddress *ipaddress = list->data;
804 g_free(ipaddress->gateway);
805 ipaddress->gateway = NULL;
808 for (config_list = g_list_first(ipconfig_list); config_list;
809 config_list = g_list_next(config_list)) {
810 struct connman_ipconfig *ipconfig = config_list->data;
812 if (index != ipconfig->index)
815 if (ipconfig->ops == NULL)
818 if (ipconfig->ops->ip_release)
819 ipconfig->ops->ip_release(ipconfig);
823 connman_info("%s {del} route %s gw %s scope %u <%s>",
824 ipdevice->ifname, dst, gateway,
825 scope, scope2str(scope));
828 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
833 keys = g_hash_table_get_keys(ipdevice_hash);
837 for (list = g_list_first(keys); list; list = g_list_next(list)) {
838 int index = GPOINTER_TO_INT(list->data);
840 function(index, user_data);
846 unsigned short __connman_ipconfig_get_type(int index)
848 struct connman_ipdevice *ipdevice;
850 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
851 if (ipdevice == NULL)
854 return ipdevice->type;
857 unsigned int __connman_ipconfig_get_flags(int index)
859 struct connman_ipdevice *ipdevice;
861 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
862 if (ipdevice == NULL)
865 return ipdevice->flags;
868 const char *__connman_ipconfig_get_gateway(int index)
870 struct connman_ipdevice *ipdevice;
872 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
873 if (ipdevice == NULL)
876 if (ipdevice->ipv4_gateway != NULL)
877 return ipdevice->ipv4_gateway;
879 if (ipdevice->config != NULL &&
880 ipdevice->config->address != NULL)
881 return ipdevice->config->address->gateway;
886 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
888 ipconfig->index = index;
890 if (ipconfig->ipv6 != NULL)
891 ipconfig->ipv6->index = index;
894 static struct connman_ipconfig *create_ipv6config(int index)
896 struct connman_ipconfig *ipv6config;
898 DBG("index %d", index);
900 ipv6config = g_try_new0(struct connman_ipconfig, 1);
901 if (ipv6config == NULL)
904 ipv6config->index = index;
905 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
906 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
908 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
909 if (ipv6config->address == NULL) {
914 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
916 ipv6config->ipv6 = NULL;
918 DBG("ipconfig %p", ipv6config);
924 * connman_ipconfig_create:
926 * Allocate a new ipconfig structure.
928 * Returns: a newly-allocated #connman_ipconfig structure
930 struct connman_ipconfig *connman_ipconfig_create(int index)
932 struct connman_ipconfig *ipconfig;
934 DBG("index %d", index);
936 ipconfig = g_try_new0(struct connman_ipconfig, 1);
937 if (ipconfig == NULL)
940 ipconfig->refcount = 1;
942 ipconfig->index = index;
943 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
945 ipconfig->address = connman_ipaddress_alloc(AF_INET);
946 if (ipconfig->address == NULL) {
951 ipconfig->system = connman_ipaddress_alloc(AF_INET);
953 ipconfig->ipv6 = create_ipv6config(index);
955 DBG("ipconfig %p", ipconfig);
961 * connman_ipconfig_clone:
963 * Clone an ipconfig structure and create new reference.
965 * Returns: a newly-allocated #connman_ipconfig structure
967 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
969 struct connman_ipconfig *ipconfig_clone;
971 DBG("ipconfig %p", ipconfig);
973 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
974 if (ipconfig_clone == NULL)
977 ipconfig_clone->refcount = 1;
979 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
981 ipconfig_clone->index = -1;
983 return ipconfig_clone;
987 * connman_ipconfig_ref:
988 * @ipconfig: ipconfig structure
990 * Increase reference counter of ipconfig
992 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
994 g_atomic_int_inc(&ipconfig->refcount);
999 static void free_ipv6config(struct connman_ipconfig *ipconfig)
1001 if (ipconfig == NULL)
1004 connman_ipconfig_set_ops(ipconfig, NULL);
1005 connman_ipaddress_free(ipconfig->system);
1006 connman_ipaddress_free(ipconfig->address);
1007 g_free(ipconfig->ipv6);
1011 * connman_ipconfig_unref:
1012 * @ipconfig: ipconfig structure
1014 * Decrease reference counter of ipconfig
1016 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1019 g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1020 __connman_ipconfig_disable(ipconfig);
1022 connman_ipconfig_set_ops(ipconfig, NULL);
1024 if (ipconfig->origin != NULL) {
1025 connman_ipconfig_unref(ipconfig->origin);
1026 ipconfig->origin = NULL;
1029 connman_ipaddress_free(ipconfig->system);
1030 connman_ipaddress_free(ipconfig->address);
1031 free_ipv6config(ipconfig->ipv6);
1037 * connman_ipconfig_get_data:
1038 * @ipconfig: ipconfig structure
1040 * Get private data pointer
1042 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1044 return ipconfig->ops_data;
1048 * connman_ipconfig_set_data:
1049 * @ipconfig: ipconfig structure
1050 * @data: data pointer
1052 * Set private data pointer
1054 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1056 ipconfig->ops_data = data;
1060 * connman_ipconfig_get_index:
1061 * @ipconfig: ipconfig structure
1063 * Get interface index
1065 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1067 if (ipconfig == NULL)
1070 if (ipconfig->origin != NULL)
1071 return ipconfig->origin->index;
1073 return ipconfig->index;
1077 * connman_ipconfig_get_ifname:
1078 * @ipconfig: ipconfig structure
1080 * Get interface name
1082 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1084 struct connman_ipdevice *ipdevice;
1086 if (ipconfig == NULL)
1089 if (ipconfig->index < 0)
1092 ipdevice = g_hash_table_lookup(ipdevice_hash,
1093 GINT_TO_POINTER(ipconfig->index));
1094 if (ipdevice == NULL)
1097 return ipdevice->ifname;
1101 * connman_ipconfig_set_ops:
1102 * @ipconfig: ipconfig structure
1103 * @ops: operation callbacks
1105 * Set the operation callbacks
1107 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1108 const struct connman_ipconfig_ops *ops)
1110 ipconfig->ops = ops;
1113 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1114 struct connman_ipconfig *ipconfig)
1116 if (ipconfig == NULL)
1119 return ipconfig->ipv6;
1123 * connman_ipconfig_set_method:
1124 * @ipconfig: ipconfig structure
1125 * @method: configuration method
1127 * Set the configuration method
1129 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1130 enum connman_ipconfig_method method)
1132 ipconfig->method = method;
1137 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1139 if (ipconfig == NULL)
1140 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1142 return ipconfig->method;
1146 * connman_ipconfig_bind:
1147 * @ipconfig: ipconfig structure
1148 * @ipaddress: ipaddress structure
1150 * Bind IP address details to configuration
1152 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1153 struct connman_ipaddress *ipaddress)
1155 struct connman_ipconfig *origin;
1157 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1159 connman_ipaddress_copy(origin->address, ipaddress);
1161 connman_inet_set_address(origin->index, origin->address);
1164 void __connman_ipconfig_set_element_ipv6_gateway(
1165 struct connman_ipconfig *ipconfig,
1166 struct connman_element *element)
1168 element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1172 * FIXME: The element soulution should be removed in the future
1173 * Set IPv4 and IPv6 gateway
1175 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1176 struct connman_element *parent)
1178 struct connman_element *connection;
1180 connection = connman_element_create(NULL);
1182 DBG("ipconfig %p", ipconfig);
1184 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1185 connection->index = ipconfig->index;
1186 connection->ipv4.gateway = ipconfig->address->gateway;
1187 connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1189 if (connman_element_register(connection, parent) < 0)
1190 connman_element_unref(connection);
1195 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1199 switch (ipconfig->method) {
1200 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1201 case CONNMAN_IPCONFIG_METHOD_OFF:
1202 case CONNMAN_IPCONFIG_METHOD_FIXED:
1203 case CONNMAN_IPCONFIG_METHOD_DHCP:
1205 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1206 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1207 return connman_inet_set_address(ipconfig->index,
1209 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1210 return connman_inet_set_ipv6_address(
1211 ipconfig->index, ipconfig->address);
1217 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1221 if (ipconfig == NULL)
1224 DBG("method %d", ipconfig->method);
1226 switch (ipconfig->method) {
1227 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1228 case CONNMAN_IPCONFIG_METHOD_OFF:
1229 case CONNMAN_IPCONFIG_METHOD_FIXED:
1230 case CONNMAN_IPCONFIG_METHOD_DHCP:
1232 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1233 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1234 return connman_inet_clear_address(ipconfig->index,
1236 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1237 return connman_inet_clear_ipv6_address(
1239 ipconfig->address->local,
1240 ipconfig->address->prefixlen);
1246 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1249 struct connman_ipdevice *ipdevice;
1251 DBG("ipconfig %p", ipconfig);
1253 if (ipconfig == NULL || ipconfig->index < 0)
1256 ipdevice = g_hash_table_lookup(ipdevice_hash,
1257 GINT_TO_POINTER(ipconfig->index));
1258 if (ipdevice == NULL)
1261 g_free(ipdevice->pac);
1262 ipdevice->pac = g_strdup(url);
1267 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1269 struct connman_ipdevice *ipdevice;
1271 DBG("ipconfig %p", ipconfig);
1273 if (ipconfig == NULL || ipconfig->index < 0)
1276 ipdevice = g_hash_table_lookup(ipdevice_hash,
1277 GINT_TO_POINTER(ipconfig->index));
1278 if (ipdevice == NULL)
1281 return ipdevice->pac;
1284 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1286 struct connman_ipdevice *ipdevice;
1287 gboolean up = FALSE, down = FALSE;
1288 gboolean lower_up = FALSE, lower_down = FALSE;
1290 DBG("ipconfig %p", ipconfig);
1292 if (ipconfig == NULL || ipconfig->index < 0)
1295 ipdevice = g_hash_table_lookup(ipdevice_hash,
1296 GINT_TO_POINTER(ipconfig->index));
1297 if (ipdevice == NULL)
1300 if (ipdevice->config == ipconfig)
1303 if (ipdevice->config != NULL) {
1304 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1306 connman_ipaddress_clear(ipdevice->config->system);
1308 connman_ipconfig_unref(ipdevice->config);
1311 ipdevice->config = connman_ipconfig_ref(ipconfig);
1313 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1315 if (ipdevice->flags & IFF_UP)
1320 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1321 (IFF_RUNNING | IFF_LOWER_UP))
1323 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1326 if (up == TRUE && ipconfig->ops->up)
1327 ipconfig->ops->up(ipconfig);
1328 if (lower_up == TRUE && ipconfig->ops->lower_up)
1329 ipconfig->ops->lower_up(ipconfig);
1331 if (lower_down == TRUE && ipconfig->ops->lower_down)
1332 ipconfig->ops->lower_down(ipconfig);
1333 if (down == TRUE && ipconfig->ops->down)
1334 ipconfig->ops->down(ipconfig);
1339 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1341 struct connman_ipdevice *ipdevice;
1343 DBG("ipconfig %p", ipconfig);
1345 if (ipconfig == NULL || ipconfig->index < 0)
1348 ipdevice = g_hash_table_lookup(ipdevice_hash,
1349 GINT_TO_POINTER(ipconfig->index));
1350 if (ipdevice == NULL)
1353 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1356 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1358 connman_ipaddress_clear(ipdevice->config->system);
1359 connman_ipaddress_clear(ipdevice->config->ipv6->system);
1361 connman_ipconfig_unref(ipdevice->config);
1362 ipdevice->config = NULL;
1367 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1370 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1372 case CONNMAN_IPCONFIG_METHOD_OFF:
1374 case CONNMAN_IPCONFIG_METHOD_FIXED:
1376 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1378 case CONNMAN_IPCONFIG_METHOD_DHCP:
1385 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1387 if (g_strcmp0(method, "off") == 0)
1388 return CONNMAN_IPCONFIG_METHOD_OFF;
1389 else if (g_strcmp0(method, "fixed") == 0)
1390 return CONNMAN_IPCONFIG_METHOD_FIXED;
1391 else if (g_strcmp0(method, "manual") == 0)
1392 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1393 else if (g_strcmp0(method, "dhcp") == 0)
1394 return CONNMAN_IPCONFIG_METHOD_DHCP;
1396 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1399 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1400 DBusMessageIter *iter)
1406 str = __connman_ipconfig_method2string(ipconfig->method);
1410 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1412 if (ipconfig->system == NULL)
1415 if (ipconfig->system->local != NULL) {
1417 struct in_addr netmask;
1420 connman_dbus_dict_append_basic(iter, "Address",
1421 DBUS_TYPE_STRING, &ipconfig->system->local);
1423 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1424 netmask.s_addr = htonl(addr);
1425 mask = inet_ntoa(netmask);
1426 connman_dbus_dict_append_basic(iter, "Netmask",
1427 DBUS_TYPE_STRING, &mask);
1430 if (ipconfig->system->gateway != NULL)
1431 connman_dbus_dict_append_basic(iter, "Gateway",
1432 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1435 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1436 DBusMessageIter *iter)
1442 str = __connman_ipconfig_method2string(ipconfig->method);
1446 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1448 if (ipconfig->system == NULL)
1451 if (ipconfig->system->local != NULL) {
1452 connman_dbus_dict_append_basic(iter, "Address",
1453 DBUS_TYPE_STRING, &ipconfig->system->local);
1454 connman_dbus_dict_append_basic(iter, "PrefixLength",
1456 &ipconfig->system->prefixlen);
1459 if (ipconfig->system->gateway != NULL)
1460 connman_dbus_dict_append_basic(iter, "Gateway",
1461 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1464 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1465 DBusMessageIter *iter)
1471 str = __connman_ipconfig_method2string(ipconfig->method);
1475 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1477 switch (ipconfig->method) {
1478 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1479 case CONNMAN_IPCONFIG_METHOD_OFF:
1480 case CONNMAN_IPCONFIG_METHOD_DHCP:
1482 case CONNMAN_IPCONFIG_METHOD_FIXED:
1483 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1487 if (ipconfig->address == NULL)
1490 if (ipconfig->address->local != NULL) {
1491 connman_dbus_dict_append_basic(iter, "Address",
1492 DBUS_TYPE_STRING, &ipconfig->address->local);
1493 connman_dbus_dict_append_basic(iter, "PrefixLength",
1495 &ipconfig->address->prefixlen);
1498 if (ipconfig->address->gateway != NULL)
1499 connman_dbus_dict_append_basic(iter, "Gateway",
1500 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1503 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1504 DBusMessageIter *iter)
1510 str = __connman_ipconfig_method2string(ipconfig->method);
1514 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1516 switch (ipconfig->method) {
1517 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1518 case CONNMAN_IPCONFIG_METHOD_OFF:
1519 case CONNMAN_IPCONFIG_METHOD_FIXED:
1520 case CONNMAN_IPCONFIG_METHOD_DHCP:
1522 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1526 if (ipconfig->address == NULL)
1529 if (ipconfig->address->local != NULL) {
1531 struct in_addr netmask;
1534 connman_dbus_dict_append_basic(iter, "Address",
1535 DBUS_TYPE_STRING, &ipconfig->address->local);
1537 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1538 netmask.s_addr = htonl(addr);
1539 mask = inet_ntoa(netmask);
1540 connman_dbus_dict_append_basic(iter, "Netmask",
1541 DBUS_TYPE_STRING, &mask);
1544 if (ipconfig->address->gateway != NULL)
1545 connman_dbus_dict_append_basic(iter, "Gateway",
1546 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1549 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1550 enum connman_ipconfig_type type, DBusMessageIter *array)
1552 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1553 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1554 *prefix_length_string = NULL;
1555 int prefix_length = 0;
1556 DBusMessageIter dict;
1558 DBG("ipconfig %p type %d", ipconfig, type);
1560 if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1561 type != CONNMAN_IPCONFIG_TYPE_IPV6)
1564 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1567 dbus_message_iter_recurse(array, &dict);
1569 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1570 DBusMessageIter entry;
1574 dbus_message_iter_recurse(&dict, &entry);
1576 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1579 dbus_message_iter_get_basic(&entry, &key);
1580 dbus_message_iter_next(&entry);
1582 type = dbus_message_iter_get_arg_type(&entry);
1584 if (g_str_equal(key, "Method") == TRUE) {
1587 if (type != DBUS_TYPE_STRING)
1590 dbus_message_iter_get_basic(&entry, &str);
1591 method = __connman_ipconfig_string2method(str);
1592 } else if (g_str_equal(key, "Address") == TRUE) {
1593 if (type != DBUS_TYPE_STRING)
1596 dbus_message_iter_get_basic(&entry, &address);
1597 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1598 if (type != DBUS_TYPE_STRING)
1601 dbus_message_iter_get_basic(&entry,
1602 &prefix_length_string);
1604 prefix_length = atoi(prefix_length_string);
1605 if (prefix_length < 0 || prefix_length > 128)
1608 } else if (g_str_equal(key, "Netmask") == TRUE) {
1609 if (type != DBUS_TYPE_STRING)
1612 dbus_message_iter_get_basic(&entry, &netmask);
1613 } else if (g_str_equal(key, "Gateway") == TRUE) {
1614 if (type != DBUS_TYPE_STRING)
1617 dbus_message_iter_get_basic(&entry, &gateway);
1619 dbus_message_iter_next(&dict);
1622 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1623 method, address, netmask, gateway, prefix_length);
1626 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1627 case CONNMAN_IPCONFIG_METHOD_OFF:
1628 case CONNMAN_IPCONFIG_METHOD_FIXED:
1631 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1632 if (address == NULL)
1635 ipconfig->method = method;
1637 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1638 connman_ipaddress_set_ipv4(ipconfig->address,
1639 address, netmask, gateway);
1641 return connman_ipaddress_set_ipv6(
1642 ipconfig->address, address,
1643 gateway, prefix_length);
1646 case CONNMAN_IPCONFIG_METHOD_DHCP:
1647 if (ipconfig->method == method)
1650 ipconfig->method = method;
1657 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1658 DBusMessageIter *iter)
1660 struct connman_ipdevice *ipdevice;
1661 const char *method = "auto";
1663 connman_dbus_dict_append_basic(iter, "Method",
1664 DBUS_TYPE_STRING, &method);
1666 ipdevice = g_hash_table_lookup(ipdevice_hash,
1667 GINT_TO_POINTER(ipconfig->index));
1668 if (ipdevice == NULL)
1671 if (ipdevice->ifname != NULL)
1672 connman_dbus_dict_append_basic(iter, "Interface",
1673 DBUS_TYPE_STRING, &ipdevice->ifname);
1675 if (ipdevice->address != NULL)
1676 connman_dbus_dict_append_basic(iter, "Address",
1677 DBUS_TYPE_STRING, &ipdevice->address);
1679 if (ipdevice->mtu > 0)
1680 connman_dbus_dict_append_basic(iter, "MTU",
1681 DBUS_TYPE_UINT16, &ipdevice->mtu);
1684 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1685 GKeyFile *keyfile, const char *identifier, const char *prefix)
1690 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1692 key = g_strdup_printf("%smethod", prefix);
1693 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1694 if (method == NULL) {
1695 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1696 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1698 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1700 ipconfig->method = __connman_ipconfig_string2method(method);
1703 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1704 ipconfig->address->prefixlen = g_key_file_get_integer(
1705 keyfile, identifier, key, NULL);
1708 key = g_strdup_printf("%slocal_address", prefix);
1709 ipconfig->address->local = g_key_file_get_string(
1710 keyfile, identifier, key, NULL);
1713 key = g_strdup_printf("%speer_address", prefix);
1714 ipconfig->address->peer = g_key_file_get_string(
1715 keyfile, identifier, key, NULL);
1718 key = g_strdup_printf("%sbroadcast_address", prefix);
1719 ipconfig->address->broadcast = g_key_file_get_string(
1720 keyfile, identifier, key, NULL);
1723 key = g_strdup_printf("%sgateway", prefix);
1724 ipconfig->address->gateway = g_key_file_get_string(
1725 keyfile, identifier, key, NULL);
1731 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1732 GKeyFile *keyfile, const char *identifier, const char *prefix)
1737 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1739 method = __connman_ipconfig_method2string(ipconfig->method);
1741 key = g_strdup_printf("%smethod", prefix);
1742 g_key_file_set_string(keyfile, identifier, key, method);
1745 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1746 g_key_file_set_integer(keyfile, identifier,
1747 key, ipconfig->address->prefixlen);
1750 key = g_strdup_printf("%slocal_address", prefix);
1751 if (ipconfig->address->local != NULL)
1752 g_key_file_set_string(keyfile, identifier,
1753 key, ipconfig->address->local);
1756 key = g_strdup_printf("%speer_address", prefix);
1757 if (ipconfig->address->peer != NULL)
1758 g_key_file_set_string(keyfile, identifier,
1759 key, ipconfig->address->peer);
1762 key = g_strdup_printf("%sbroadcast_address", prefix);
1763 if (ipconfig->address->broadcast != NULL)
1764 g_key_file_set_string(keyfile, identifier,
1765 key, ipconfig->address->broadcast);
1768 key = g_strdup_printf("%sgateway", prefix);
1769 if (ipconfig->address->gateway != NULL)
1770 g_key_file_set_string(keyfile, identifier,
1771 key, ipconfig->address->gateway);
1777 int __connman_ipconfig_init(void)
1781 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1782 NULL, free_ipdevice);
1787 void __connman_ipconfig_cleanup(void)
1791 g_hash_table_destroy(ipdevice_hash);
1792 ipdevice_hash = NULL;