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 {
78 struct connman_ipconfig *config;
80 struct connman_ipconfig_driver *driver;
81 struct connman_ipconfig *driver_config;
84 static GHashTable *ipdevice_hash = NULL;
85 static GList *ipconfig_list = NULL;
87 struct connman_ipaddress *connman_ipaddress_alloc(int family)
89 struct connman_ipaddress *ipaddress;
91 ipaddress = g_try_new0(struct connman_ipaddress, 1);
92 if (ipaddress == NULL)
95 ipaddress->family = family;
96 ipaddress->prefixlen = 0;
97 ipaddress->local = NULL;
98 ipaddress->peer = NULL;
99 ipaddress->broadcast = NULL;
100 ipaddress->gateway = NULL;
105 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
107 if (ipaddress == NULL)
110 g_free(ipaddress->broadcast);
111 g_free(ipaddress->peer);
112 g_free(ipaddress->local);
113 g_free(ipaddress->gateway);
117 static unsigned char netmask2prefixlen(const char *netmask)
119 unsigned char bits = 0;
120 in_addr_t mask = inet_network(netmask);
121 in_addr_t host = ~mask;
123 /* a valid netmask must be 2^n - 1 */
124 if ((host & (host + 1)) != 0)
127 for (; mask; mask <<= 1)
133 static gboolean check_ipv6_address(const char *address)
135 unsigned char buf[sizeof(struct in6_addr)];
138 err = inet_pton(AF_INET6, address, buf);
145 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
146 const char *address, const char *gateway,
147 unsigned char prefix_length)
149 if (ipaddress == NULL)
152 if (check_ipv6_address(address) == FALSE)
155 if (check_ipv6_address(gateway) == FALSE)
158 DBG("prefix_len %d address %s gateway %s",
159 prefix_length, address, gateway);
161 ipaddress->prefixlen = prefix_length;
163 g_free(ipaddress->local);
164 ipaddress->local = g_strdup(address);
166 g_free(ipaddress->gateway);
167 ipaddress->gateway = g_strdup(gateway);
172 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
173 const char *address, const char *netmask, const char *gateway)
175 if (ipaddress == NULL)
179 ipaddress->prefixlen = netmask2prefixlen(netmask);
181 ipaddress->prefixlen = 32;
183 g_free(ipaddress->local);
184 ipaddress->local = g_strdup(address);
186 g_free(ipaddress->gateway);
187 ipaddress->gateway = g_strdup(gateway);
190 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
192 if (ipaddress == NULL)
195 ipaddress->prefixlen = 0;
197 g_free(ipaddress->local);
198 ipaddress->local = NULL;
200 g_free(ipaddress->peer);
201 ipaddress->peer = NULL;
203 g_free(ipaddress->broadcast);
204 ipaddress->broadcast = NULL;
206 g_free(ipaddress->gateway);
207 ipaddress->gateway = NULL;
210 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
211 struct connman_ipaddress *source)
213 if (ipaddress == NULL || source == NULL)
216 ipaddress->family = source->family;
217 ipaddress->prefixlen = source->prefixlen;
219 g_free(ipaddress->local);
220 ipaddress->local = g_strdup(source->local);
222 g_free(ipaddress->peer);
223 ipaddress->peer = g_strdup(source->peer);
225 g_free(ipaddress->broadcast);
226 ipaddress->broadcast = g_strdup(source->broadcast);
228 g_free(ipaddress->gateway);
229 ipaddress->gateway = g_strdup(source->gateway);
232 static void free_address_list(struct connman_ipdevice *ipdevice)
236 for (list = ipdevice->address_list; list; list = list->next) {
237 struct connman_ipaddress *ipaddress = list->data;
239 connman_ipaddress_free(ipaddress);
243 g_slist_free(ipdevice->address_list);
244 ipdevice->address_list = NULL;
247 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
248 unsigned char prefixlen, const char *local)
252 for (list = ipdevice->address_list; list; list = list->next) {
253 struct connman_ipaddress *ipaddress = list->data;
255 if (g_strcmp0(ipaddress->local, local) == 0 &&
256 ipaddress->prefixlen == prefixlen)
263 static const char *type2str(unsigned short type)
268 case ARPHRD_LOOPBACK:
281 static const char *scope2str(unsigned char scope)
293 static void free_ipdevice(gpointer data)
295 struct connman_ipdevice *ipdevice = data;
297 connman_info("%s {remove} index %d", ipdevice->ifname,
300 if (ipdevice->config != NULL)
301 connman_ipconfig_unref(ipdevice->config);
303 free_address_list(ipdevice);
304 g_free(ipdevice->ipv4_gateway);
305 g_free(ipdevice->ipv6_gateway);
306 g_free(ipdevice->pac);
308 g_free(ipdevice->address);
309 g_free(ipdevice->ifname);
313 static GSList *driver_list = NULL;
315 static gint compare_priority(gconstpointer a, gconstpointer b)
317 const struct connman_ipconfig_driver *driver1 = a;
318 const struct connman_ipconfig_driver *driver2 = b;
320 return driver2->priority - driver1->priority;
324 * connman_ipconfig_driver_register:
325 * @driver: IP configuration driver
327 * Register a new IP configuration driver
329 * Returns: %0 on success
331 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
333 DBG("driver %p name %s", driver, driver->name);
335 driver_list = g_slist_insert_sorted(driver_list, driver,
342 * connman_ipconfig_driver_unregister:
343 * @driver: IP configuration driver
345 * Remove a previously registered IP configuration driver.
347 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
349 DBG("driver %p name %s", driver, driver->name);
351 driver_list = g_slist_remove(driver_list, driver);
354 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
358 DBG("ipconfig %p", ipdevice->config);
360 if (ipdevice->config == NULL)
363 switch (ipdevice->config->method) {
364 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
365 case CONNMAN_IPCONFIG_METHOD_OFF:
366 case CONNMAN_IPCONFIG_METHOD_FIXED:
367 case CONNMAN_IPCONFIG_METHOD_MANUAL:
369 case CONNMAN_IPCONFIG_METHOD_DHCP:
373 if (ipdevice->driver != NULL)
376 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
377 if (ipdevice->driver_config == NULL)
380 for (list = driver_list; list; list = list->next) {
381 struct connman_ipconfig_driver *driver = list->data;
383 if (driver->request(ipdevice->driver_config) == 0) {
384 ipdevice->driver = driver;
389 if (ipdevice->driver == NULL) {
390 connman_ipconfig_unref(ipdevice->driver_config);
391 ipdevice->driver_config = NULL;
395 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
397 DBG("ipconfig %p", ipdevice->config);
399 if (ipdevice->config == NULL)
402 if (ipdevice->driver == NULL)
405 ipdevice->driver->release(ipdevice->driver_config);
407 ipdevice->driver = NULL;
409 connman_ipconfig_unref(ipdevice->driver_config);
410 ipdevice->driver_config = NULL;
412 connman_inet_clear_address(ipdevice->index);
413 connman_inet_clear_ipv6_address(ipdevice->index,
414 ipdevice->driver_config->address->local,
415 ipdevice->driver_config->address->prefixlen);
418 static void update_stats(struct connman_ipdevice *ipdevice,
419 struct rtnl_link_stats *stats)
421 if (stats->rx_packets == 0 && stats->tx_packets == 0)
424 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
425 stats->rx_packets, stats->rx_bytes);
426 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
427 stats->tx_packets, stats->tx_bytes);
429 if (ipdevice->config == NULL)
432 ipdevice->rx_packets = stats->rx_packets;
433 ipdevice->tx_packets = stats->tx_packets;
434 ipdevice->rx_bytes = stats->rx_bytes;
435 ipdevice->tx_bytes = stats->tx_bytes;
436 ipdevice->rx_errors = stats->rx_errors;
437 ipdevice->tx_errors = stats->tx_errors;
438 ipdevice->rx_dropped = stats->rx_dropped;
439 ipdevice->tx_dropped = stats->tx_dropped;
441 __connman_counter_notify(ipdevice->config,
442 ipdevice->rx_packets, ipdevice->tx_packets,
443 ipdevice->rx_bytes, ipdevice->tx_bytes,
444 ipdevice->rx_errors, ipdevice->tx_errors,
445 ipdevice->rx_dropped, ipdevice->tx_dropped);
448 void __connman_ipconfig_newlink(int index, unsigned short type,
449 unsigned int flags, const char *address,
451 struct rtnl_link_stats *stats)
453 struct connman_ipdevice *ipdevice;
456 gboolean up = FALSE, down = FALSE;
457 gboolean lower_up = FALSE, lower_down = FALSE;
459 DBG("index %d", index);
461 if (type == ARPHRD_LOOPBACK)
464 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
465 if (ipdevice != NULL)
468 ipdevice = g_try_new0(struct connman_ipdevice, 1);
469 if (ipdevice == NULL)
472 ipdevice->index = index;
473 ipdevice->ifname = connman_inet_ifname(index);
474 ipdevice->type = type;
476 ipdevice->address = g_strdup(address);
478 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
480 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
481 index, type, type2str(type));
486 update_stats(ipdevice, stats);
488 if (flags == ipdevice->flags)
491 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
498 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
499 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
500 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
501 (IFF_RUNNING | IFF_LOWER_UP))
503 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
507 ipdevice->flags = flags;
509 str = g_string_new(NULL);
514 g_string_append(str, "UP");
516 g_string_append(str, "DOWN");
518 if (flags & IFF_RUNNING)
519 g_string_append(str, ",RUNNING");
521 if (flags & IFF_LOWER_UP)
522 g_string_append(str, ",LOWER_UP");
524 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
527 g_string_free(str, TRUE);
529 for (list = g_list_first(ipconfig_list); list;
530 list = g_list_next(list)) {
531 struct connman_ipconfig *ipconfig = list->data;
533 if (index != ipconfig->index)
536 if (ipconfig->ops == NULL)
539 if (up == TRUE && ipconfig->ops->up)
540 ipconfig->ops->up(ipconfig);
541 if (lower_up == TRUE && ipconfig->ops->lower_up)
542 ipconfig->ops->lower_up(ipconfig);
544 if (lower_down == TRUE && ipconfig->ops->lower_down)
545 ipconfig->ops->lower_down(ipconfig);
546 if (down == TRUE && ipconfig->ops->down)
547 ipconfig->ops->down(ipconfig);
551 __connman_ipconfig_lower_up(ipdevice);
553 __connman_ipconfig_lower_down(ipdevice);
556 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
558 struct connman_ipdevice *ipdevice;
561 DBG("index %d", index);
563 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
564 if (ipdevice == NULL)
567 update_stats(ipdevice, stats);
569 for (list = g_list_first(ipconfig_list); list;
570 list = g_list_next(list)) {
571 struct connman_ipconfig *ipconfig = list->data;
573 if (index != ipconfig->index)
576 ipconfig->index = -1;
578 if (ipconfig->ops == NULL)
581 if (ipconfig->ops->lower_down)
582 ipconfig->ops->lower_down(ipconfig);
583 if (ipconfig->ops->down)
584 ipconfig->ops->down(ipconfig);
587 __connman_ipconfig_lower_down(ipdevice);
589 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
592 void __connman_ipconfig_newaddr(int index, int family, const char *label,
593 unsigned char prefixlen, const char *address)
595 struct connman_ipdevice *ipdevice;
596 struct connman_ipaddress *ipaddress;
599 DBG("index %d", index);
601 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
602 if (ipdevice == NULL)
605 ipaddress = connman_ipaddress_alloc(family);
606 if (ipaddress == NULL)
609 ipaddress->prefixlen = prefixlen;
610 ipaddress->local = g_strdup(address);
612 ipdevice->address_list = g_slist_append(ipdevice->address_list,
615 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
616 address, prefixlen, label);
618 if (ipdevice->config != NULL) {
619 if (family == AF_INET6 && ipdevice->config->ipv6 != NULL)
620 connman_ipaddress_copy(ipdevice->config->ipv6->system,
623 connman_ipaddress_copy(ipdevice->config->system,
627 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
630 if (g_slist_length(ipdevice->address_list) > 1)
633 for (list = g_list_first(ipconfig_list); list;
634 list = g_list_next(list)) {
635 struct connman_ipconfig *ipconfig = list->data;
637 if (index != ipconfig->index)
640 if (ipconfig->ops == NULL)
643 if (ipconfig->ops->ip_bound)
644 ipconfig->ops->ip_bound(ipconfig);
648 void __connman_ipconfig_deladdr(int index, int family, const char *label,
649 unsigned char prefixlen, const char *address)
651 struct connman_ipdevice *ipdevice;
652 struct connman_ipaddress *ipaddress;
655 DBG("index %d", index);
657 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
658 if (ipdevice == NULL)
661 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
662 if (ipaddress == NULL)
665 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
668 connman_ipaddress_free(ipaddress);
670 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
671 address, prefixlen, label);
673 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
676 if (g_slist_length(ipdevice->address_list) > 0)
679 for (list = g_list_first(ipconfig_list); list;
680 list = g_list_next(list)) {
681 struct connman_ipconfig *ipconfig = list->data;
683 if (index != ipconfig->index)
686 if (ipconfig->ops == NULL)
689 if (ipconfig->ops->ip_release)
690 ipconfig->ops->ip_release(ipconfig);
694 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
695 const char *dst, const char *gateway)
697 struct connman_ipdevice *ipdevice;
699 DBG("index %d", index);
701 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
702 if (ipdevice == NULL)
705 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
708 if (family == AF_INET6) {
709 g_free(ipdevice->ipv6_gateway);
710 ipdevice->ipv6_gateway = g_strdup(gateway);
712 g_free(ipdevice->ipv4_gateway);
713 ipdevice->ipv4_gateway = g_strdup(gateway);
716 if (ipdevice->config != NULL &&
717 ipdevice->config->system != NULL) {
718 g_free(ipdevice->config->system->gateway);
719 ipdevice->config->system->gateway = g_strdup(gateway);
722 for (list = ipdevice->address_list; list; list = list->next) {
723 struct connman_ipaddress *ipaddress = list->data;
725 g_free(ipaddress->gateway);
726 ipaddress->gateway = g_strdup(gateway);
730 connman_info("%s {add} route %s gw %s scope %u <%s>",
731 ipdevice->ifname, dst, gateway,
732 scope, scope2str(scope));
735 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
736 const char *dst, const char *gateway)
738 struct connman_ipdevice *ipdevice;
740 DBG("index %d", index);
742 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
743 if (ipdevice == NULL)
746 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
749 if (family == AF_INET6) {
750 g_free(ipdevice->ipv6_gateway);
751 ipdevice->ipv6_gateway = NULL;
753 g_free(ipdevice->ipv4_gateway);
754 ipdevice->ipv4_gateway = NULL;
757 if (ipdevice->config != NULL &&
758 ipdevice->config->system != NULL) {
759 g_free(ipdevice->config->system->gateway);
760 ipdevice->config->system->gateway = NULL;
763 for (list = ipdevice->address_list; list; list = list->next) {
764 struct connman_ipaddress *ipaddress = list->data;
766 g_free(ipaddress->gateway);
767 ipaddress->gateway = NULL;
771 connman_info("%s {del} route %s gw %s scope %u <%s>",
772 ipdevice->ifname, dst, gateway,
773 scope, scope2str(scope));
776 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
781 keys = g_hash_table_get_keys(ipdevice_hash);
785 for (list = g_list_first(keys); list; list = g_list_next(list)) {
786 int index = GPOINTER_TO_INT(list->data);
788 function(index, user_data);
794 unsigned short __connman_ipconfig_get_type(int index)
796 struct connman_ipdevice *ipdevice;
798 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
799 if (ipdevice == NULL)
802 return ipdevice->type;
805 unsigned int __connman_ipconfig_get_flags(int index)
807 struct connman_ipdevice *ipdevice;
809 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
810 if (ipdevice == NULL)
813 return ipdevice->flags;
816 const char *__connman_ipconfig_get_gateway(int index)
818 struct connman_ipdevice *ipdevice;
820 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
821 if (ipdevice == NULL)
824 if (ipdevice->ipv4_gateway != NULL)
825 return ipdevice->ipv4_gateway;
827 if (ipdevice->config != NULL &&
828 ipdevice->config->address != NULL)
829 return ipdevice->config->address->gateway;
834 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
836 ipconfig->index = index;
838 if (ipconfig->ipv6 != NULL)
839 ipconfig->ipv6->index = index;
842 static struct connman_ipconfig *create_ipv6config(int index)
844 struct connman_ipconfig *ipv6config;
846 DBG("index %d", index);
848 ipv6config = g_try_new0(struct connman_ipconfig, 1);
849 if (ipv6config == NULL)
852 ipv6config->index = index;
853 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
854 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
856 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
857 if (ipv6config->address == NULL) {
862 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
864 ipv6config->ipv6 = NULL;
866 DBG("ipconfig %p", ipv6config);
872 * connman_ipconfig_create:
874 * Allocate a new ipconfig structure.
876 * Returns: a newly-allocated #connman_ipconfig structure
878 struct connman_ipconfig *connman_ipconfig_create(int index)
880 struct connman_ipconfig *ipconfig;
882 DBG("index %d", index);
884 ipconfig = g_try_new0(struct connman_ipconfig, 1);
885 if (ipconfig == NULL)
888 ipconfig->refcount = 1;
890 ipconfig->index = index;
891 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
893 ipconfig->address = connman_ipaddress_alloc(AF_INET);
894 if (ipconfig->address == NULL) {
899 ipconfig->system = connman_ipaddress_alloc(AF_INET);
901 ipconfig->ipv6 = create_ipv6config(index);
903 DBG("ipconfig %p", ipconfig);
909 * connman_ipconfig_clone:
911 * Clone an ipconfig structure and create new reference.
913 * Returns: a newly-allocated #connman_ipconfig structure
915 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
917 struct connman_ipconfig *ipconfig_clone;
919 DBG("ipconfig %p", ipconfig);
921 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
922 if (ipconfig_clone == NULL)
925 ipconfig_clone->refcount = 1;
927 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
929 ipconfig_clone->index = -1;
931 return ipconfig_clone;
935 * connman_ipconfig_ref:
936 * @ipconfig: ipconfig structure
938 * Increase reference counter of ipconfig
940 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
942 g_atomic_int_inc(&ipconfig->refcount);
947 static void free_ipv6config(struct connman_ipconfig *ipconfig)
949 if (ipconfig == NULL)
952 connman_ipconfig_set_ops(ipconfig, NULL);
953 connman_ipaddress_free(ipconfig->system);
954 connman_ipaddress_free(ipconfig->address);
955 g_free(ipconfig->ipv6);
959 * connman_ipconfig_unref:
960 * @ipconfig: ipconfig structure
962 * Decrease reference counter of ipconfig
964 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
966 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
967 __connman_ipconfig_disable(ipconfig);
969 connman_ipconfig_set_ops(ipconfig, NULL);
971 if (ipconfig->origin != NULL) {
972 connman_ipconfig_unref(ipconfig->origin);
973 ipconfig->origin = NULL;
976 connman_ipaddress_free(ipconfig->system);
977 connman_ipaddress_free(ipconfig->address);
978 free_ipv6config(ipconfig->ipv6);
984 * connman_ipconfig_get_data:
985 * @ipconfig: ipconfig structure
987 * Get private data pointer
989 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
991 return ipconfig->ops_data;
995 * connman_ipconfig_set_data:
996 * @ipconfig: ipconfig structure
997 * @data: data pointer
999 * Set private data pointer
1001 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1003 ipconfig->ops_data = data;
1007 * connman_ipconfig_get_index:
1008 * @ipconfig: ipconfig structure
1010 * Get interface index
1012 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1014 if (ipconfig == NULL)
1017 if (ipconfig->origin != NULL)
1018 return ipconfig->origin->index;
1020 return ipconfig->index;
1024 * connman_ipconfig_get_ifname:
1025 * @ipconfig: ipconfig structure
1027 * Get interface name
1029 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1031 struct connman_ipdevice *ipdevice;
1033 if (ipconfig == NULL)
1036 if (ipconfig->index < 0)
1039 ipdevice = g_hash_table_lookup(ipdevice_hash,
1040 GINT_TO_POINTER(ipconfig->index));
1041 if (ipdevice == NULL)
1044 return ipdevice->ifname;
1048 * connman_ipconfig_set_ops:
1049 * @ipconfig: ipconfig structure
1050 * @ops: operation callbacks
1052 * Set the operation callbacks
1054 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1055 const struct connman_ipconfig_ops *ops)
1057 ipconfig->ops = ops;
1060 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1061 struct connman_ipconfig *ipconfig)
1063 if (ipconfig == NULL)
1066 return ipconfig->ipv6;
1070 * connman_ipconfig_set_method:
1071 * @ipconfig: ipconfig structure
1072 * @method: configuration method
1074 * Set the configuration method
1076 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1077 enum connman_ipconfig_method method)
1079 ipconfig->method = method;
1084 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1086 if (ipconfig == NULL)
1087 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1089 return ipconfig->method;
1093 * connman_ipconfig_bind:
1094 * @ipconfig: ipconfig structure
1095 * @ipaddress: ipaddress structure
1097 * Bind IP address details to configuration
1099 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1100 struct connman_ipaddress *ipaddress)
1102 struct connman_ipconfig *origin;
1104 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1106 connman_ipaddress_copy(origin->address, ipaddress);
1108 connman_inet_set_address(origin->index, origin->address);
1111 void __connman_ipconfig_set_element_ipv6_gateway(
1112 struct connman_ipconfig *ipconfig,
1113 struct connman_element *element)
1115 element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1119 * FIXME: The element soulution should be removed in the future
1120 * Set IPv4 and IPv6 gateway
1122 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1123 struct connman_element *parent)
1125 struct connman_element *connection;
1127 connection = connman_element_create(NULL);
1129 DBG("ipconfig %p", ipconfig);
1131 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1132 connection->index = ipconfig->index;
1133 connection->ipv4.gateway = ipconfig->address->gateway;
1134 connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1136 if (connman_element_register(connection, parent) < 0)
1137 connman_element_unref(connection);
1142 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1146 switch (ipconfig->method) {
1147 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1148 case CONNMAN_IPCONFIG_METHOD_OFF:
1149 case CONNMAN_IPCONFIG_METHOD_FIXED:
1150 case CONNMAN_IPCONFIG_METHOD_DHCP:
1152 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1153 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1154 return connman_inet_set_address(ipconfig->index,
1156 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1157 return connman_inet_set_ipv6_address(
1158 ipconfig->index, ipconfig->address);
1164 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1168 if (ipconfig == NULL)
1171 DBG("method %d", ipconfig->method);
1173 switch (ipconfig->method) {
1174 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1175 case CONNMAN_IPCONFIG_METHOD_OFF:
1176 case CONNMAN_IPCONFIG_METHOD_FIXED:
1177 case CONNMAN_IPCONFIG_METHOD_DHCP:
1179 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1180 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1181 return connman_inet_clear_address(ipconfig->index);
1182 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1183 return connman_inet_clear_ipv6_address(
1185 ipconfig->address->local,
1186 ipconfig->address->prefixlen);
1192 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1195 struct connman_ipdevice *ipdevice;
1197 DBG("ipconfig %p", ipconfig);
1199 if (ipconfig == NULL || ipconfig->index < 0)
1202 ipdevice = g_hash_table_lookup(ipdevice_hash,
1203 GINT_TO_POINTER(ipconfig->index));
1204 if (ipdevice == NULL)
1207 g_free(ipdevice->pac);
1208 ipdevice->pac = g_strdup(url);
1213 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1215 struct connman_ipdevice *ipdevice;
1217 DBG("ipconfig %p", ipconfig);
1219 if (ipconfig == NULL || ipconfig->index < 0)
1222 ipdevice = g_hash_table_lookup(ipdevice_hash,
1223 GINT_TO_POINTER(ipconfig->index));
1224 if (ipdevice == NULL)
1227 return ipdevice->pac;
1230 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1232 struct connman_ipdevice *ipdevice;
1233 gboolean up = FALSE, down = FALSE;
1234 gboolean lower_up = FALSE, lower_down = FALSE;
1236 DBG("ipconfig %p", ipconfig);
1238 if (ipconfig == NULL || ipconfig->index < 0)
1241 ipdevice = g_hash_table_lookup(ipdevice_hash,
1242 GINT_TO_POINTER(ipconfig->index));
1243 if (ipdevice == NULL)
1246 if (ipdevice->config == ipconfig)
1249 if (ipdevice->config != NULL) {
1250 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1252 connman_ipaddress_clear(ipdevice->config->system);
1254 connman_ipconfig_unref(ipdevice->config);
1257 ipdevice->config = connman_ipconfig_ref(ipconfig);
1259 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1261 if (ipdevice->flags & IFF_UP)
1266 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1267 (IFF_RUNNING | IFF_LOWER_UP))
1269 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1272 if (up == TRUE && ipconfig->ops->up)
1273 ipconfig->ops->up(ipconfig);
1274 if (lower_up == TRUE && ipconfig->ops->lower_up)
1275 ipconfig->ops->lower_up(ipconfig);
1277 if (lower_down == TRUE && ipconfig->ops->lower_down)
1278 ipconfig->ops->lower_down(ipconfig);
1279 if (down == TRUE && ipconfig->ops->down)
1280 ipconfig->ops->down(ipconfig);
1285 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1287 struct connman_ipdevice *ipdevice;
1289 DBG("ipconfig %p", ipconfig);
1291 if (ipconfig == NULL || ipconfig->index < 0)
1294 ipdevice = g_hash_table_lookup(ipdevice_hash,
1295 GINT_TO_POINTER(ipconfig->index));
1296 if (ipdevice == NULL)
1299 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1302 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1304 connman_ipaddress_clear(ipdevice->config->system);
1305 connman_ipaddress_clear(ipdevice->config->ipv6->system);
1307 connman_ipconfig_unref(ipdevice->config);
1308 ipdevice->config = NULL;
1313 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1316 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1318 case CONNMAN_IPCONFIG_METHOD_OFF:
1320 case CONNMAN_IPCONFIG_METHOD_FIXED:
1322 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1324 case CONNMAN_IPCONFIG_METHOD_DHCP:
1331 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1333 if (g_strcmp0(method, "off") == 0)
1334 return CONNMAN_IPCONFIG_METHOD_OFF;
1335 else if (g_strcmp0(method, "fixed") == 0)
1336 return CONNMAN_IPCONFIG_METHOD_FIXED;
1337 else if (g_strcmp0(method, "manual") == 0)
1338 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1339 else if (g_strcmp0(method, "dhcp") == 0)
1340 return CONNMAN_IPCONFIG_METHOD_DHCP;
1342 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1345 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1346 DBusMessageIter *iter)
1352 str = __connman_ipconfig_method2string(ipconfig->method);
1356 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1358 if (ipconfig->system == NULL)
1361 if (ipconfig->system->local != NULL) {
1363 struct in_addr netmask;
1366 connman_dbus_dict_append_basic(iter, "Address",
1367 DBUS_TYPE_STRING, &ipconfig->system->local);
1369 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1370 netmask.s_addr = htonl(addr);
1371 mask = inet_ntoa(netmask);
1372 connman_dbus_dict_append_basic(iter, "Netmask",
1373 DBUS_TYPE_STRING, &mask);
1376 if (ipconfig->system->gateway != NULL)
1377 connman_dbus_dict_append_basic(iter, "Gateway",
1378 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1381 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1382 DBusMessageIter *iter)
1388 str = __connman_ipconfig_method2string(ipconfig->method);
1392 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1394 if (ipconfig->system == NULL)
1397 if (ipconfig->system->local != NULL) {
1398 connman_dbus_dict_append_basic(iter, "Address",
1399 DBUS_TYPE_STRING, &ipconfig->system->local);
1400 connman_dbus_dict_append_basic(iter, "PrefixLength",
1402 &ipconfig->system->prefixlen);
1405 if (ipconfig->system->gateway != NULL)
1406 connman_dbus_dict_append_basic(iter, "Gateway",
1407 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1410 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1411 DBusMessageIter *iter)
1417 str = __connman_ipconfig_method2string(ipconfig->method);
1421 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1423 switch (ipconfig->method) {
1424 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1425 case CONNMAN_IPCONFIG_METHOD_OFF:
1426 case CONNMAN_IPCONFIG_METHOD_DHCP:
1428 case CONNMAN_IPCONFIG_METHOD_FIXED:
1429 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1433 if (ipconfig->address == NULL)
1436 if (ipconfig->address->local != NULL) {
1437 connman_dbus_dict_append_basic(iter, "Address",
1438 DBUS_TYPE_STRING, &ipconfig->address->local);
1439 connman_dbus_dict_append_basic(iter, "PrefixLength",
1441 &ipconfig->address->prefixlen);
1444 if (ipconfig->address->gateway != NULL)
1445 connman_dbus_dict_append_basic(iter, "Gateway",
1446 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1449 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1450 DBusMessageIter *iter)
1456 str = __connman_ipconfig_method2string(ipconfig->method);
1460 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1462 switch (ipconfig->method) {
1463 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1464 case CONNMAN_IPCONFIG_METHOD_OFF:
1465 case CONNMAN_IPCONFIG_METHOD_FIXED:
1466 case CONNMAN_IPCONFIG_METHOD_DHCP:
1468 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1472 if (ipconfig->address == NULL)
1475 if (ipconfig->address->local != NULL) {
1477 struct in_addr netmask;
1480 connman_dbus_dict_append_basic(iter, "Address",
1481 DBUS_TYPE_STRING, &ipconfig->address->local);
1483 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1484 netmask.s_addr = htonl(addr);
1485 mask = inet_ntoa(netmask);
1486 connman_dbus_dict_append_basic(iter, "Netmask",
1487 DBUS_TYPE_STRING, &mask);
1490 if (ipconfig->address->gateway != NULL)
1491 connman_dbus_dict_append_basic(iter, "Gateway",
1492 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1495 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1496 enum connman_ipconfig_type type, DBusMessageIter *array)
1498 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1499 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1500 *prefix_length_string = NULL;
1501 int prefix_length = 0;
1502 DBusMessageIter dict;
1504 DBG("ipconfig %p type %d", ipconfig, type);
1506 if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1507 type != CONNMAN_IPCONFIG_TYPE_IPV6)
1510 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1513 dbus_message_iter_recurse(array, &dict);
1515 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1516 DBusMessageIter entry;
1520 dbus_message_iter_recurse(&dict, &entry);
1522 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1525 dbus_message_iter_get_basic(&entry, &key);
1526 dbus_message_iter_next(&entry);
1528 type = dbus_message_iter_get_arg_type(&entry);
1530 if (g_str_equal(key, "Method") == TRUE) {
1533 if (type != DBUS_TYPE_STRING)
1536 dbus_message_iter_get_basic(&entry, &str);
1537 method = __connman_ipconfig_string2method(str);
1538 } else if (g_str_equal(key, "Address") == TRUE) {
1539 if (type != DBUS_TYPE_STRING)
1542 dbus_message_iter_get_basic(&entry, &address);
1543 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1544 if (type != DBUS_TYPE_STRING)
1547 dbus_message_iter_get_basic(&entry,
1548 &prefix_length_string);
1550 prefix_length = atoi(prefix_length_string);
1551 if (prefix_length < 0 || prefix_length > 128)
1554 } else if (g_str_equal(key, "Netmask") == TRUE) {
1555 if (type != DBUS_TYPE_STRING)
1558 dbus_message_iter_get_basic(&entry, &netmask);
1559 } else if (g_str_equal(key, "Gateway") == TRUE) {
1560 if (type != DBUS_TYPE_STRING)
1563 dbus_message_iter_get_basic(&entry, &gateway);
1565 dbus_message_iter_next(&dict);
1568 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1569 method, address, netmask, gateway, prefix_length);
1572 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1573 case CONNMAN_IPCONFIG_METHOD_OFF:
1574 case CONNMAN_IPCONFIG_METHOD_FIXED:
1577 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1578 if (address == NULL)
1581 ipconfig->method = method;
1583 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1584 connman_ipaddress_set_ipv4(ipconfig->address,
1585 address, netmask, gateway);
1587 return connman_ipaddress_set_ipv6(
1588 ipconfig->address, address,
1589 gateway, prefix_length);
1592 case CONNMAN_IPCONFIG_METHOD_DHCP:
1593 if (ipconfig->method == method)
1596 ipconfig->method = method;
1603 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1604 DBusMessageIter *iter)
1606 struct connman_ipdevice *ipdevice;
1607 const char *method = "direct";
1609 ipdevice = g_hash_table_lookup(ipdevice_hash,
1610 GINT_TO_POINTER(ipconfig->index));
1611 if (ipdevice == NULL)
1614 if (ipdevice->pac == NULL)
1617 method = "auto-config";
1619 connman_dbus_dict_append_basic(iter, "URL",
1620 DBUS_TYPE_STRING, &ipdevice->pac);
1623 connman_dbus_dict_append_basic(iter, "Method",
1624 DBUS_TYPE_STRING, &method);
1627 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1628 DBusMessageIter *iter)
1630 struct connman_ipdevice *ipdevice;
1631 const char *method = "auto";
1633 connman_dbus_dict_append_basic(iter, "Method",
1634 DBUS_TYPE_STRING, &method);
1636 ipdevice = g_hash_table_lookup(ipdevice_hash,
1637 GINT_TO_POINTER(ipconfig->index));
1638 if (ipdevice == NULL)
1641 if (ipdevice->ifname != NULL)
1642 connman_dbus_dict_append_basic(iter, "Interface",
1643 DBUS_TYPE_STRING, &ipdevice->ifname);
1645 if (ipdevice->address != NULL)
1646 connman_dbus_dict_append_basic(iter, "Address",
1647 DBUS_TYPE_STRING, &ipdevice->address);
1649 if (ipdevice->mtu > 0)
1650 connman_dbus_dict_append_basic(iter, "MTU",
1651 DBUS_TYPE_UINT16, &ipdevice->mtu);
1654 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1655 GKeyFile *keyfile, const char *identifier, const char *prefix)
1660 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1662 key = g_strdup_printf("%smethod", prefix);
1663 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1664 if (method == NULL) {
1665 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1666 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1668 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1670 ipconfig->method = __connman_ipconfig_string2method(method);
1673 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1674 ipconfig->address->prefixlen = g_key_file_get_integer(
1675 keyfile, identifier, key, NULL);
1678 key = g_strdup_printf("%slocal_address", prefix);
1679 ipconfig->address->local = g_key_file_get_string(
1680 keyfile, identifier, key, NULL);
1683 key = g_strdup_printf("%speer_address", prefix);
1684 ipconfig->address->peer = g_key_file_get_string(
1685 keyfile, identifier, key, NULL);
1688 key = g_strdup_printf("%sbroadcast_address", prefix);
1689 ipconfig->address->broadcast = g_key_file_get_string(
1690 keyfile, identifier, key, NULL);
1693 key = g_strdup_printf("%sgateway", prefix);
1694 ipconfig->address->gateway = g_key_file_get_string(
1695 keyfile, identifier, key, NULL);
1701 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1702 GKeyFile *keyfile, const char *identifier, const char *prefix)
1707 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1709 method = __connman_ipconfig_method2string(ipconfig->method);
1711 key = g_strdup_printf("%smethod", prefix);
1712 g_key_file_set_string(keyfile, identifier, key, method);
1715 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1716 g_key_file_set_integer(keyfile, identifier,
1717 key, ipconfig->address->prefixlen);
1720 key = g_strdup_printf("%slocal_address", prefix);
1721 if (ipconfig->address->local != NULL)
1722 g_key_file_set_string(keyfile, identifier,
1723 key, ipconfig->address->local);
1726 key = g_strdup_printf("%speer_address", prefix);
1727 if (ipconfig->address->peer != NULL)
1728 g_key_file_set_string(keyfile, identifier,
1729 key, ipconfig->address->peer);
1732 key = g_strdup_printf("%sbroadcast_address", prefix);
1733 if (ipconfig->address->broadcast != NULL)
1734 g_key_file_set_string(keyfile, identifier,
1735 key, ipconfig->address->broadcast);
1738 key = g_strdup_printf("%sgateway", prefix);
1739 if (ipconfig->address->gateway != NULL)
1740 g_key_file_set_string(keyfile, identifier,
1741 key, ipconfig->address->gateway);
1747 int __connman_ipconfig_init(void)
1751 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1752 NULL, free_ipdevice);
1757 void __connman_ipconfig_cleanup(void)
1761 g_hash_table_destroy(ipdevice_hash);
1762 ipdevice_hash = NULL;