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;
852 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
854 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
855 if (ipv6config->address == NULL) {
860 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
862 ipv6config->ipv6 = NULL;
864 DBG("ipconfig %p", ipv6config);
870 * connman_ipconfig_create:
872 * Allocate a new ipconfig structure.
874 * Returns: a newly-allocated #connman_ipconfig structure
876 struct connman_ipconfig *connman_ipconfig_create(int index)
878 struct connman_ipconfig *ipconfig;
880 DBG("index %d", index);
882 ipconfig = g_try_new0(struct connman_ipconfig, 1);
883 if (ipconfig == NULL)
886 ipconfig->refcount = 1;
888 ipconfig->index = index;
889 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
891 ipconfig->address = connman_ipaddress_alloc(AF_INET);
892 if (ipconfig->address == NULL) {
897 ipconfig->system = connman_ipaddress_alloc(AF_INET);
899 ipconfig->ipv6 = create_ipv6config(index);
901 DBG("ipconfig %p", ipconfig);
907 * connman_ipconfig_clone:
909 * Clone an ipconfig structure and create new reference.
911 * Returns: a newly-allocated #connman_ipconfig structure
913 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
915 struct connman_ipconfig *ipconfig_clone;
917 DBG("ipconfig %p", ipconfig);
919 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
920 if (ipconfig_clone == NULL)
923 ipconfig_clone->refcount = 1;
925 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
927 ipconfig_clone->index = -1;
929 return ipconfig_clone;
933 * connman_ipconfig_ref:
934 * @ipconfig: ipconfig structure
936 * Increase reference counter of ipconfig
938 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
940 g_atomic_int_inc(&ipconfig->refcount);
945 static void free_ipv6config(struct connman_ipconfig *ipconfig)
947 if (ipconfig == NULL)
950 connman_ipconfig_set_ops(ipconfig, NULL);
951 connman_ipaddress_free(ipconfig->system);
952 connman_ipaddress_free(ipconfig->address);
953 g_free(ipconfig->ipv6);
957 * connman_ipconfig_unref:
958 * @ipconfig: ipconfig structure
960 * Decrease reference counter of ipconfig
962 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
964 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
965 __connman_ipconfig_disable(ipconfig);
967 connman_ipconfig_set_ops(ipconfig, NULL);
969 if (ipconfig->origin != NULL) {
970 connman_ipconfig_unref(ipconfig->origin);
971 ipconfig->origin = NULL;
974 connman_ipaddress_free(ipconfig->system);
975 connman_ipaddress_free(ipconfig->address);
976 free_ipv6config(ipconfig->ipv6);
982 * connman_ipconfig_get_data:
983 * @ipconfig: ipconfig structure
985 * Get private data pointer
987 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
989 return ipconfig->ops_data;
993 * connman_ipconfig_set_data:
994 * @ipconfig: ipconfig structure
995 * @data: data pointer
997 * Set private data pointer
999 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1001 ipconfig->ops_data = data;
1005 * connman_ipconfig_get_index:
1006 * @ipconfig: ipconfig structure
1008 * Get interface index
1010 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1012 if (ipconfig == NULL)
1015 if (ipconfig->origin != NULL)
1016 return ipconfig->origin->index;
1018 return ipconfig->index;
1022 * connman_ipconfig_get_ifname:
1023 * @ipconfig: ipconfig structure
1025 * Get interface name
1027 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1029 struct connman_ipdevice *ipdevice;
1031 if (ipconfig == NULL)
1034 if (ipconfig->index < 0)
1037 ipdevice = g_hash_table_lookup(ipdevice_hash,
1038 GINT_TO_POINTER(ipconfig->index));
1039 if (ipdevice == NULL)
1042 return ipdevice->ifname;
1046 * connman_ipconfig_set_ops:
1047 * @ipconfig: ipconfig structure
1048 * @ops: operation callbacks
1050 * Set the operation callbacks
1052 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1053 const struct connman_ipconfig_ops *ops)
1055 ipconfig->ops = ops;
1058 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1059 struct connman_ipconfig *ipconfig)
1061 if (ipconfig == NULL)
1064 return ipconfig->ipv6;
1068 * connman_ipconfig_set_method:
1069 * @ipconfig: ipconfig structure
1070 * @method: configuration method
1072 * Set the configuration method
1074 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1075 enum connman_ipconfig_method method)
1077 ipconfig->method = method;
1082 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1084 if (ipconfig == NULL)
1085 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1087 return ipconfig->method;
1091 * connman_ipconfig_bind:
1092 * @ipconfig: ipconfig structure
1093 * @ipaddress: ipaddress structure
1095 * Bind IP address details to configuration
1097 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1098 struct connman_ipaddress *ipaddress)
1100 struct connman_ipconfig *origin;
1102 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1104 connman_ipaddress_copy(origin->address, ipaddress);
1106 connman_inet_set_address(origin->index, origin->address);
1109 void __connman_ipconfig_set_element_ipv6_gateway(
1110 struct connman_ipconfig *ipconfig,
1111 struct connman_element *element)
1113 element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1117 * FIXME: The element soulution should be removed in the future
1118 * Set IPv4 and IPv6 gateway
1120 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1121 struct connman_element *parent)
1123 struct connman_element *connection;
1125 connection = connman_element_create(NULL);
1127 DBG("ipconfig %p", ipconfig);
1129 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1130 connection->index = ipconfig->index;
1131 connection->ipv4.gateway = ipconfig->address->gateway;
1132 connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1134 if (connman_element_register(connection, parent) < 0)
1135 connman_element_unref(connection);
1140 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1144 switch (ipconfig->method) {
1145 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1146 case CONNMAN_IPCONFIG_METHOD_OFF:
1147 case CONNMAN_IPCONFIG_METHOD_FIXED:
1148 case CONNMAN_IPCONFIG_METHOD_DHCP:
1150 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1151 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1152 return connman_inet_set_address(ipconfig->index,
1154 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1155 return connman_inet_set_ipv6_address(
1156 ipconfig->index, ipconfig->address);
1162 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1166 if (ipconfig == NULL)
1169 DBG("method %d", ipconfig->method);
1171 switch (ipconfig->method) {
1172 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1173 case CONNMAN_IPCONFIG_METHOD_OFF:
1174 case CONNMAN_IPCONFIG_METHOD_FIXED:
1175 case CONNMAN_IPCONFIG_METHOD_DHCP:
1177 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1178 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1179 return connman_inet_clear_address(ipconfig->index);
1180 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1181 return connman_inet_clear_ipv6_address(
1183 ipconfig->address->local,
1184 ipconfig->address->prefixlen);
1190 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1192 struct connman_ipdevice *ipdevice;
1193 gboolean up = FALSE, down = FALSE;
1194 gboolean lower_up = FALSE, lower_down = FALSE;
1196 DBG("ipconfig %p", ipconfig);
1198 if (ipconfig == NULL || ipconfig->index < 0)
1201 ipdevice = g_hash_table_lookup(ipdevice_hash,
1202 GINT_TO_POINTER(ipconfig->index));
1203 if (ipdevice == NULL)
1206 if (ipdevice->config == ipconfig)
1209 if (ipdevice->config != NULL) {
1210 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1212 connman_ipaddress_clear(ipdevice->config->system);
1214 connman_ipconfig_unref(ipdevice->config);
1217 ipdevice->config = connman_ipconfig_ref(ipconfig);
1219 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1221 if (ipdevice->flags & IFF_UP)
1226 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1227 (IFF_RUNNING | IFF_LOWER_UP))
1229 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1232 if (up == TRUE && ipconfig->ops->up)
1233 ipconfig->ops->up(ipconfig);
1234 if (lower_up == TRUE && ipconfig->ops->lower_up)
1235 ipconfig->ops->lower_up(ipconfig);
1237 if (lower_down == TRUE && ipconfig->ops->lower_down)
1238 ipconfig->ops->lower_down(ipconfig);
1239 if (down == TRUE && ipconfig->ops->down)
1240 ipconfig->ops->down(ipconfig);
1245 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1247 struct connman_ipdevice *ipdevice;
1249 DBG("ipconfig %p", ipconfig);
1251 if (ipconfig == NULL || ipconfig->index < 0)
1254 ipdevice = g_hash_table_lookup(ipdevice_hash,
1255 GINT_TO_POINTER(ipconfig->index));
1256 if (ipdevice == NULL)
1259 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1262 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1264 connman_ipaddress_clear(ipdevice->config->system);
1265 connman_ipaddress_clear(ipdevice->config->ipv6->system);
1267 connman_ipconfig_unref(ipdevice->config);
1268 ipdevice->config = NULL;
1273 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1276 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1278 case CONNMAN_IPCONFIG_METHOD_OFF:
1280 case CONNMAN_IPCONFIG_METHOD_FIXED:
1282 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1284 case CONNMAN_IPCONFIG_METHOD_DHCP:
1291 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1293 if (g_strcmp0(method, "off") == 0)
1294 return CONNMAN_IPCONFIG_METHOD_OFF;
1295 else if (g_strcmp0(method, "fixed") == 0)
1296 return CONNMAN_IPCONFIG_METHOD_FIXED;
1297 else if (g_strcmp0(method, "manual") == 0)
1298 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1299 else if (g_strcmp0(method, "dhcp") == 0)
1300 return CONNMAN_IPCONFIG_METHOD_DHCP;
1302 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1305 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1306 DBusMessageIter *iter)
1312 str = __connman_ipconfig_method2string(ipconfig->method);
1316 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1318 if (ipconfig->system == NULL)
1321 if (ipconfig->system->local != NULL) {
1323 struct in_addr netmask;
1326 connman_dbus_dict_append_basic(iter, "Address",
1327 DBUS_TYPE_STRING, &ipconfig->system->local);
1329 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1330 netmask.s_addr = htonl(addr);
1331 mask = inet_ntoa(netmask);
1332 connman_dbus_dict_append_basic(iter, "Netmask",
1333 DBUS_TYPE_STRING, &mask);
1336 if (ipconfig->system->gateway != NULL)
1337 connman_dbus_dict_append_basic(iter, "Gateway",
1338 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1341 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1342 DBusMessageIter *iter)
1348 str = __connman_ipconfig_method2string(ipconfig->method);
1352 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1354 if (ipconfig->system == NULL)
1357 if (ipconfig->system->local != NULL) {
1358 connman_dbus_dict_append_basic(iter, "Address",
1359 DBUS_TYPE_STRING, &ipconfig->system->local);
1360 connman_dbus_dict_append_basic(iter, "PrefixLength",
1362 &ipconfig->system->prefixlen);
1365 if (ipconfig->system->gateway != NULL)
1366 connman_dbus_dict_append_basic(iter, "Gateway",
1367 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1370 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1371 DBusMessageIter *iter)
1377 str = __connman_ipconfig_method2string(ipconfig->method);
1381 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1383 switch (ipconfig->method) {
1384 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1385 case CONNMAN_IPCONFIG_METHOD_OFF:
1386 case CONNMAN_IPCONFIG_METHOD_DHCP:
1388 case CONNMAN_IPCONFIG_METHOD_FIXED:
1389 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1393 if (ipconfig->address == NULL)
1396 if (ipconfig->address->local != NULL) {
1397 connman_dbus_dict_append_basic(iter, "Address",
1398 DBUS_TYPE_STRING, &ipconfig->address->local);
1399 connman_dbus_dict_append_basic(iter, "PrefixLength",
1401 &ipconfig->address->prefixlen);
1404 if (ipconfig->address->gateway != NULL)
1405 connman_dbus_dict_append_basic(iter, "Gateway",
1406 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1409 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1410 DBusMessageIter *iter)
1416 str = __connman_ipconfig_method2string(ipconfig->method);
1420 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1422 switch (ipconfig->method) {
1423 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1424 case CONNMAN_IPCONFIG_METHOD_OFF:
1425 case CONNMAN_IPCONFIG_METHOD_FIXED:
1426 case CONNMAN_IPCONFIG_METHOD_DHCP:
1428 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1432 if (ipconfig->address == NULL)
1435 if (ipconfig->address->local != NULL) {
1437 struct in_addr netmask;
1440 connman_dbus_dict_append_basic(iter, "Address",
1441 DBUS_TYPE_STRING, &ipconfig->address->local);
1443 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1444 netmask.s_addr = htonl(addr);
1445 mask = inet_ntoa(netmask);
1446 connman_dbus_dict_append_basic(iter, "Netmask",
1447 DBUS_TYPE_STRING, &mask);
1450 if (ipconfig->address->gateway != NULL)
1451 connman_dbus_dict_append_basic(iter, "Gateway",
1452 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1455 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1456 enum connman_ipconfig_type type, DBusMessageIter *array)
1458 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1459 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1460 *prefix_length_string = NULL;
1461 int prefix_length = 0;
1462 DBusMessageIter dict;
1464 DBG("ipconfig %p type %d", ipconfig, type);
1466 if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1467 type != CONNMAN_IPCONFIG_TYPE_IPV6)
1470 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1473 dbus_message_iter_recurse(array, &dict);
1475 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1476 DBusMessageIter entry;
1480 dbus_message_iter_recurse(&dict, &entry);
1482 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1485 dbus_message_iter_get_basic(&entry, &key);
1486 dbus_message_iter_next(&entry);
1488 type = dbus_message_iter_get_arg_type(&entry);
1490 if (g_str_equal(key, "Method") == TRUE) {
1493 if (type != DBUS_TYPE_STRING)
1496 dbus_message_iter_get_basic(&entry, &str);
1497 method = __connman_ipconfig_string2method(str);
1498 } else if (g_str_equal(key, "Address") == TRUE) {
1499 if (type != DBUS_TYPE_STRING)
1502 dbus_message_iter_get_basic(&entry, &address);
1503 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1504 if (type != DBUS_TYPE_STRING)
1507 dbus_message_iter_get_basic(&entry,
1508 &prefix_length_string);
1510 prefix_length = atoi(prefix_length_string);
1511 if (prefix_length < 0 || prefix_length > 128)
1514 } else if (g_str_equal(key, "Netmask") == TRUE) {
1515 if (type != DBUS_TYPE_STRING)
1518 dbus_message_iter_get_basic(&entry, &netmask);
1519 } else if (g_str_equal(key, "Gateway") == TRUE) {
1520 if (type != DBUS_TYPE_STRING)
1523 dbus_message_iter_get_basic(&entry, &gateway);
1525 dbus_message_iter_next(&dict);
1528 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1529 method, address, netmask, gateway, prefix_length);
1532 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1533 case CONNMAN_IPCONFIG_METHOD_OFF:
1534 case CONNMAN_IPCONFIG_METHOD_FIXED:
1537 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1538 if (address == NULL)
1541 ipconfig->method = method;
1543 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1544 connman_ipaddress_set_ipv4(ipconfig->address,
1545 address, netmask, gateway);
1547 return connman_ipaddress_set_ipv6(
1548 ipconfig->address, address,
1549 gateway, prefix_length);
1552 case CONNMAN_IPCONFIG_METHOD_DHCP:
1553 if (ipconfig->method == method)
1556 ipconfig->method = method;
1563 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1564 DBusMessageIter *iter)
1566 const char *method = "direct";
1568 connman_dbus_dict_append_basic(iter, "Method",
1569 DBUS_TYPE_STRING, &method);
1572 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1573 DBusMessageIter *iter)
1575 struct connman_ipdevice *ipdevice;
1576 const char *method = "auto";
1578 connman_dbus_dict_append_basic(iter, "Method",
1579 DBUS_TYPE_STRING, &method);
1581 ipdevice = g_hash_table_lookup(ipdevice_hash,
1582 GINT_TO_POINTER(ipconfig->index));
1583 if (ipdevice == NULL)
1586 if (ipdevice->ifname != NULL)
1587 connman_dbus_dict_append_basic(iter, "Interface",
1588 DBUS_TYPE_STRING, &ipdevice->ifname);
1590 if (ipdevice->address != NULL)
1591 connman_dbus_dict_append_basic(iter, "Address",
1592 DBUS_TYPE_STRING, &ipdevice->address);
1594 if (ipdevice->mtu > 0)
1595 connman_dbus_dict_append_basic(iter, "MTU",
1596 DBUS_TYPE_UINT16, &ipdevice->mtu);
1599 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1600 GKeyFile *keyfile, const char *identifier, const char *prefix)
1605 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1607 key = g_strdup_printf("%smethod", prefix);
1608 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1610 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1612 ipconfig->method = __connman_ipconfig_string2method(method);
1615 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1616 ipconfig->address->prefixlen = g_key_file_get_integer(
1617 keyfile, identifier, key, NULL);
1620 key = g_strdup_printf("%slocal_address", prefix);
1621 ipconfig->address->local = g_key_file_get_string(
1622 keyfile, identifier, key, NULL);
1625 key = g_strdup_printf("%speer_address", prefix);
1626 ipconfig->address->peer = g_key_file_get_string(
1627 keyfile, identifier, key, NULL);
1630 key = g_strdup_printf("%sbroadcast_address", prefix);
1631 ipconfig->address->broadcast = g_key_file_get_string(
1632 keyfile, identifier, key, NULL);
1635 key = g_strdup_printf("%sgateway", prefix);
1636 ipconfig->address->gateway = g_key_file_get_string(
1637 keyfile, identifier, key, NULL);
1643 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1644 GKeyFile *keyfile, const char *identifier, const char *prefix)
1649 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1651 method = __connman_ipconfig_method2string(ipconfig->method);
1653 key = g_strdup_printf("%smethod", prefix);
1654 g_key_file_set_string(keyfile, identifier, key, method);
1657 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1658 g_key_file_set_integer(keyfile, identifier,
1659 key, ipconfig->address->prefixlen);
1662 key = g_strdup_printf("%slocal_address", prefix);
1663 if (ipconfig->address->local != NULL)
1664 g_key_file_set_string(keyfile, identifier,
1665 key, ipconfig->address->local);
1668 key = g_strdup_printf("%speer_address", prefix);
1669 if (ipconfig->address->peer != NULL)
1670 g_key_file_set_string(keyfile, identifier,
1671 key, ipconfig->address->peer);
1674 key = g_strdup_printf("%sbroadcast_address", prefix);
1675 if (ipconfig->address->broadcast != NULL)
1676 g_key_file_set_string(keyfile, identifier,
1677 key, ipconfig->address->broadcast);
1680 key = g_strdup_printf("%sgateway", prefix);
1681 if (ipconfig->address->gateway != NULL)
1682 g_key_file_set_string(keyfile, identifier,
1683 key, ipconfig->address->gateway);
1689 int __connman_ipconfig_init(void)
1693 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1694 NULL, free_ipdevice);
1699 void __connman_ipconfig_cleanup(void)
1703 g_hash_table_destroy(ipdevice_hash);
1704 ipdevice_hash = NULL;