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 {
77 struct connman_ipconfig *config;
79 struct connman_ipconfig_driver *driver;
80 struct connman_ipconfig *driver_config;
83 static GHashTable *ipdevice_hash = NULL;
84 static GList *ipconfig_list = NULL;
86 struct connman_ipaddress *connman_ipaddress_alloc(int family)
88 struct connman_ipaddress *ipaddress;
90 ipaddress = g_try_new0(struct connman_ipaddress, 1);
91 if (ipaddress == NULL)
94 ipaddress->family = family;
95 ipaddress->prefixlen = 0;
96 ipaddress->local = NULL;
97 ipaddress->peer = NULL;
98 ipaddress->broadcast = NULL;
99 ipaddress->gateway = NULL;
104 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
106 if (ipaddress == NULL)
109 g_free(ipaddress->broadcast);
110 g_free(ipaddress->peer);
111 g_free(ipaddress->local);
112 g_free(ipaddress->gateway);
116 static unsigned char netmask2prefixlen(const char *netmask)
118 unsigned char bits = 0;
119 in_addr_t mask = inet_network(netmask);
120 in_addr_t host = ~mask;
122 /* a valid netmask must be 2^n - 1 */
123 if ((host & (host + 1)) != 0)
126 for (; mask; mask <<= 1)
132 static gboolean check_ipv6_address(const char *address)
134 unsigned char buf[sizeof(struct in6_addr)];
137 err = inet_pton(AF_INET6, address, buf);
144 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
145 const char *address, const char *gateway,
146 unsigned char prefix_length)
148 if (ipaddress == NULL)
151 if (check_ipv6_address(address) == FALSE)
154 if (check_ipv6_address(gateway) == FALSE)
157 DBG("prefix_len %d address %s gateway %s",
158 prefix_length, address, gateway);
160 ipaddress->prefixlen = prefix_length;
162 g_free(ipaddress->local);
163 ipaddress->local = g_strdup(address);
165 g_free(ipaddress->gateway);
166 ipaddress->gateway = g_strdup(gateway);
171 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
172 const char *address, const char *netmask, const char *gateway)
174 if (ipaddress == NULL)
178 ipaddress->prefixlen = netmask2prefixlen(netmask);
180 ipaddress->prefixlen = 32;
182 g_free(ipaddress->local);
183 ipaddress->local = g_strdup(address);
185 g_free(ipaddress->gateway);
186 ipaddress->gateway = g_strdup(gateway);
189 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
191 if (ipaddress == NULL)
194 ipaddress->prefixlen = 0;
196 g_free(ipaddress->local);
197 ipaddress->local = NULL;
199 g_free(ipaddress->peer);
200 ipaddress->peer = NULL;
202 g_free(ipaddress->broadcast);
203 ipaddress->broadcast = NULL;
205 g_free(ipaddress->gateway);
206 ipaddress->gateway = NULL;
209 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
210 struct connman_ipaddress *source)
212 if (ipaddress == NULL || source == NULL)
215 ipaddress->family = source->family;
216 ipaddress->prefixlen = source->prefixlen;
218 g_free(ipaddress->local);
219 ipaddress->local = g_strdup(source->local);
221 g_free(ipaddress->peer);
222 ipaddress->peer = g_strdup(source->peer);
224 g_free(ipaddress->broadcast);
225 ipaddress->broadcast = g_strdup(source->broadcast);
227 g_free(ipaddress->gateway);
228 ipaddress->gateway = g_strdup(source->gateway);
231 static void free_address_list(struct connman_ipdevice *ipdevice)
235 for (list = ipdevice->address_list; list; list = list->next) {
236 struct connman_ipaddress *ipaddress = list->data;
238 connman_ipaddress_free(ipaddress);
242 g_slist_free(ipdevice->address_list);
243 ipdevice->address_list = NULL;
246 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
247 unsigned char prefixlen, const char *local)
251 for (list = ipdevice->address_list; list; list = list->next) {
252 struct connman_ipaddress *ipaddress = list->data;
254 if (g_strcmp0(ipaddress->local, local) == 0 &&
255 ipaddress->prefixlen == prefixlen)
262 static const char *type2str(unsigned short type)
267 case ARPHRD_LOOPBACK:
280 static const char *scope2str(unsigned char scope)
292 static void free_ipdevice(gpointer data)
294 struct connman_ipdevice *ipdevice = data;
296 connman_info("%s {remove} index %d", ipdevice->ifname,
299 if (ipdevice->config != NULL)
300 connman_ipconfig_unref(ipdevice->config);
302 free_address_list(ipdevice);
303 g_free(ipdevice->ipv4_gateway);
304 g_free(ipdevice->ipv6_gateway);
306 g_free(ipdevice->address);
307 g_free(ipdevice->ifname);
311 static GSList *driver_list = NULL;
313 static gint compare_priority(gconstpointer a, gconstpointer b)
315 const struct connman_ipconfig_driver *driver1 = a;
316 const struct connman_ipconfig_driver *driver2 = b;
318 return driver2->priority - driver1->priority;
322 * connman_ipconfig_driver_register:
323 * @driver: IP configuration driver
325 * Register a new IP configuration driver
327 * Returns: %0 on success
329 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
331 DBG("driver %p name %s", driver, driver->name);
333 driver_list = g_slist_insert_sorted(driver_list, driver,
340 * connman_ipconfig_driver_unregister:
341 * @driver: IP configuration driver
343 * Remove a previously registered IP configuration driver.
345 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
347 DBG("driver %p name %s", driver, driver->name);
349 driver_list = g_slist_remove(driver_list, driver);
352 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
356 DBG("ipconfig %p", ipdevice->config);
358 if (ipdevice->config == NULL)
361 switch (ipdevice->config->method) {
362 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
363 case CONNMAN_IPCONFIG_METHOD_OFF:
364 case CONNMAN_IPCONFIG_METHOD_FIXED:
365 case CONNMAN_IPCONFIG_METHOD_MANUAL:
367 case CONNMAN_IPCONFIG_METHOD_DHCP:
371 if (ipdevice->driver != NULL)
374 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
375 if (ipdevice->driver_config == NULL)
378 for (list = driver_list; list; list = list->next) {
379 struct connman_ipconfig_driver *driver = list->data;
381 if (driver->request(ipdevice->driver_config) == 0) {
382 ipdevice->driver = driver;
387 if (ipdevice->driver == NULL) {
388 connman_ipconfig_unref(ipdevice->driver_config);
389 ipdevice->driver_config = NULL;
393 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
395 DBG("ipconfig %p", ipdevice->config);
397 if (ipdevice->config == NULL)
400 if (ipdevice->driver == NULL)
403 ipdevice->driver->release(ipdevice->driver_config);
405 ipdevice->driver = NULL;
407 connman_ipconfig_unref(ipdevice->driver_config);
408 ipdevice->driver_config = NULL;
410 connman_inet_clear_address(ipdevice->index);
411 connman_inet_clear_ipv6_address(ipdevice->index,
412 ipdevice->driver_config->address->local,
413 ipdevice->driver_config->address->prefixlen);
416 static void update_stats(struct connman_ipdevice *ipdevice,
417 struct rtnl_link_stats *stats)
419 if (stats->rx_packets == 0 && stats->tx_packets == 0)
422 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
423 stats->rx_packets, stats->rx_bytes);
424 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
425 stats->tx_packets, stats->tx_bytes);
427 if (ipdevice->config == NULL)
430 ipdevice->rx_packets = stats->rx_packets;
431 ipdevice->tx_packets = stats->tx_packets;
432 ipdevice->rx_bytes = stats->rx_bytes;
433 ipdevice->tx_bytes = stats->tx_bytes;
434 ipdevice->rx_errors = stats->rx_errors;
435 ipdevice->tx_errors = stats->tx_errors;
436 ipdevice->rx_dropped = stats->rx_dropped;
437 ipdevice->tx_dropped = stats->tx_dropped;
439 __connman_counter_notify(ipdevice->config,
440 ipdevice->rx_packets, ipdevice->tx_packets,
441 ipdevice->rx_bytes, ipdevice->tx_bytes,
442 ipdevice->rx_errors, ipdevice->tx_errors,
443 ipdevice->rx_dropped, ipdevice->tx_dropped);
446 void __connman_ipconfig_newlink(int index, unsigned short type,
447 unsigned int flags, const char *address,
449 struct rtnl_link_stats *stats)
451 struct connman_ipdevice *ipdevice;
454 gboolean up = FALSE, down = FALSE;
455 gboolean lower_up = FALSE, lower_down = FALSE;
457 DBG("index %d", index);
459 if (type == ARPHRD_LOOPBACK)
462 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
463 if (ipdevice != NULL)
466 ipdevice = g_try_new0(struct connman_ipdevice, 1);
467 if (ipdevice == NULL)
470 ipdevice->index = index;
471 ipdevice->ifname = connman_inet_ifname(index);
472 ipdevice->type = type;
474 ipdevice->address = g_strdup(address);
476 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
478 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
479 index, type, type2str(type));
484 update_stats(ipdevice, stats);
486 if (flags == ipdevice->flags)
489 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
496 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
497 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
498 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
499 (IFF_RUNNING | IFF_LOWER_UP))
501 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
505 ipdevice->flags = flags;
507 str = g_string_new(NULL);
512 g_string_append(str, "UP");
514 g_string_append(str, "DOWN");
516 if (flags & IFF_RUNNING)
517 g_string_append(str, ",RUNNING");
519 if (flags & IFF_LOWER_UP)
520 g_string_append(str, ",LOWER_UP");
522 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
525 g_string_free(str, TRUE);
527 for (list = g_list_first(ipconfig_list); list;
528 list = g_list_next(list)) {
529 struct connman_ipconfig *ipconfig = list->data;
531 if (index != ipconfig->index)
534 if (ipconfig->ops == NULL)
537 if (up == TRUE && ipconfig->ops->up)
538 ipconfig->ops->up(ipconfig);
539 if (lower_up == TRUE && ipconfig->ops->lower_up)
540 ipconfig->ops->lower_up(ipconfig);
542 if (lower_down == TRUE && ipconfig->ops->lower_down)
543 ipconfig->ops->lower_down(ipconfig);
544 if (down == TRUE && ipconfig->ops->down)
545 ipconfig->ops->down(ipconfig);
549 __connman_ipconfig_lower_up(ipdevice);
551 __connman_ipconfig_lower_down(ipdevice);
554 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
556 struct connman_ipdevice *ipdevice;
559 DBG("index %d", index);
561 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
562 if (ipdevice == NULL)
565 update_stats(ipdevice, stats);
567 for (list = g_list_first(ipconfig_list); list;
568 list = g_list_next(list)) {
569 struct connman_ipconfig *ipconfig = list->data;
571 if (index != ipconfig->index)
574 ipconfig->index = -1;
576 if (ipconfig->ops == NULL)
579 if (ipconfig->ops->lower_down)
580 ipconfig->ops->lower_down(ipconfig);
581 if (ipconfig->ops->down)
582 ipconfig->ops->down(ipconfig);
585 __connman_ipconfig_lower_down(ipdevice);
587 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
590 void __connman_ipconfig_newaddr(int index, int family, const char *label,
591 unsigned char prefixlen, const char *address)
593 struct connman_ipdevice *ipdevice;
594 struct connman_ipaddress *ipaddress;
597 DBG("index %d", index);
599 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
600 if (ipdevice == NULL)
603 ipaddress = connman_ipaddress_alloc(family);
604 if (ipaddress == NULL)
607 ipaddress->prefixlen = prefixlen;
608 ipaddress->local = g_strdup(address);
610 ipdevice->address_list = g_slist_append(ipdevice->address_list,
613 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
614 address, prefixlen, label);
616 if (ipdevice->config != NULL) {
617 if (family == AF_INET6 && ipdevice->config->ipv6 != NULL)
618 connman_ipaddress_copy(ipdevice->config->ipv6->system,
621 connman_ipaddress_copy(ipdevice->config->system,
625 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
628 if (g_slist_length(ipdevice->address_list) > 1)
631 for (list = g_list_first(ipconfig_list); list;
632 list = g_list_next(list)) {
633 struct connman_ipconfig *ipconfig = list->data;
635 if (index != ipconfig->index)
638 if (ipconfig->ops == NULL)
641 if (ipconfig->ops->ip_bound)
642 ipconfig->ops->ip_bound(ipconfig);
646 void __connman_ipconfig_deladdr(int index, int family, const char *label,
647 unsigned char prefixlen, const char *address)
649 struct connman_ipdevice *ipdevice;
650 struct connman_ipaddress *ipaddress;
653 DBG("index %d", index);
655 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
656 if (ipdevice == NULL)
659 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
660 if (ipaddress == NULL)
663 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
666 connman_ipaddress_free(ipaddress);
668 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
669 address, prefixlen, label);
671 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
674 if (g_slist_length(ipdevice->address_list) > 0)
677 for (list = g_list_first(ipconfig_list); list;
678 list = g_list_next(list)) {
679 struct connman_ipconfig *ipconfig = list->data;
681 if (index != ipconfig->index)
684 if (ipconfig->ops == NULL)
687 if (ipconfig->ops->ip_release)
688 ipconfig->ops->ip_release(ipconfig);
692 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
693 const char *dst, const char *gateway)
695 struct connman_ipdevice *ipdevice;
697 DBG("index %d", index);
699 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
700 if (ipdevice == NULL)
703 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
706 if (family == AF_INET6) {
707 g_free(ipdevice->ipv6_gateway);
708 ipdevice->ipv6_gateway = g_strdup(gateway);
710 g_free(ipdevice->ipv4_gateway);
711 ipdevice->ipv4_gateway = g_strdup(gateway);
714 if (ipdevice->config != NULL &&
715 ipdevice->config->system != NULL) {
716 g_free(ipdevice->config->system->gateway);
717 ipdevice->config->system->gateway = g_strdup(gateway);
720 for (list = ipdevice->address_list; list; list = list->next) {
721 struct connman_ipaddress *ipaddress = list->data;
723 g_free(ipaddress->gateway);
724 ipaddress->gateway = g_strdup(gateway);
728 connman_info("%s {add} route %s gw %s scope %u <%s>",
729 ipdevice->ifname, dst, gateway,
730 scope, scope2str(scope));
733 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
734 const char *dst, const char *gateway)
736 struct connman_ipdevice *ipdevice;
738 DBG("index %d", index);
740 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
741 if (ipdevice == NULL)
744 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
747 if (family == AF_INET6) {
748 g_free(ipdevice->ipv6_gateway);
749 ipdevice->ipv6_gateway = NULL;
751 g_free(ipdevice->ipv4_gateway);
752 ipdevice->ipv4_gateway = NULL;
755 if (ipdevice->config != NULL &&
756 ipdevice->config->system != NULL) {
757 g_free(ipdevice->config->system->gateway);
758 ipdevice->config->system->gateway = NULL;
761 for (list = ipdevice->address_list; list; list = list->next) {
762 struct connman_ipaddress *ipaddress = list->data;
764 g_free(ipaddress->gateway);
765 ipaddress->gateway = NULL;
769 connman_info("%s {del} route %s gw %s scope %u <%s>",
770 ipdevice->ifname, dst, gateway,
771 scope, scope2str(scope));
774 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
779 keys = g_hash_table_get_keys(ipdevice_hash);
783 for (list = g_list_first(keys); list; list = g_list_next(list)) {
784 int index = GPOINTER_TO_INT(list->data);
786 function(index, user_data);
792 unsigned short __connman_ipconfig_get_type(int index)
794 struct connman_ipdevice *ipdevice;
796 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
797 if (ipdevice == NULL)
800 return ipdevice->type;
803 unsigned int __connman_ipconfig_get_flags(int index)
805 struct connman_ipdevice *ipdevice;
807 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
808 if (ipdevice == NULL)
811 return ipdevice->flags;
814 const char *__connman_ipconfig_get_gateway(int index)
816 struct connman_ipdevice *ipdevice;
818 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
819 if (ipdevice == NULL)
822 if (ipdevice->ipv4_gateway != NULL)
823 return ipdevice->ipv4_gateway;
825 if (ipdevice->config != NULL &&
826 ipdevice->config->address != NULL)
827 return ipdevice->config->address->gateway;
832 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
834 ipconfig->index = index;
836 if (ipconfig->ipv6 != NULL)
837 ipconfig->ipv6->index = index;
840 static struct connman_ipconfig *create_ipv6config(int index)
842 struct connman_ipconfig *ipv6config;
844 DBG("index %d", index);
846 ipv6config = g_try_new0(struct connman_ipconfig, 1);
847 if (ipv6config == NULL)
850 ipv6config->index = index;
851 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
853 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
854 if (ipv6config->address == NULL) {
859 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
861 ipv6config->ipv6 = NULL;
863 DBG("ipconfig %p", ipv6config);
869 * connman_ipconfig_create:
871 * Allocate a new ipconfig structure.
873 * Returns: a newly-allocated #connman_ipconfig structure
875 struct connman_ipconfig *connman_ipconfig_create(int index)
877 struct connman_ipconfig *ipconfig;
879 DBG("index %d", index);
881 ipconfig = g_try_new0(struct connman_ipconfig, 1);
882 if (ipconfig == NULL)
885 ipconfig->refcount = 1;
887 ipconfig->index = index;
888 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
890 ipconfig->address = connman_ipaddress_alloc(AF_INET);
891 if (ipconfig->address == NULL) {
896 ipconfig->system = connman_ipaddress_alloc(AF_INET);
898 ipconfig->ipv6 = create_ipv6config(index);
900 DBG("ipconfig %p", ipconfig);
906 * connman_ipconfig_clone:
908 * Clone an ipconfig structure and create new reference.
910 * Returns: a newly-allocated #connman_ipconfig structure
912 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
914 struct connman_ipconfig *ipconfig_clone;
916 DBG("ipconfig %p", ipconfig);
918 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
919 if (ipconfig_clone == NULL)
922 ipconfig_clone->refcount = 1;
924 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
926 ipconfig_clone->index = -1;
928 return ipconfig_clone;
932 * connman_ipconfig_ref:
933 * @ipconfig: ipconfig structure
935 * Increase reference counter of ipconfig
937 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
939 g_atomic_int_inc(&ipconfig->refcount);
944 static void free_ipv6config(struct connman_ipconfig *ipconfig)
946 if (ipconfig == NULL)
949 connman_ipconfig_set_ops(ipconfig, NULL);
950 connman_ipaddress_free(ipconfig->system);
951 connman_ipaddress_free(ipconfig->address);
952 g_free(ipconfig->ipv6);
956 * connman_ipconfig_unref:
957 * @ipconfig: ipconfig structure
959 * Decrease reference counter of ipconfig
961 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
963 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
964 __connman_ipconfig_disable(ipconfig);
966 connman_ipconfig_set_ops(ipconfig, NULL);
968 if (ipconfig->origin != NULL) {
969 connman_ipconfig_unref(ipconfig->origin);
970 ipconfig->origin = NULL;
973 connman_ipaddress_free(ipconfig->system);
974 connman_ipaddress_free(ipconfig->address);
975 free_ipv6config(ipconfig->ipv6);
981 * connman_ipconfig_get_data:
982 * @ipconfig: ipconfig structure
984 * Get private data pointer
986 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
988 return ipconfig->ops_data;
992 * connman_ipconfig_set_data:
993 * @ipconfig: ipconfig structure
994 * @data: data pointer
996 * Set private data pointer
998 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1000 ipconfig->ops_data = data;
1004 * connman_ipconfig_get_index:
1005 * @ipconfig: ipconfig structure
1007 * Get interface index
1009 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1011 if (ipconfig == NULL)
1014 if (ipconfig->origin != NULL)
1015 return ipconfig->origin->index;
1017 return ipconfig->index;
1021 * connman_ipconfig_get_ifname:
1022 * @ipconfig: ipconfig structure
1024 * Get interface name
1026 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1028 struct connman_ipdevice *ipdevice;
1030 if (ipconfig == NULL)
1033 if (ipconfig->index < 0)
1036 ipdevice = g_hash_table_lookup(ipdevice_hash,
1037 GINT_TO_POINTER(ipconfig->index));
1038 if (ipdevice == NULL)
1041 return ipdevice->ifname;
1045 * connman_ipconfig_set_ops:
1046 * @ipconfig: ipconfig structure
1047 * @ops: operation callbacks
1049 * Set the operation callbacks
1051 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1052 const struct connman_ipconfig_ops *ops)
1054 ipconfig->ops = ops;
1057 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1058 struct connman_ipconfig *ipconfig)
1060 if (ipconfig == NULL)
1063 return ipconfig->ipv6;
1067 * connman_ipconfig_set_method:
1068 * @ipconfig: ipconfig structure
1069 * @method: configuration method
1071 * Set the configuration method
1073 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1074 enum connman_ipconfig_method method)
1076 ipconfig->method = method;
1081 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1083 if (ipconfig == NULL)
1084 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1086 return ipconfig->method;
1090 * connman_ipconfig_bind:
1091 * @ipconfig: ipconfig structure
1092 * @ipaddress: ipaddress structure
1094 * Bind IP address details to configuration
1096 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1097 struct connman_ipaddress *ipaddress)
1099 struct connman_ipconfig *origin;
1101 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1103 connman_ipaddress_copy(origin->address, ipaddress);
1105 connman_inet_set_address(origin->index, origin->address);
1108 void __connman_ipconfig_set_element_ipv6_gateway(
1109 struct connman_ipconfig *ipconfig,
1110 struct connman_element *element)
1112 element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1116 * FIXME: The element soulution should be removed in the future
1117 * Set IPv4 and IPv6 gateway
1119 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1120 struct connman_element *parent)
1122 struct connman_element *connection;
1124 connection = connman_element_create(NULL);
1126 DBG("ipconfig %p", ipconfig);
1128 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1129 connection->index = ipconfig->index;
1130 connection->ipv4.gateway = ipconfig->address->gateway;
1131 connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1133 if (connman_element_register(connection, parent) < 0)
1134 connman_element_unref(connection);
1139 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1143 switch (ipconfig->method) {
1144 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1145 case CONNMAN_IPCONFIG_METHOD_OFF:
1146 case CONNMAN_IPCONFIG_METHOD_FIXED:
1147 case CONNMAN_IPCONFIG_METHOD_DHCP:
1149 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1150 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1151 return connman_inet_set_address(ipconfig->index,
1153 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1154 return connman_inet_set_ipv6_address(
1155 ipconfig->index, ipconfig->address);
1161 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1165 if (ipconfig == NULL)
1168 DBG("method %d", ipconfig->method);
1170 switch (ipconfig->method) {
1171 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1172 case CONNMAN_IPCONFIG_METHOD_OFF:
1173 case CONNMAN_IPCONFIG_METHOD_FIXED:
1174 case CONNMAN_IPCONFIG_METHOD_DHCP:
1176 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1177 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1178 return connman_inet_clear_address(ipconfig->index);
1179 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1180 return connman_inet_clear_ipv6_address(
1182 ipconfig->address->local,
1183 ipconfig->address->prefixlen);
1189 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1191 struct connman_ipdevice *ipdevice;
1192 gboolean up = FALSE, down = FALSE;
1193 gboolean lower_up = FALSE, lower_down = FALSE;
1195 DBG("ipconfig %p", ipconfig);
1197 if (ipconfig == NULL || ipconfig->index < 0)
1200 ipdevice = g_hash_table_lookup(ipdevice_hash,
1201 GINT_TO_POINTER(ipconfig->index));
1202 if (ipdevice == NULL)
1205 if (ipdevice->config == ipconfig)
1208 if (ipdevice->config != NULL) {
1209 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1211 connman_ipaddress_clear(ipdevice->config->system);
1213 connman_ipconfig_unref(ipdevice->config);
1216 ipdevice->config = connman_ipconfig_ref(ipconfig);
1218 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1220 if (ipdevice->flags & IFF_UP)
1225 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1226 (IFF_RUNNING | IFF_LOWER_UP))
1228 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1231 if (up == TRUE && ipconfig->ops->up)
1232 ipconfig->ops->up(ipconfig);
1233 if (lower_up == TRUE && ipconfig->ops->lower_up)
1234 ipconfig->ops->lower_up(ipconfig);
1236 if (lower_down == TRUE && ipconfig->ops->lower_down)
1237 ipconfig->ops->lower_down(ipconfig);
1238 if (down == TRUE && ipconfig->ops->down)
1239 ipconfig->ops->down(ipconfig);
1244 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1246 struct connman_ipdevice *ipdevice;
1248 DBG("ipconfig %p", ipconfig);
1250 if (ipconfig == NULL || ipconfig->index < 0)
1253 ipdevice = g_hash_table_lookup(ipdevice_hash,
1254 GINT_TO_POINTER(ipconfig->index));
1255 if (ipdevice == NULL)
1258 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1261 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1263 connman_ipaddress_clear(ipdevice->config->system);
1264 connman_ipaddress_clear(ipdevice->config->ipv6->system);
1266 connman_ipconfig_unref(ipdevice->config);
1267 ipdevice->config = NULL;
1272 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1275 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1277 case CONNMAN_IPCONFIG_METHOD_OFF:
1279 case CONNMAN_IPCONFIG_METHOD_FIXED:
1281 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1283 case CONNMAN_IPCONFIG_METHOD_DHCP:
1290 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1292 if (g_strcmp0(method, "off") == 0)
1293 return CONNMAN_IPCONFIG_METHOD_OFF;
1294 else if (g_strcmp0(method, "fixed") == 0)
1295 return CONNMAN_IPCONFIG_METHOD_FIXED;
1296 else if (g_strcmp0(method, "manual") == 0)
1297 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1298 else if (g_strcmp0(method, "dhcp") == 0)
1299 return CONNMAN_IPCONFIG_METHOD_DHCP;
1301 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1304 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1305 DBusMessageIter *iter)
1311 str = __connman_ipconfig_method2string(ipconfig->method);
1315 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1317 if (ipconfig->system == NULL)
1320 if (ipconfig->system->local != NULL) {
1322 struct in_addr netmask;
1325 connman_dbus_dict_append_basic(iter, "Address",
1326 DBUS_TYPE_STRING, &ipconfig->system->local);
1328 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1329 netmask.s_addr = htonl(addr);
1330 mask = inet_ntoa(netmask);
1331 connman_dbus_dict_append_basic(iter, "Netmask",
1332 DBUS_TYPE_STRING, &mask);
1335 if (ipconfig->system->gateway != NULL)
1336 connman_dbus_dict_append_basic(iter, "Gateway",
1337 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1340 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1341 DBusMessageIter *iter)
1347 str = __connman_ipconfig_method2string(ipconfig->method);
1351 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1353 if (ipconfig->system == NULL)
1356 if (ipconfig->system->local != NULL) {
1357 connman_dbus_dict_append_basic(iter, "Address",
1358 DBUS_TYPE_STRING, &ipconfig->system->local);
1359 connman_dbus_dict_append_basic(iter, "PrefixLength",
1361 &ipconfig->system->prefixlen);
1364 if (ipconfig->system->gateway != NULL)
1365 connman_dbus_dict_append_basic(iter, "Gateway",
1366 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1369 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1370 DBusMessageIter *iter)
1376 str = __connman_ipconfig_method2string(ipconfig->method);
1380 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1382 switch (ipconfig->method) {
1383 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1384 case CONNMAN_IPCONFIG_METHOD_OFF:
1385 case CONNMAN_IPCONFIG_METHOD_DHCP:
1387 case CONNMAN_IPCONFIG_METHOD_FIXED:
1388 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1392 if (ipconfig->address == NULL)
1395 if (ipconfig->address->local != NULL) {
1396 connman_dbus_dict_append_basic(iter, "Address",
1397 DBUS_TYPE_STRING, &ipconfig->address->local);
1398 connman_dbus_dict_append_basic(iter, "PrefixLength",
1400 &ipconfig->address->prefixlen);
1403 if (ipconfig->address->gateway != NULL)
1404 connman_dbus_dict_append_basic(iter, "Gateway",
1405 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1408 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1409 DBusMessageIter *iter)
1415 str = __connman_ipconfig_method2string(ipconfig->method);
1419 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1421 switch (ipconfig->method) {
1422 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1423 case CONNMAN_IPCONFIG_METHOD_OFF:
1424 case CONNMAN_IPCONFIG_METHOD_FIXED:
1425 case CONNMAN_IPCONFIG_METHOD_DHCP:
1427 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1431 if (ipconfig->address == NULL)
1434 if (ipconfig->address->local != NULL) {
1436 struct in_addr netmask;
1439 connman_dbus_dict_append_basic(iter, "Address",
1440 DBUS_TYPE_STRING, &ipconfig->address->local);
1442 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1443 netmask.s_addr = htonl(addr);
1444 mask = inet_ntoa(netmask);
1445 connman_dbus_dict_append_basic(iter, "Netmask",
1446 DBUS_TYPE_STRING, &mask);
1449 if (ipconfig->address->gateway != NULL)
1450 connman_dbus_dict_append_basic(iter, "Gateway",
1451 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1454 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1455 enum connman_ipconfig_type type, DBusMessageIter *array)
1457 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1458 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1459 *prefix_length_string = NULL;
1460 int prefix_length = 0;
1461 DBusMessageIter dict;
1463 DBG("ipconfig %p type %d", ipconfig, type);
1465 if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1466 type != CONNMAN_IPCONFIG_TYPE_IPV6)
1469 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1472 dbus_message_iter_recurse(array, &dict);
1474 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1475 DBusMessageIter entry;
1479 dbus_message_iter_recurse(&dict, &entry);
1481 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1484 dbus_message_iter_get_basic(&entry, &key);
1485 dbus_message_iter_next(&entry);
1487 type = dbus_message_iter_get_arg_type(&entry);
1489 if (g_str_equal(key, "Method") == TRUE) {
1492 if (type != DBUS_TYPE_STRING)
1495 dbus_message_iter_get_basic(&entry, &str);
1496 method = __connman_ipconfig_string2method(str);
1497 } else if (g_str_equal(key, "Address") == TRUE) {
1498 if (type != DBUS_TYPE_STRING)
1501 dbus_message_iter_get_basic(&entry, &address);
1502 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1503 if (type != DBUS_TYPE_STRING)
1506 dbus_message_iter_get_basic(&entry,
1507 &prefix_length_string);
1509 prefix_length = atoi(prefix_length_string);
1510 if (prefix_length < 0 || prefix_length > 128)
1513 } else if (g_str_equal(key, "Netmask") == TRUE) {
1514 if (type != DBUS_TYPE_STRING)
1517 dbus_message_iter_get_basic(&entry, &netmask);
1518 } else if (g_str_equal(key, "Gateway") == TRUE) {
1519 if (type != DBUS_TYPE_STRING)
1522 dbus_message_iter_get_basic(&entry, &gateway);
1524 dbus_message_iter_next(&dict);
1527 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1528 method, address, netmask, gateway, prefix_length);
1531 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1532 case CONNMAN_IPCONFIG_METHOD_OFF:
1533 case CONNMAN_IPCONFIG_METHOD_FIXED:
1536 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1537 if (address == NULL)
1540 ipconfig->method = method;
1542 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1543 connman_ipaddress_set_ipv4(ipconfig->address,
1544 address, netmask, gateway);
1546 return connman_ipaddress_set_ipv6(
1547 ipconfig->address, address,
1548 gateway, prefix_length);
1551 case CONNMAN_IPCONFIG_METHOD_DHCP:
1552 if (ipconfig->method == method)
1555 ipconfig->method = method;
1562 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1563 DBusMessageIter *iter)
1565 const char *method = "direct";
1567 connman_dbus_dict_append_basic(iter, "Method",
1568 DBUS_TYPE_STRING, &method);
1571 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1572 DBusMessageIter *iter)
1574 struct connman_ipdevice *ipdevice;
1575 const char *method = "auto";
1577 connman_dbus_dict_append_basic(iter, "Method",
1578 DBUS_TYPE_STRING, &method);
1580 ipdevice = g_hash_table_lookup(ipdevice_hash,
1581 GINT_TO_POINTER(ipconfig->index));
1582 if (ipdevice == NULL)
1585 if (ipdevice->ifname != NULL)
1586 connman_dbus_dict_append_basic(iter, "Interface",
1587 DBUS_TYPE_STRING, &ipdevice->ifname);
1589 if (ipdevice->address != NULL)
1590 connman_dbus_dict_append_basic(iter, "Address",
1591 DBUS_TYPE_STRING, &ipdevice->address);
1593 if (ipdevice->mtu > 0)
1594 connman_dbus_dict_append_basic(iter, "MTU",
1595 DBUS_TYPE_UINT16, &ipdevice->mtu);
1598 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1599 GKeyFile *keyfile, const char *identifier, const char *prefix)
1604 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1606 key = g_strdup_printf("%smethod", prefix);
1607 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1609 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1611 ipconfig->method = __connman_ipconfig_string2method(method);
1614 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1615 ipconfig->address->prefixlen = g_key_file_get_integer(
1616 keyfile, identifier, key, NULL);
1619 key = g_strdup_printf("%slocal_address", prefix);
1620 ipconfig->address->local = g_key_file_get_string(
1621 keyfile, identifier, key, NULL);
1624 key = g_strdup_printf("%speer_address", prefix);
1625 ipconfig->address->peer = g_key_file_get_string(
1626 keyfile, identifier, key, NULL);
1629 key = g_strdup_printf("%sbroadcast_address", prefix);
1630 ipconfig->address->broadcast = g_key_file_get_string(
1631 keyfile, identifier, key, NULL);
1634 key = g_strdup_printf("%sgateway", prefix);
1635 ipconfig->address->gateway = g_key_file_get_string(
1636 keyfile, identifier, key, NULL);
1642 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1643 GKeyFile *keyfile, const char *identifier, const char *prefix)
1648 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1650 method = __connman_ipconfig_method2string(ipconfig->method);
1652 key = g_strdup_printf("%smethod", prefix);
1653 g_key_file_set_string(keyfile, identifier, key, method);
1656 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1657 g_key_file_set_integer(keyfile, identifier,
1658 key, ipconfig->address->prefixlen);
1661 key = g_strdup_printf("%slocal_address", prefix);
1662 if (ipconfig->address->local != NULL)
1663 g_key_file_set_string(keyfile, identifier,
1664 key, ipconfig->address->local);
1667 key = g_strdup_printf("%speer_address", prefix);
1668 if (ipconfig->address->peer != NULL)
1669 g_key_file_set_string(keyfile, identifier,
1670 key, ipconfig->address->peer);
1673 key = g_strdup_printf("%sbroadcast_address", prefix);
1674 if (ipconfig->address->broadcast != NULL)
1675 g_key_file_set_string(keyfile, identifier,
1676 key, ipconfig->address->broadcast);
1679 key = g_strdup_printf("%sgateway", prefix);
1680 if (ipconfig->address->gateway != NULL)
1681 g_key_file_set_string(keyfile, identifier,
1682 key, ipconfig->address->gateway);
1688 int __connman_ipconfig_init(void)
1692 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1693 NULL, free_ipdevice);
1698 void __connman_ipconfig_cleanup(void)
1702 g_hash_table_destroy(ipdevice_hash);
1703 ipdevice_hash = NULL;