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 struct connman_service *service;
428 if (stats->rx_packets == 0 && stats->tx_packets == 0)
431 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
432 stats->rx_packets, stats->rx_bytes);
433 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
434 stats->tx_packets, stats->tx_bytes);
436 if (ipdevice->config == NULL)
439 service = connman_ipconfig_get_data(ipdevice->config);
443 ipdevice->rx_packets = stats->rx_packets;
444 ipdevice->tx_packets = stats->tx_packets;
445 ipdevice->rx_bytes = stats->rx_bytes;
446 ipdevice->tx_bytes = stats->tx_bytes;
447 ipdevice->rx_errors = stats->rx_errors;
448 ipdevice->tx_errors = stats->tx_errors;
449 ipdevice->rx_dropped = stats->rx_dropped;
450 ipdevice->tx_dropped = stats->tx_dropped;
452 __connman_service_notify(service,
453 ipdevice->rx_packets, ipdevice->tx_packets,
454 ipdevice->rx_bytes, ipdevice->tx_bytes,
455 ipdevice->rx_errors, ipdevice->tx_errors,
456 ipdevice->rx_dropped, ipdevice->tx_dropped);
459 void __connman_ipconfig_newlink(int index, unsigned short type,
460 unsigned int flags, const char *address,
462 struct rtnl_link_stats *stats)
464 struct connman_ipdevice *ipdevice;
467 gboolean up = FALSE, down = FALSE;
468 gboolean lower_up = FALSE, lower_down = FALSE;
470 DBG("index %d", index);
472 if (type == ARPHRD_LOOPBACK)
475 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
476 if (ipdevice != NULL)
479 ipdevice = g_try_new0(struct connman_ipdevice, 1);
480 if (ipdevice == NULL)
483 ipdevice->index = index;
484 ipdevice->ifname = connman_inet_ifname(index);
485 ipdevice->type = type;
487 ipdevice->address = g_strdup(address);
489 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
491 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
492 index, type, type2str(type));
497 update_stats(ipdevice, stats);
499 if (flags == ipdevice->flags)
502 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
509 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
510 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
511 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
512 (IFF_RUNNING | IFF_LOWER_UP))
514 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
518 ipdevice->flags = flags;
520 str = g_string_new(NULL);
525 g_string_append(str, "UP");
527 g_string_append(str, "DOWN");
529 if (flags & IFF_RUNNING)
530 g_string_append(str, ",RUNNING");
532 if (flags & IFF_LOWER_UP)
533 g_string_append(str, ",LOWER_UP");
535 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
538 g_string_free(str, TRUE);
540 for (list = g_list_first(ipconfig_list); list;
541 list = g_list_next(list)) {
542 struct connman_ipconfig *ipconfig = list->data;
544 if (index != ipconfig->index)
547 if (ipconfig->ops == NULL)
550 if (up == TRUE && ipconfig->ops->up)
551 ipconfig->ops->up(ipconfig);
552 if (lower_up == TRUE && ipconfig->ops->lower_up)
553 ipconfig->ops->lower_up(ipconfig);
555 if (lower_down == TRUE && ipconfig->ops->lower_down)
556 ipconfig->ops->lower_down(ipconfig);
557 if (down == TRUE && ipconfig->ops->down)
558 ipconfig->ops->down(ipconfig);
562 __connman_ipconfig_lower_up(ipdevice);
564 __connman_ipconfig_lower_down(ipdevice);
567 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
569 struct connman_ipdevice *ipdevice;
572 DBG("index %d", index);
574 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
575 if (ipdevice == NULL)
578 update_stats(ipdevice, stats);
580 for (list = g_list_first(ipconfig_list); list;
581 list = g_list_next(list)) {
582 struct connman_ipconfig *ipconfig = list->data;
584 if (index != ipconfig->index)
587 ipconfig->index = -1;
589 if (ipconfig->ops == NULL)
592 if (ipconfig->ops->lower_down)
593 ipconfig->ops->lower_down(ipconfig);
594 if (ipconfig->ops->down)
595 ipconfig->ops->down(ipconfig);
598 __connman_ipconfig_lower_down(ipdevice);
600 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
603 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
605 const struct connman_ipaddress *addr1 = a;
606 const struct connman_ipaddress *addr2 = b;
608 if (addr1->prefixlen != addr2->prefixlen)
609 return addr2->prefixlen - addr1->prefixlen;
611 return g_strcmp0(addr1->local, addr2->local);
614 void __connman_ipconfig_newaddr(int index, int family, const char *label,
615 unsigned char prefixlen, const char *address)
617 struct connman_ipdevice *ipdevice;
618 struct connman_ipaddress *ipaddress;
621 DBG("index %d", index);
623 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
624 if (ipdevice == NULL)
627 ipaddress = connman_ipaddress_alloc(family);
628 if (ipaddress == NULL)
631 ipaddress->prefixlen = prefixlen;
632 ipaddress->local = g_strdup(address);
634 if (g_slist_find_custom(ipdevice->address_list, ipaddress,
635 check_duplicate_address)) {
636 connman_ipaddress_free(ipaddress);
640 ipdevice->address_list = g_slist_append(ipdevice->address_list,
643 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
644 address, prefixlen, label);
646 if (ipdevice->config != NULL) {
647 if (family == AF_INET6 && ipdevice->config->ipv6 != NULL)
648 connman_ipaddress_copy(ipdevice->config->ipv6->system,
651 connman_ipaddress_copy(ipdevice->config->system,
655 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
658 for (list = g_list_first(ipconfig_list); list;
659 list = g_list_next(list)) {
660 struct connman_ipconfig *ipconfig = list->data;
662 if (index != ipconfig->index)
665 if (ipconfig->ops == NULL)
668 if (ipconfig->ops->ip_bound)
669 ipconfig->ops->ip_bound(ipconfig);
673 void __connman_ipconfig_deladdr(int index, int family, const char *label,
674 unsigned char prefixlen, const char *address)
676 struct connman_ipdevice *ipdevice;
677 struct connman_ipaddress *ipaddress;
680 DBG("index %d", index);
682 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
683 if (ipdevice == NULL)
686 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
687 if (ipaddress == NULL)
690 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
693 connman_ipaddress_free(ipaddress);
695 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
696 address, prefixlen, label);
698 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
701 if (g_slist_length(ipdevice->address_list) > 0)
704 for (list = g_list_first(ipconfig_list); list;
705 list = g_list_next(list)) {
706 struct connman_ipconfig *ipconfig = list->data;
708 if (index != ipconfig->index)
711 if (ipconfig->ops == NULL)
714 if (ipconfig->ops->ip_release)
715 ipconfig->ops->ip_release(ipconfig);
719 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
720 const char *dst, const char *gateway)
722 struct connman_ipdevice *ipdevice;
724 DBG("index %d", index);
726 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
727 if (ipdevice == NULL)
730 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
734 if (family == AF_INET6) {
735 g_free(ipdevice->ipv6_gateway);
736 ipdevice->ipv6_gateway = g_strdup(gateway);
738 g_free(ipdevice->ipv4_gateway);
739 ipdevice->ipv4_gateway = g_strdup(gateway);
742 if (ipdevice->config != NULL &&
743 ipdevice->config->system != NULL) {
744 g_free(ipdevice->config->system->gateway);
745 ipdevice->config->system->gateway = g_strdup(gateway);
748 for (list = ipdevice->address_list; list; list = list->next) {
749 struct connman_ipaddress *ipaddress = list->data;
751 g_free(ipaddress->gateway);
752 ipaddress->gateway = g_strdup(gateway);
755 for (config_list = g_list_first(ipconfig_list); config_list;
756 config_list = g_list_next(config_list)) {
757 struct connman_ipconfig *ipconfig = config_list->data;
759 if (index != ipconfig->index)
762 if (ipconfig->ops == NULL)
765 if (ipconfig->ops->ip_bound)
766 ipconfig->ops->ip_bound(ipconfig);
770 connman_info("%s {add} route %s gw %s scope %u <%s>",
771 ipdevice->ifname, dst, gateway,
772 scope, scope2str(scope));
775 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
776 const char *dst, const char *gateway)
778 struct connman_ipdevice *ipdevice;
780 DBG("index %d", index);
782 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
783 if (ipdevice == NULL)
786 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
790 if (family == AF_INET6) {
791 g_free(ipdevice->ipv6_gateway);
792 ipdevice->ipv6_gateway = NULL;
794 g_free(ipdevice->ipv4_gateway);
795 ipdevice->ipv4_gateway = NULL;
798 if (ipdevice->config != NULL &&
799 ipdevice->config->system != NULL) {
800 g_free(ipdevice->config->system->gateway);
801 ipdevice->config->system->gateway = NULL;
804 for (list = ipdevice->address_list; list; list = list->next) {
805 struct connman_ipaddress *ipaddress = list->data;
807 g_free(ipaddress->gateway);
808 ipaddress->gateway = NULL;
811 for (config_list = g_list_first(ipconfig_list); config_list;
812 config_list = g_list_next(config_list)) {
813 struct connman_ipconfig *ipconfig = config_list->data;
815 if (index != ipconfig->index)
818 if (ipconfig->ops == NULL)
821 if (ipconfig->ops->ip_release)
822 ipconfig->ops->ip_release(ipconfig);
826 connman_info("%s {del} route %s gw %s scope %u <%s>",
827 ipdevice->ifname, dst, gateway,
828 scope, scope2str(scope));
831 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
836 keys = g_hash_table_get_keys(ipdevice_hash);
840 for (list = g_list_first(keys); list; list = g_list_next(list)) {
841 int index = GPOINTER_TO_INT(list->data);
843 function(index, user_data);
849 unsigned short __connman_ipconfig_get_type(int index)
851 struct connman_ipdevice *ipdevice;
853 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
854 if (ipdevice == NULL)
857 return ipdevice->type;
860 unsigned int __connman_ipconfig_get_flags(int index)
862 struct connman_ipdevice *ipdevice;
864 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
865 if (ipdevice == NULL)
868 return ipdevice->flags;
871 const char *__connman_ipconfig_get_gateway(int index)
873 struct connman_ipdevice *ipdevice;
875 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
876 if (ipdevice == NULL)
879 if (ipdevice->ipv4_gateway != NULL)
880 return ipdevice->ipv4_gateway;
882 if (ipdevice->config != NULL &&
883 ipdevice->config->address != NULL)
884 return ipdevice->config->address->gateway;
889 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
891 ipconfig->index = index;
893 if (ipconfig->ipv6 != NULL)
894 ipconfig->ipv6->index = index;
897 static struct connman_ipconfig *create_ipv6config(int index)
899 struct connman_ipconfig *ipv6config;
901 DBG("index %d", index);
903 ipv6config = g_try_new0(struct connman_ipconfig, 1);
904 if (ipv6config == NULL)
907 ipv6config->index = index;
908 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
909 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
911 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
912 if (ipv6config->address == NULL) {
917 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
919 ipv6config->ipv6 = NULL;
921 DBG("ipconfig %p", ipv6config);
927 * connman_ipconfig_create:
929 * Allocate a new ipconfig structure.
931 * Returns: a newly-allocated #connman_ipconfig structure
933 struct connman_ipconfig *connman_ipconfig_create(int index)
935 struct connman_ipconfig *ipconfig;
937 DBG("index %d", index);
939 ipconfig = g_try_new0(struct connman_ipconfig, 1);
940 if (ipconfig == NULL)
943 ipconfig->refcount = 1;
945 ipconfig->index = index;
946 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
948 ipconfig->address = connman_ipaddress_alloc(AF_INET);
949 if (ipconfig->address == NULL) {
954 ipconfig->system = connman_ipaddress_alloc(AF_INET);
956 ipconfig->ipv6 = create_ipv6config(index);
958 DBG("ipconfig %p", ipconfig);
964 * connman_ipconfig_clone:
966 * Clone an ipconfig structure and create new reference.
968 * Returns: a newly-allocated #connman_ipconfig structure
970 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
972 struct connman_ipconfig *ipconfig_clone;
974 DBG("ipconfig %p", ipconfig);
976 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
977 if (ipconfig_clone == NULL)
980 ipconfig_clone->refcount = 1;
982 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
984 ipconfig_clone->index = -1;
986 return ipconfig_clone;
990 * connman_ipconfig_ref:
991 * @ipconfig: ipconfig structure
993 * Increase reference counter of ipconfig
995 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
997 g_atomic_int_inc(&ipconfig->refcount);
1002 static void free_ipv6config(struct connman_ipconfig *ipconfig)
1004 if (ipconfig == NULL)
1007 connman_ipconfig_set_ops(ipconfig, NULL);
1008 connman_ipaddress_free(ipconfig->system);
1009 connman_ipaddress_free(ipconfig->address);
1010 g_free(ipconfig->ipv6);
1014 * connman_ipconfig_unref:
1015 * @ipconfig: ipconfig structure
1017 * Decrease reference counter of ipconfig
1019 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1022 g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1023 __connman_ipconfig_disable(ipconfig);
1025 connman_ipconfig_set_ops(ipconfig, NULL);
1027 if (ipconfig->origin != NULL) {
1028 connman_ipconfig_unref(ipconfig->origin);
1029 ipconfig->origin = NULL;
1032 connman_ipaddress_free(ipconfig->system);
1033 connman_ipaddress_free(ipconfig->address);
1034 free_ipv6config(ipconfig->ipv6);
1040 * connman_ipconfig_get_data:
1041 * @ipconfig: ipconfig structure
1043 * Get private data pointer
1045 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1047 return ipconfig->ops_data;
1051 * connman_ipconfig_set_data:
1052 * @ipconfig: ipconfig structure
1053 * @data: data pointer
1055 * Set private data pointer
1057 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1059 ipconfig->ops_data = data;
1063 * connman_ipconfig_get_index:
1064 * @ipconfig: ipconfig structure
1066 * Get interface index
1068 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1070 if (ipconfig == NULL)
1073 if (ipconfig->origin != NULL)
1074 return ipconfig->origin->index;
1076 return ipconfig->index;
1080 * connman_ipconfig_get_ifname:
1081 * @ipconfig: ipconfig structure
1083 * Get interface name
1085 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1087 struct connman_ipdevice *ipdevice;
1089 if (ipconfig == NULL)
1092 if (ipconfig->index < 0)
1095 ipdevice = g_hash_table_lookup(ipdevice_hash,
1096 GINT_TO_POINTER(ipconfig->index));
1097 if (ipdevice == NULL)
1100 return ipdevice->ifname;
1104 * connman_ipconfig_set_ops:
1105 * @ipconfig: ipconfig structure
1106 * @ops: operation callbacks
1108 * Set the operation callbacks
1110 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1111 const struct connman_ipconfig_ops *ops)
1113 ipconfig->ops = ops;
1116 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1117 struct connman_ipconfig *ipconfig)
1119 if (ipconfig == NULL)
1122 return ipconfig->ipv6;
1126 * connman_ipconfig_set_method:
1127 * @ipconfig: ipconfig structure
1128 * @method: configuration method
1130 * Set the configuration method
1132 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1133 enum connman_ipconfig_method method)
1135 ipconfig->method = method;
1140 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1142 if (ipconfig == NULL)
1143 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1145 return ipconfig->method;
1149 * connman_ipconfig_bind:
1150 * @ipconfig: ipconfig structure
1151 * @ipaddress: ipaddress structure
1153 * Bind IP address details to configuration
1155 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1156 struct connman_ipaddress *ipaddress)
1158 struct connman_ipconfig *origin;
1160 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1162 connman_ipaddress_copy(origin->address, ipaddress);
1164 connman_inet_set_address(origin->index, origin->address);
1167 void __connman_ipconfig_set_element_ipv6_gateway(
1168 struct connman_ipconfig *ipconfig,
1169 struct connman_element *element)
1171 element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1175 * FIXME: The element soulution should be removed in the future
1176 * Set IPv4 and IPv6 gateway
1178 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1179 struct connman_element *parent)
1181 struct connman_element *connection;
1183 connection = connman_element_create(NULL);
1185 DBG("ipconfig %p", ipconfig);
1187 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1188 connection->index = ipconfig->index;
1189 connection->ipv4.gateway = ipconfig->address->gateway;
1190 connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1192 if (connman_element_register(connection, parent) < 0)
1193 connman_element_unref(connection);
1198 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1202 switch (ipconfig->method) {
1203 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1204 case CONNMAN_IPCONFIG_METHOD_OFF:
1205 case CONNMAN_IPCONFIG_METHOD_FIXED:
1206 case CONNMAN_IPCONFIG_METHOD_DHCP:
1208 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1209 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1210 return connman_inet_set_address(ipconfig->index,
1212 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1213 return connman_inet_set_ipv6_address(
1214 ipconfig->index, ipconfig->address);
1220 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1224 if (ipconfig == NULL)
1227 DBG("method %d", ipconfig->method);
1229 switch (ipconfig->method) {
1230 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1231 case CONNMAN_IPCONFIG_METHOD_OFF:
1232 case CONNMAN_IPCONFIG_METHOD_FIXED:
1233 case CONNMAN_IPCONFIG_METHOD_DHCP:
1235 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1236 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1237 return connman_inet_clear_address(ipconfig->index,
1239 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1240 return connman_inet_clear_ipv6_address(
1242 ipconfig->address->local,
1243 ipconfig->address->prefixlen);
1249 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1252 struct connman_ipdevice *ipdevice;
1254 DBG("ipconfig %p", ipconfig);
1256 if (ipconfig == NULL || ipconfig->index < 0)
1259 ipdevice = g_hash_table_lookup(ipdevice_hash,
1260 GINT_TO_POINTER(ipconfig->index));
1261 if (ipdevice == NULL)
1264 g_free(ipdevice->pac);
1265 ipdevice->pac = g_strdup(url);
1270 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1272 struct connman_ipdevice *ipdevice;
1274 DBG("ipconfig %p", ipconfig);
1276 if (ipconfig == NULL || ipconfig->index < 0)
1279 ipdevice = g_hash_table_lookup(ipdevice_hash,
1280 GINT_TO_POINTER(ipconfig->index));
1281 if (ipdevice == NULL)
1284 return ipdevice->pac;
1287 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1289 struct connman_ipdevice *ipdevice;
1290 gboolean up = FALSE, down = FALSE;
1291 gboolean lower_up = FALSE, lower_down = FALSE;
1293 DBG("ipconfig %p", ipconfig);
1295 if (ipconfig == NULL || ipconfig->index < 0)
1298 ipdevice = g_hash_table_lookup(ipdevice_hash,
1299 GINT_TO_POINTER(ipconfig->index));
1300 if (ipdevice == NULL)
1303 if (ipdevice->config == ipconfig)
1306 if (ipdevice->config != NULL) {
1307 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1309 connman_ipaddress_clear(ipdevice->config->system);
1311 connman_ipconfig_unref(ipdevice->config);
1314 ipdevice->config = connman_ipconfig_ref(ipconfig);
1316 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1318 if (ipdevice->flags & IFF_UP)
1323 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1324 (IFF_RUNNING | IFF_LOWER_UP))
1326 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1329 if (up == TRUE && ipconfig->ops->up)
1330 ipconfig->ops->up(ipconfig);
1331 if (lower_up == TRUE && ipconfig->ops->lower_up)
1332 ipconfig->ops->lower_up(ipconfig);
1334 if (lower_down == TRUE && ipconfig->ops->lower_down)
1335 ipconfig->ops->lower_down(ipconfig);
1336 if (down == TRUE && ipconfig->ops->down)
1337 ipconfig->ops->down(ipconfig);
1342 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1344 struct connman_ipdevice *ipdevice;
1346 DBG("ipconfig %p", ipconfig);
1348 if (ipconfig == NULL || ipconfig->index < 0)
1351 ipdevice = g_hash_table_lookup(ipdevice_hash,
1352 GINT_TO_POINTER(ipconfig->index));
1353 if (ipdevice == NULL)
1356 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1359 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1361 connman_ipaddress_clear(ipdevice->config->system);
1362 connman_ipaddress_clear(ipdevice->config->ipv6->system);
1364 connman_ipconfig_unref(ipdevice->config);
1365 ipdevice->config = NULL;
1370 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1373 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1375 case CONNMAN_IPCONFIG_METHOD_OFF:
1377 case CONNMAN_IPCONFIG_METHOD_FIXED:
1379 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1381 case CONNMAN_IPCONFIG_METHOD_DHCP:
1388 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1390 if (g_strcmp0(method, "off") == 0)
1391 return CONNMAN_IPCONFIG_METHOD_OFF;
1392 else if (g_strcmp0(method, "fixed") == 0)
1393 return CONNMAN_IPCONFIG_METHOD_FIXED;
1394 else if (g_strcmp0(method, "manual") == 0)
1395 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1396 else if (g_strcmp0(method, "dhcp") == 0)
1397 return CONNMAN_IPCONFIG_METHOD_DHCP;
1399 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1402 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1403 DBusMessageIter *iter)
1409 str = __connman_ipconfig_method2string(ipconfig->method);
1413 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1415 if (ipconfig->system == NULL)
1418 if (ipconfig->system->local != NULL) {
1420 struct in_addr netmask;
1423 connman_dbus_dict_append_basic(iter, "Address",
1424 DBUS_TYPE_STRING, &ipconfig->system->local);
1426 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1427 netmask.s_addr = htonl(addr);
1428 mask = inet_ntoa(netmask);
1429 connman_dbus_dict_append_basic(iter, "Netmask",
1430 DBUS_TYPE_STRING, &mask);
1433 if (ipconfig->system->gateway != NULL)
1434 connman_dbus_dict_append_basic(iter, "Gateway",
1435 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1438 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1439 DBusMessageIter *iter)
1445 str = __connman_ipconfig_method2string(ipconfig->method);
1449 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1451 if (ipconfig->system == NULL)
1454 if (ipconfig->system->local != NULL) {
1455 connman_dbus_dict_append_basic(iter, "Address",
1456 DBUS_TYPE_STRING, &ipconfig->system->local);
1457 connman_dbus_dict_append_basic(iter, "PrefixLength",
1459 &ipconfig->system->prefixlen);
1462 if (ipconfig->system->gateway != NULL)
1463 connman_dbus_dict_append_basic(iter, "Gateway",
1464 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1467 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1468 DBusMessageIter *iter)
1474 str = __connman_ipconfig_method2string(ipconfig->method);
1478 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1480 switch (ipconfig->method) {
1481 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1482 case CONNMAN_IPCONFIG_METHOD_OFF:
1483 case CONNMAN_IPCONFIG_METHOD_DHCP:
1485 case CONNMAN_IPCONFIG_METHOD_FIXED:
1486 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1490 if (ipconfig->address == NULL)
1493 if (ipconfig->address->local != NULL) {
1494 connman_dbus_dict_append_basic(iter, "Address",
1495 DBUS_TYPE_STRING, &ipconfig->address->local);
1496 connman_dbus_dict_append_basic(iter, "PrefixLength",
1498 &ipconfig->address->prefixlen);
1501 if (ipconfig->address->gateway != NULL)
1502 connman_dbus_dict_append_basic(iter, "Gateway",
1503 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1506 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1507 DBusMessageIter *iter)
1513 str = __connman_ipconfig_method2string(ipconfig->method);
1517 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1519 switch (ipconfig->method) {
1520 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1521 case CONNMAN_IPCONFIG_METHOD_OFF:
1522 case CONNMAN_IPCONFIG_METHOD_FIXED:
1523 case CONNMAN_IPCONFIG_METHOD_DHCP:
1525 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1529 if (ipconfig->address == NULL)
1532 if (ipconfig->address->local != NULL) {
1534 struct in_addr netmask;
1537 connman_dbus_dict_append_basic(iter, "Address",
1538 DBUS_TYPE_STRING, &ipconfig->address->local);
1540 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1541 netmask.s_addr = htonl(addr);
1542 mask = inet_ntoa(netmask);
1543 connman_dbus_dict_append_basic(iter, "Netmask",
1544 DBUS_TYPE_STRING, &mask);
1547 if (ipconfig->address->gateway != NULL)
1548 connman_dbus_dict_append_basic(iter, "Gateway",
1549 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1552 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1553 enum connman_ipconfig_type type, DBusMessageIter *array)
1555 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1556 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1557 *prefix_length_string = NULL;
1558 int prefix_length = 0;
1559 DBusMessageIter dict;
1561 DBG("ipconfig %p type %d", ipconfig, type);
1563 if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1564 type != CONNMAN_IPCONFIG_TYPE_IPV6)
1567 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1570 dbus_message_iter_recurse(array, &dict);
1572 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1573 DBusMessageIter entry;
1577 dbus_message_iter_recurse(&dict, &entry);
1579 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1582 dbus_message_iter_get_basic(&entry, &key);
1583 dbus_message_iter_next(&entry);
1585 type = dbus_message_iter_get_arg_type(&entry);
1587 if (g_str_equal(key, "Method") == TRUE) {
1590 if (type != DBUS_TYPE_STRING)
1593 dbus_message_iter_get_basic(&entry, &str);
1594 method = __connman_ipconfig_string2method(str);
1595 } else if (g_str_equal(key, "Address") == TRUE) {
1596 if (type != DBUS_TYPE_STRING)
1599 dbus_message_iter_get_basic(&entry, &address);
1600 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1601 if (type != DBUS_TYPE_STRING)
1604 dbus_message_iter_get_basic(&entry,
1605 &prefix_length_string);
1607 prefix_length = atoi(prefix_length_string);
1608 if (prefix_length < 0 || prefix_length > 128)
1611 } else if (g_str_equal(key, "Netmask") == TRUE) {
1612 if (type != DBUS_TYPE_STRING)
1615 dbus_message_iter_get_basic(&entry, &netmask);
1616 } else if (g_str_equal(key, "Gateway") == TRUE) {
1617 if (type != DBUS_TYPE_STRING)
1620 dbus_message_iter_get_basic(&entry, &gateway);
1622 dbus_message_iter_next(&dict);
1625 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1626 method, address, netmask, gateway, prefix_length);
1629 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1630 case CONNMAN_IPCONFIG_METHOD_OFF:
1631 case CONNMAN_IPCONFIG_METHOD_FIXED:
1634 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1635 if (address == NULL)
1638 ipconfig->method = method;
1640 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1641 connman_ipaddress_set_ipv4(ipconfig->address,
1642 address, netmask, gateway);
1644 return connman_ipaddress_set_ipv6(
1645 ipconfig->address, address,
1646 gateway, prefix_length);
1649 case CONNMAN_IPCONFIG_METHOD_DHCP:
1650 if (ipconfig->method == method)
1653 ipconfig->method = method;
1660 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1661 DBusMessageIter *iter)
1663 struct connman_ipdevice *ipdevice;
1664 const char *method = "auto";
1666 connman_dbus_dict_append_basic(iter, "Method",
1667 DBUS_TYPE_STRING, &method);
1669 ipdevice = g_hash_table_lookup(ipdevice_hash,
1670 GINT_TO_POINTER(ipconfig->index));
1671 if (ipdevice == NULL)
1674 if (ipdevice->ifname != NULL)
1675 connman_dbus_dict_append_basic(iter, "Interface",
1676 DBUS_TYPE_STRING, &ipdevice->ifname);
1678 if (ipdevice->address != NULL)
1679 connman_dbus_dict_append_basic(iter, "Address",
1680 DBUS_TYPE_STRING, &ipdevice->address);
1682 if (ipdevice->mtu > 0)
1683 connman_dbus_dict_append_basic(iter, "MTU",
1684 DBUS_TYPE_UINT16, &ipdevice->mtu);
1687 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1688 GKeyFile *keyfile, const char *identifier, const char *prefix)
1693 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1695 key = g_strdup_printf("%smethod", prefix);
1696 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1697 if (method == NULL) {
1698 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1699 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1701 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1703 ipconfig->method = __connman_ipconfig_string2method(method);
1706 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1707 ipconfig->address->prefixlen = g_key_file_get_integer(
1708 keyfile, identifier, key, NULL);
1711 key = g_strdup_printf("%slocal_address", prefix);
1712 ipconfig->address->local = g_key_file_get_string(
1713 keyfile, identifier, key, NULL);
1716 key = g_strdup_printf("%speer_address", prefix);
1717 ipconfig->address->peer = g_key_file_get_string(
1718 keyfile, identifier, key, NULL);
1721 key = g_strdup_printf("%sbroadcast_address", prefix);
1722 ipconfig->address->broadcast = g_key_file_get_string(
1723 keyfile, identifier, key, NULL);
1726 key = g_strdup_printf("%sgateway", prefix);
1727 ipconfig->address->gateway = g_key_file_get_string(
1728 keyfile, identifier, key, NULL);
1734 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1735 GKeyFile *keyfile, const char *identifier, const char *prefix)
1740 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1742 method = __connman_ipconfig_method2string(ipconfig->method);
1744 key = g_strdup_printf("%smethod", prefix);
1745 g_key_file_set_string(keyfile, identifier, key, method);
1748 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1749 g_key_file_set_integer(keyfile, identifier,
1750 key, ipconfig->address->prefixlen);
1753 key = g_strdup_printf("%slocal_address", prefix);
1754 if (ipconfig->address->local != NULL)
1755 g_key_file_set_string(keyfile, identifier,
1756 key, ipconfig->address->local);
1759 key = g_strdup_printf("%speer_address", prefix);
1760 if (ipconfig->address->peer != NULL)
1761 g_key_file_set_string(keyfile, identifier,
1762 key, ipconfig->address->peer);
1765 key = g_strdup_printf("%sbroadcast_address", prefix);
1766 if (ipconfig->address->broadcast != NULL)
1767 g_key_file_set_string(keyfile, identifier,
1768 key, ipconfig->address->broadcast);
1771 key = g_strdup_printf("%sgateway", prefix);
1772 if (ipconfig->address->gateway != NULL)
1773 g_key_file_set_string(keyfile, identifier,
1774 key, ipconfig->address->gateway);
1780 int __connman_ipconfig_init(void)
1784 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1785 NULL, free_ipdevice);
1790 void __connman_ipconfig_cleanup(void)
1794 g_hash_table_destroy(ipdevice_hash);
1795 ipdevice_hash = NULL;