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 {
76 struct connman_ipconfig *config;
78 struct connman_ipconfig_driver *driver;
79 struct connman_ipconfig *driver_config;
82 static GHashTable *ipdevice_hash = NULL;
83 static GList *ipconfig_list = NULL;
85 struct connman_ipaddress *connman_ipaddress_alloc(int family)
87 struct connman_ipaddress *ipaddress;
89 ipaddress = g_try_new0(struct connman_ipaddress, 1);
90 if (ipaddress == NULL)
93 ipaddress->family = family;
94 ipaddress->prefixlen = 0;
95 ipaddress->local = NULL;
96 ipaddress->peer = NULL;
97 ipaddress->broadcast = NULL;
98 ipaddress->gateway = NULL;
103 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
105 if (ipaddress == NULL)
108 g_free(ipaddress->broadcast);
109 g_free(ipaddress->peer);
110 g_free(ipaddress->local);
111 g_free(ipaddress->gateway);
115 static unsigned char netmask2prefixlen(const char *netmask)
117 unsigned char bits = 0;
118 in_addr_t mask = inet_network(netmask);
119 in_addr_t host = ~mask;
121 /* a valid netmask must be 2^n - 1 */
122 if ((host & (host + 1)) != 0)
125 for (; mask; mask <<= 1)
131 static gboolean check_ipv6_address(const char *address)
133 unsigned char buf[sizeof(struct in6_addr)];
136 err = inet_pton(AF_INET6, address, buf);
143 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
144 const char *address, const char *gateway,
145 unsigned char prefix_length)
147 if (ipaddress == NULL)
150 if (check_ipv6_address(address) == FALSE)
153 if (check_ipv6_address(gateway) == FALSE)
156 DBG("prefix_len %d address %s gateway %s",
157 prefix_length, address, gateway);
159 ipaddress->prefixlen = prefix_length;
161 g_free(ipaddress->local);
162 ipaddress->local = g_strdup(address);
164 g_free(ipaddress->gateway);
165 ipaddress->gateway = g_strdup(gateway);
170 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
171 const char *address, const char *netmask, const char *gateway)
173 if (ipaddress == NULL)
177 ipaddress->prefixlen = netmask2prefixlen(netmask);
179 ipaddress->prefixlen = 32;
181 g_free(ipaddress->local);
182 ipaddress->local = g_strdup(address);
184 g_free(ipaddress->gateway);
185 ipaddress->gateway = g_strdup(gateway);
188 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
190 if (ipaddress == NULL)
193 ipaddress->prefixlen = 0;
195 g_free(ipaddress->local);
196 ipaddress->local = NULL;
198 g_free(ipaddress->peer);
199 ipaddress->peer = NULL;
201 g_free(ipaddress->broadcast);
202 ipaddress->broadcast = NULL;
204 g_free(ipaddress->gateway);
205 ipaddress->gateway = NULL;
208 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
209 struct connman_ipaddress *source)
211 if (ipaddress == NULL || source == NULL)
214 ipaddress->family = source->family;
215 ipaddress->prefixlen = source->prefixlen;
217 g_free(ipaddress->local);
218 ipaddress->local = g_strdup(source->local);
220 g_free(ipaddress->peer);
221 ipaddress->peer = g_strdup(source->peer);
223 g_free(ipaddress->broadcast);
224 ipaddress->broadcast = g_strdup(source->broadcast);
226 g_free(ipaddress->gateway);
227 ipaddress->gateway = g_strdup(source->gateway);
230 static void free_address_list(struct connman_ipdevice *ipdevice)
234 for (list = ipdevice->address_list; list; list = list->next) {
235 struct connman_ipaddress *ipaddress = list->data;
237 connman_ipaddress_free(ipaddress);
241 g_slist_free(ipdevice->address_list);
242 ipdevice->address_list = NULL;
245 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
246 unsigned char prefixlen, const char *local)
250 for (list = ipdevice->address_list; list; list = list->next) {
251 struct connman_ipaddress *ipaddress = list->data;
253 if (g_strcmp0(ipaddress->local, local) == 0 &&
254 ipaddress->prefixlen == prefixlen)
261 static const char *type2str(unsigned short type)
266 case ARPHRD_LOOPBACK:
279 static const char *scope2str(unsigned char scope)
291 static void free_ipdevice(gpointer data)
293 struct connman_ipdevice *ipdevice = data;
295 connman_info("%s {remove} index %d", ipdevice->ifname,
298 if (ipdevice->config != NULL)
299 connman_ipconfig_unref(ipdevice->config);
301 free_address_list(ipdevice);
302 g_free(ipdevice->gateway);
304 g_free(ipdevice->address);
305 g_free(ipdevice->ifname);
309 static GSList *driver_list = NULL;
311 static gint compare_priority(gconstpointer a, gconstpointer b)
313 const struct connman_ipconfig_driver *driver1 = a;
314 const struct connman_ipconfig_driver *driver2 = b;
316 return driver2->priority - driver1->priority;
320 * connman_ipconfig_driver_register:
321 * @driver: IP configuration driver
323 * Register a new IP configuration driver
325 * Returns: %0 on success
327 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
329 DBG("driver %p name %s", driver, driver->name);
331 driver_list = g_slist_insert_sorted(driver_list, driver,
338 * connman_ipconfig_driver_unregister:
339 * @driver: IP configuration driver
341 * Remove a previously registered IP configuration driver.
343 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
345 DBG("driver %p name %s", driver, driver->name);
347 driver_list = g_slist_remove(driver_list, driver);
350 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
354 DBG("ipconfig %p", ipdevice->config);
356 if (ipdevice->config == NULL)
359 switch (ipdevice->config->method) {
360 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
361 case CONNMAN_IPCONFIG_METHOD_OFF:
362 case CONNMAN_IPCONFIG_METHOD_FIXED:
363 case CONNMAN_IPCONFIG_METHOD_MANUAL:
365 case CONNMAN_IPCONFIG_METHOD_DHCP:
369 if (ipdevice->driver != NULL)
372 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
373 if (ipdevice->driver_config == NULL)
376 for (list = driver_list; list; list = list->next) {
377 struct connman_ipconfig_driver *driver = list->data;
379 if (driver->request(ipdevice->driver_config) == 0) {
380 ipdevice->driver = driver;
385 if (ipdevice->driver == NULL) {
386 connman_ipconfig_unref(ipdevice->driver_config);
387 ipdevice->driver_config = NULL;
391 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
393 DBG("ipconfig %p", ipdevice->config);
395 if (ipdevice->config == NULL)
398 if (ipdevice->driver == NULL)
401 ipdevice->driver->release(ipdevice->driver_config);
403 ipdevice->driver = NULL;
405 connman_ipconfig_unref(ipdevice->driver_config);
406 ipdevice->driver_config = NULL;
408 connman_inet_clear_address(ipdevice->index);
409 connman_inet_clear_ipv6_address(ipdevice->index,
410 ipdevice->driver_config->address->local,
411 ipdevice->driver_config->address->prefixlen);
414 static void update_stats(struct connman_ipdevice *ipdevice,
415 struct rtnl_link_stats *stats)
417 if (stats->rx_packets == 0 && stats->tx_packets == 0)
420 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
421 stats->rx_packets, stats->rx_bytes);
422 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
423 stats->tx_packets, stats->tx_bytes);
425 if (ipdevice->config == NULL)
428 ipdevice->rx_packets = stats->rx_packets;
429 ipdevice->tx_packets = stats->tx_packets;
430 ipdevice->rx_bytes = stats->rx_bytes;
431 ipdevice->tx_bytes = stats->tx_bytes;
432 ipdevice->rx_errors = stats->rx_errors;
433 ipdevice->tx_errors = stats->tx_errors;
434 ipdevice->rx_dropped = stats->rx_dropped;
435 ipdevice->tx_dropped = stats->tx_dropped;
437 __connman_counter_notify(ipdevice->config,
438 ipdevice->rx_packets, ipdevice->tx_packets,
439 ipdevice->rx_bytes, ipdevice->tx_bytes,
440 ipdevice->rx_errors, ipdevice->tx_errors,
441 ipdevice->rx_dropped, ipdevice->tx_dropped);
444 void __connman_ipconfig_newlink(int index, unsigned short type,
445 unsigned int flags, const char *address,
447 struct rtnl_link_stats *stats)
449 struct connman_ipdevice *ipdevice;
452 gboolean up = FALSE, down = FALSE;
453 gboolean lower_up = FALSE, lower_down = FALSE;
455 DBG("index %d", index);
457 if (type == ARPHRD_LOOPBACK)
460 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
461 if (ipdevice != NULL)
464 ipdevice = g_try_new0(struct connman_ipdevice, 1);
465 if (ipdevice == NULL)
468 ipdevice->index = index;
469 ipdevice->ifname = connman_inet_ifname(index);
470 ipdevice->type = type;
472 ipdevice->address = g_strdup(address);
474 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
476 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
477 index, type, type2str(type));
482 update_stats(ipdevice, stats);
484 if (flags == ipdevice->flags)
487 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
494 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
495 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
496 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
497 (IFF_RUNNING | IFF_LOWER_UP))
499 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
503 ipdevice->flags = flags;
505 str = g_string_new(NULL);
510 g_string_append(str, "UP");
512 g_string_append(str, "DOWN");
514 if (flags & IFF_RUNNING)
515 g_string_append(str, ",RUNNING");
517 if (flags & IFF_LOWER_UP)
518 g_string_append(str, ",LOWER_UP");
520 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
523 g_string_free(str, TRUE);
525 for (list = g_list_first(ipconfig_list); list;
526 list = g_list_next(list)) {
527 struct connman_ipconfig *ipconfig = list->data;
529 if (index != ipconfig->index)
532 if (ipconfig->ops == NULL)
535 if (up == TRUE && ipconfig->ops->up)
536 ipconfig->ops->up(ipconfig);
537 if (lower_up == TRUE && ipconfig->ops->lower_up)
538 ipconfig->ops->lower_up(ipconfig);
540 if (lower_down == TRUE && ipconfig->ops->lower_down)
541 ipconfig->ops->lower_down(ipconfig);
542 if (down == TRUE && ipconfig->ops->down)
543 ipconfig->ops->down(ipconfig);
547 __connman_ipconfig_lower_up(ipdevice);
549 __connman_ipconfig_lower_down(ipdevice);
552 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
554 struct connman_ipdevice *ipdevice;
557 DBG("index %d", index);
559 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
560 if (ipdevice == NULL)
563 update_stats(ipdevice, stats);
565 for (list = g_list_first(ipconfig_list); list;
566 list = g_list_next(list)) {
567 struct connman_ipconfig *ipconfig = list->data;
569 if (index != ipconfig->index)
572 ipconfig->index = -1;
574 if (ipconfig->ops == NULL)
577 if (ipconfig->ops->lower_down)
578 ipconfig->ops->lower_down(ipconfig);
579 if (ipconfig->ops->down)
580 ipconfig->ops->down(ipconfig);
583 __connman_ipconfig_lower_down(ipdevice);
585 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
588 void __connman_ipconfig_newaddr(int index, int family, const char *label,
589 unsigned char prefixlen, const char *address)
591 struct connman_ipdevice *ipdevice;
592 struct connman_ipaddress *ipaddress;
595 DBG("index %d", index);
597 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
598 if (ipdevice == NULL)
601 ipaddress = connman_ipaddress_alloc(family);
602 if (ipaddress == NULL)
605 ipaddress->prefixlen = prefixlen;
606 ipaddress->local = g_strdup(address);
608 ipdevice->address_list = g_slist_append(ipdevice->address_list,
611 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
612 address, prefixlen, label);
614 if (ipdevice->config != NULL) {
615 if (family == AF_INET6 && ipdevice->config->ipv6 != NULL)
616 connman_ipaddress_copy(ipdevice->config->ipv6->system,
619 connman_ipaddress_copy(ipdevice->config->system,
623 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
626 if (g_slist_length(ipdevice->address_list) > 1)
629 for (list = g_list_first(ipconfig_list); list;
630 list = g_list_next(list)) {
631 struct connman_ipconfig *ipconfig = list->data;
633 if (index != ipconfig->index)
636 if (ipconfig->ops == NULL)
639 if (ipconfig->ops->ip_bound)
640 ipconfig->ops->ip_bound(ipconfig);
644 void __connman_ipconfig_deladdr(int index, int family, const char *label,
645 unsigned char prefixlen, const char *address)
647 struct connman_ipdevice *ipdevice;
648 struct connman_ipaddress *ipaddress;
651 DBG("index %d", index);
653 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
654 if (ipdevice == NULL)
657 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
658 if (ipaddress == NULL)
661 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
664 connman_ipaddress_free(ipaddress);
666 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
667 address, prefixlen, label);
669 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
672 if (g_slist_length(ipdevice->address_list) > 0)
675 for (list = g_list_first(ipconfig_list); list;
676 list = g_list_next(list)) {
677 struct connman_ipconfig *ipconfig = list->data;
679 if (index != ipconfig->index)
682 if (ipconfig->ops == NULL)
685 if (ipconfig->ops->ip_release)
686 ipconfig->ops->ip_release(ipconfig);
690 void __connman_ipconfig_newroute(int index, unsigned char scope,
691 const char *dst, const char *gateway)
693 struct connman_ipdevice *ipdevice;
695 DBG("index %d", index);
697 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
698 if (ipdevice == NULL)
701 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
704 g_free(ipdevice->gateway);
705 ipdevice->gateway = g_strdup(gateway);
707 if (ipdevice->config != NULL &&
708 ipdevice->config->system != NULL) {
709 g_free(ipdevice->config->system->gateway);
710 ipdevice->config->system->gateway = g_strdup(gateway);
713 for (list = ipdevice->address_list; list; list = list->next) {
714 struct connman_ipaddress *ipaddress = list->data;
716 g_free(ipaddress->gateway);
717 ipaddress->gateway = g_strdup(gateway);
721 connman_info("%s {add} route %s gw %s scope %u <%s>",
722 ipdevice->ifname, dst, gateway,
723 scope, scope2str(scope));
726 void __connman_ipconfig_delroute(int index, unsigned char scope,
727 const char *dst, const char *gateway)
729 struct connman_ipdevice *ipdevice;
731 DBG("index %d", index);
733 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
734 if (ipdevice == NULL)
737 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
740 g_free(ipdevice->gateway);
741 ipdevice->gateway = NULL;
743 if (ipdevice->config != NULL &&
744 ipdevice->config->system != NULL) {
745 g_free(ipdevice->config->system->gateway);
746 ipdevice->config->system->gateway = NULL;
749 for (list = ipdevice->address_list; list; list = list->next) {
750 struct connman_ipaddress *ipaddress = list->data;
752 g_free(ipaddress->gateway);
753 ipaddress->gateway = NULL;
757 connman_info("%s {del} route %s gw %s scope %u <%s>",
758 ipdevice->ifname, dst, gateway,
759 scope, scope2str(scope));
762 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
767 keys = g_hash_table_get_keys(ipdevice_hash);
771 for (list = g_list_first(keys); list; list = g_list_next(list)) {
772 int index = GPOINTER_TO_INT(list->data);
774 function(index, user_data);
780 unsigned short __connman_ipconfig_get_type(int index)
782 struct connman_ipdevice *ipdevice;
784 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
785 if (ipdevice == NULL)
788 return ipdevice->type;
791 unsigned int __connman_ipconfig_get_flags(int index)
793 struct connman_ipdevice *ipdevice;
795 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
796 if (ipdevice == NULL)
799 return ipdevice->flags;
802 const char *__connman_ipconfig_get_gateway(int index)
804 struct connman_ipdevice *ipdevice;
806 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
807 if (ipdevice == NULL)
810 if (ipdevice->gateway != NULL)
811 return ipdevice->gateway;
813 if (ipdevice->config != NULL &&
814 ipdevice->config->address != NULL)
815 return ipdevice->config->address->gateway;
820 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
822 ipconfig->index = index;
824 if (ipconfig->ipv6 != NULL)
825 ipconfig->ipv6->index = index;
828 static struct connman_ipconfig *create_ipv6config(int index)
830 struct connman_ipconfig *ipv6config;
832 DBG("index %d", index);
834 ipv6config = g_try_new0(struct connman_ipconfig, 1);
835 if (ipv6config == NULL)
838 ipv6config->index = index;
839 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
841 ipv6config->address = connman_ipaddress_alloc(AF_INET6);
842 if (ipv6config->address == NULL) {
847 ipv6config->system = connman_ipaddress_alloc(AF_INET6);
849 ipv6config->ipv6 = NULL;
851 DBG("ipconfig %p", ipv6config);
857 * connman_ipconfig_create:
859 * Allocate a new ipconfig structure.
861 * Returns: a newly-allocated #connman_ipconfig structure
863 struct connman_ipconfig *connman_ipconfig_create(int index)
865 struct connman_ipconfig *ipconfig;
867 DBG("index %d", index);
869 ipconfig = g_try_new0(struct connman_ipconfig, 1);
870 if (ipconfig == NULL)
873 ipconfig->refcount = 1;
875 ipconfig->index = index;
876 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
878 ipconfig->address = connman_ipaddress_alloc(AF_INET);
879 if (ipconfig->address == NULL) {
884 ipconfig->system = connman_ipaddress_alloc(AF_INET);
886 ipconfig->ipv6 = create_ipv6config(index);
888 DBG("ipconfig %p", ipconfig);
894 * connman_ipconfig_clone:
896 * Clone an ipconfig structure and create new reference.
898 * Returns: a newly-allocated #connman_ipconfig structure
900 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
902 struct connman_ipconfig *ipconfig_clone;
904 DBG("ipconfig %p", ipconfig);
906 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
907 if (ipconfig_clone == NULL)
910 ipconfig_clone->refcount = 1;
912 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
914 ipconfig_clone->index = -1;
916 return ipconfig_clone;
920 * connman_ipconfig_ref:
921 * @ipconfig: ipconfig structure
923 * Increase reference counter of ipconfig
925 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
927 g_atomic_int_inc(&ipconfig->refcount);
932 static void free_ipv6config(struct connman_ipconfig *ipconfig)
934 if (ipconfig == NULL)
937 connman_ipconfig_set_ops(ipconfig, NULL);
938 connman_ipaddress_free(ipconfig->system);
939 connman_ipaddress_free(ipconfig->address);
940 g_free(ipconfig->ipv6);
944 * connman_ipconfig_unref:
945 * @ipconfig: ipconfig structure
947 * Decrease reference counter of ipconfig
949 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
951 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
952 __connman_ipconfig_disable(ipconfig);
954 connman_ipconfig_set_ops(ipconfig, NULL);
956 if (ipconfig->origin != NULL) {
957 connman_ipconfig_unref(ipconfig->origin);
958 ipconfig->origin = NULL;
961 connman_ipaddress_free(ipconfig->system);
962 connman_ipaddress_free(ipconfig->address);
963 free_ipv6config(ipconfig->ipv6);
969 * connman_ipconfig_get_data:
970 * @ipconfig: ipconfig structure
972 * Get private data pointer
974 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
976 return ipconfig->ops_data;
980 * connman_ipconfig_set_data:
981 * @ipconfig: ipconfig structure
982 * @data: data pointer
984 * Set private data pointer
986 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
988 ipconfig->ops_data = data;
992 * connman_ipconfig_get_index:
993 * @ipconfig: ipconfig structure
995 * Get interface index
997 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
999 if (ipconfig == NULL)
1002 if (ipconfig->origin != NULL)
1003 return ipconfig->origin->index;
1005 return ipconfig->index;
1009 * connman_ipconfig_get_ifname:
1010 * @ipconfig: ipconfig structure
1012 * Get interface name
1014 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1016 struct connman_ipdevice *ipdevice;
1018 if (ipconfig == NULL)
1021 if (ipconfig->index < 0)
1024 ipdevice = g_hash_table_lookup(ipdevice_hash,
1025 GINT_TO_POINTER(ipconfig->index));
1026 if (ipdevice == NULL)
1029 return ipdevice->ifname;
1033 * connman_ipconfig_set_ops:
1034 * @ipconfig: ipconfig structure
1035 * @ops: operation callbacks
1037 * Set the operation callbacks
1039 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1040 const struct connman_ipconfig_ops *ops)
1042 ipconfig->ops = ops;
1045 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1046 struct connman_ipconfig *ipconfig)
1048 if (ipconfig == NULL)
1051 return ipconfig->ipv6;
1055 * connman_ipconfig_set_method:
1056 * @ipconfig: ipconfig structure
1057 * @method: configuration method
1059 * Set the configuration method
1061 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1062 enum connman_ipconfig_method method)
1064 ipconfig->method = method;
1069 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1071 if (ipconfig == NULL)
1072 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1074 return ipconfig->method;
1078 * connman_ipconfig_bind:
1079 * @ipconfig: ipconfig structure
1080 * @ipaddress: ipaddress structure
1082 * Bind IP address details to configuration
1084 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1085 struct connman_ipaddress *ipaddress)
1087 struct connman_ipconfig *origin;
1089 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1091 connman_ipaddress_copy(origin->address, ipaddress);
1093 connman_inet_set_address(origin->index, origin->address);
1096 void __connman_ipconfig_set_element_ipv6_gateway(
1097 struct connman_ipconfig *ipconfig,
1098 struct connman_element *element)
1100 element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1104 * FIXME: The element soulution should be removed in the future
1105 * Set IPv4 and IPv6 gateway
1107 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1108 struct connman_element *parent)
1110 struct connman_element *connection;
1112 connection = connman_element_create(NULL);
1114 DBG("ipconfig %p", ipconfig);
1116 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1117 connection->index = ipconfig->index;
1118 connection->ipv4.gateway = ipconfig->address->gateway;
1119 connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1121 if (connman_element_register(connection, parent) < 0)
1122 connman_element_unref(connection);
1127 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1131 switch (ipconfig->method) {
1132 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1133 case CONNMAN_IPCONFIG_METHOD_OFF:
1134 case CONNMAN_IPCONFIG_METHOD_FIXED:
1135 case CONNMAN_IPCONFIG_METHOD_DHCP:
1137 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1138 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1139 return connman_inet_set_address(ipconfig->index,
1141 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1142 return connman_inet_set_ipv6_address(
1143 ipconfig->index, ipconfig->address);
1149 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1153 if (ipconfig == NULL)
1156 DBG("method %d", ipconfig->method);
1158 switch (ipconfig->method) {
1159 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1160 case CONNMAN_IPCONFIG_METHOD_OFF:
1161 case CONNMAN_IPCONFIG_METHOD_FIXED:
1162 case CONNMAN_IPCONFIG_METHOD_DHCP:
1164 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1165 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1166 return connman_inet_clear_address(ipconfig->index);
1167 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1168 return connman_inet_clear_ipv6_address(
1170 ipconfig->address->local,
1171 ipconfig->address->prefixlen);
1177 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1179 struct connman_ipdevice *ipdevice;
1180 gboolean up = FALSE, down = FALSE;
1181 gboolean lower_up = FALSE, lower_down = FALSE;
1183 DBG("ipconfig %p", ipconfig);
1185 if (ipconfig == NULL || ipconfig->index < 0)
1188 ipdevice = g_hash_table_lookup(ipdevice_hash,
1189 GINT_TO_POINTER(ipconfig->index));
1190 if (ipdevice == NULL)
1193 if (ipdevice->config == ipconfig)
1196 if (ipdevice->config != NULL) {
1197 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1199 connman_ipaddress_clear(ipdevice->config->system);
1201 connman_ipconfig_unref(ipdevice->config);
1204 ipdevice->config = connman_ipconfig_ref(ipconfig);
1206 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1208 if (ipdevice->flags & IFF_UP)
1213 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1214 (IFF_RUNNING | IFF_LOWER_UP))
1216 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1219 if (up == TRUE && ipconfig->ops->up)
1220 ipconfig->ops->up(ipconfig);
1221 if (lower_up == TRUE && ipconfig->ops->lower_up)
1222 ipconfig->ops->lower_up(ipconfig);
1224 if (lower_down == TRUE && ipconfig->ops->lower_down)
1225 ipconfig->ops->lower_down(ipconfig);
1226 if (down == TRUE && ipconfig->ops->down)
1227 ipconfig->ops->down(ipconfig);
1232 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1234 struct connman_ipdevice *ipdevice;
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 == NULL || ipdevice->config != ipconfig)
1249 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1251 connman_ipaddress_clear(ipdevice->config->system);
1252 connman_ipaddress_clear(ipdevice->config->ipv6->system);
1254 connman_ipconfig_unref(ipdevice->config);
1255 ipdevice->config = NULL;
1260 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1263 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1265 case CONNMAN_IPCONFIG_METHOD_OFF:
1267 case CONNMAN_IPCONFIG_METHOD_FIXED:
1269 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1271 case CONNMAN_IPCONFIG_METHOD_DHCP:
1278 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1280 if (g_strcmp0(method, "off") == 0)
1281 return CONNMAN_IPCONFIG_METHOD_OFF;
1282 else if (g_strcmp0(method, "fixed") == 0)
1283 return CONNMAN_IPCONFIG_METHOD_FIXED;
1284 else if (g_strcmp0(method, "manual") == 0)
1285 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1286 else if (g_strcmp0(method, "dhcp") == 0)
1287 return CONNMAN_IPCONFIG_METHOD_DHCP;
1289 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1292 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1293 DBusMessageIter *iter)
1299 str = __connman_ipconfig_method2string(ipconfig->method);
1303 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1305 if (ipconfig->system == NULL)
1308 if (ipconfig->system->local != NULL) {
1310 struct in_addr netmask;
1313 connman_dbus_dict_append_basic(iter, "Address",
1314 DBUS_TYPE_STRING, &ipconfig->system->local);
1316 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1317 netmask.s_addr = htonl(addr);
1318 mask = inet_ntoa(netmask);
1319 connman_dbus_dict_append_basic(iter, "Netmask",
1320 DBUS_TYPE_STRING, &mask);
1323 if (ipconfig->system->gateway != NULL)
1324 connman_dbus_dict_append_basic(iter, "Gateway",
1325 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1328 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1329 DBusMessageIter *iter)
1335 str = __connman_ipconfig_method2string(ipconfig->method);
1339 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1341 if (ipconfig->system == NULL)
1344 if (ipconfig->system->local != NULL) {
1345 connman_dbus_dict_append_basic(iter, "Address",
1346 DBUS_TYPE_STRING, &ipconfig->system->local);
1347 connman_dbus_dict_append_basic(iter, "PrefixLength",
1349 &ipconfig->system->prefixlen);
1352 if (ipconfig->system->gateway != NULL)
1353 connman_dbus_dict_append_basic(iter, "Gateway",
1354 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1357 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1358 DBusMessageIter *iter)
1364 str = __connman_ipconfig_method2string(ipconfig->method);
1368 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1370 switch (ipconfig->method) {
1371 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1372 case CONNMAN_IPCONFIG_METHOD_OFF:
1373 case CONNMAN_IPCONFIG_METHOD_DHCP:
1375 case CONNMAN_IPCONFIG_METHOD_FIXED:
1376 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1380 if (ipconfig->address == NULL)
1383 if (ipconfig->address->local != NULL) {
1384 connman_dbus_dict_append_basic(iter, "Address",
1385 DBUS_TYPE_STRING, &ipconfig->address->local);
1386 connman_dbus_dict_append_basic(iter, "PrefixLength",
1388 &ipconfig->address->prefixlen);
1391 if (ipconfig->address->gateway != NULL)
1392 connman_dbus_dict_append_basic(iter, "Gateway",
1393 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1396 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1397 DBusMessageIter *iter)
1403 str = __connman_ipconfig_method2string(ipconfig->method);
1407 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1409 switch (ipconfig->method) {
1410 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1411 case CONNMAN_IPCONFIG_METHOD_OFF:
1412 case CONNMAN_IPCONFIG_METHOD_FIXED:
1413 case CONNMAN_IPCONFIG_METHOD_DHCP:
1415 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1419 if (ipconfig->address == NULL)
1422 if (ipconfig->address->local != NULL) {
1424 struct in_addr netmask;
1427 connman_dbus_dict_append_basic(iter, "Address",
1428 DBUS_TYPE_STRING, &ipconfig->address->local);
1430 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1431 netmask.s_addr = htonl(addr);
1432 mask = inet_ntoa(netmask);
1433 connman_dbus_dict_append_basic(iter, "Netmask",
1434 DBUS_TYPE_STRING, &mask);
1437 if (ipconfig->address->gateway != NULL)
1438 connman_dbus_dict_append_basic(iter, "Gateway",
1439 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1442 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1443 enum connman_ipconfig_type type, DBusMessageIter *array)
1445 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1446 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1447 *prefix_length_string = NULL;
1448 int prefix_length = 0;
1449 DBusMessageIter dict;
1451 DBG("ipconfig %p type %d", ipconfig, type);
1453 if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1454 type != CONNMAN_IPCONFIG_TYPE_IPV6)
1457 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1460 dbus_message_iter_recurse(array, &dict);
1462 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1463 DBusMessageIter entry;
1467 dbus_message_iter_recurse(&dict, &entry);
1469 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1472 dbus_message_iter_get_basic(&entry, &key);
1473 dbus_message_iter_next(&entry);
1475 type = dbus_message_iter_get_arg_type(&entry);
1477 if (g_str_equal(key, "Method") == TRUE) {
1480 if (type != DBUS_TYPE_STRING)
1483 dbus_message_iter_get_basic(&entry, &str);
1484 method = __connman_ipconfig_string2method(str);
1485 } else if (g_str_equal(key, "Address") == TRUE) {
1486 if (type != DBUS_TYPE_STRING)
1489 dbus_message_iter_get_basic(&entry, &address);
1490 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1491 if (type != DBUS_TYPE_STRING)
1494 dbus_message_iter_get_basic(&entry,
1495 &prefix_length_string);
1497 prefix_length = atoi(prefix_length_string);
1498 if (prefix_length < 0 || prefix_length > 128)
1501 } else if (g_str_equal(key, "Netmask") == TRUE) {
1502 if (type != DBUS_TYPE_STRING)
1505 dbus_message_iter_get_basic(&entry, &netmask);
1506 } else if (g_str_equal(key, "Gateway") == TRUE) {
1507 if (type != DBUS_TYPE_STRING)
1510 dbus_message_iter_get_basic(&entry, &gateway);
1512 dbus_message_iter_next(&dict);
1515 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1516 method, address, netmask, gateway, prefix_length);
1519 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1520 case CONNMAN_IPCONFIG_METHOD_OFF:
1521 case CONNMAN_IPCONFIG_METHOD_FIXED:
1524 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1525 if (address == NULL)
1528 ipconfig->method = method;
1530 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1531 connman_ipaddress_set_ipv4(ipconfig->address,
1532 address, netmask, gateway);
1534 return connman_ipaddress_set_ipv6(
1535 ipconfig->address, address,
1536 gateway, prefix_length);
1539 case CONNMAN_IPCONFIG_METHOD_DHCP:
1540 if (ipconfig->method == method)
1543 ipconfig->method = method;
1550 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1551 DBusMessageIter *iter)
1553 const char *method = "direct";
1555 connman_dbus_dict_append_basic(iter, "Method",
1556 DBUS_TYPE_STRING, &method);
1559 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1560 DBusMessageIter *iter)
1562 struct connman_ipdevice *ipdevice;
1563 const char *method = "auto";
1565 connman_dbus_dict_append_basic(iter, "Method",
1566 DBUS_TYPE_STRING, &method);
1568 ipdevice = g_hash_table_lookup(ipdevice_hash,
1569 GINT_TO_POINTER(ipconfig->index));
1570 if (ipdevice == NULL)
1573 if (ipdevice->ifname != NULL)
1574 connman_dbus_dict_append_basic(iter, "Interface",
1575 DBUS_TYPE_STRING, &ipdevice->ifname);
1577 if (ipdevice->address != NULL)
1578 connman_dbus_dict_append_basic(iter, "Address",
1579 DBUS_TYPE_STRING, &ipdevice->address);
1581 if (ipdevice->mtu > 0)
1582 connman_dbus_dict_append_basic(iter, "MTU",
1583 DBUS_TYPE_UINT16, &ipdevice->mtu);
1586 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1587 GKeyFile *keyfile, const char *identifier, const char *prefix)
1592 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1594 key = g_strdup_printf("%smethod", prefix);
1595 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1597 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1599 ipconfig->method = __connman_ipconfig_string2method(method);
1602 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1603 ipconfig->address->prefixlen = g_key_file_get_integer(
1604 keyfile, identifier, key, NULL);
1607 key = g_strdup_printf("%slocal_address", prefix);
1608 ipconfig->address->local = g_key_file_get_string(
1609 keyfile, identifier, key, NULL);
1612 key = g_strdup_printf("%speer_address", prefix);
1613 ipconfig->address->peer = g_key_file_get_string(
1614 keyfile, identifier, key, NULL);
1617 key = g_strdup_printf("%sbroadcast_address", prefix);
1618 ipconfig->address->broadcast = g_key_file_get_string(
1619 keyfile, identifier, key, NULL);
1622 key = g_strdup_printf("%sgateway", prefix);
1623 ipconfig->address->gateway = g_key_file_get_string(
1624 keyfile, identifier, key, NULL);
1630 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1631 GKeyFile *keyfile, const char *identifier, const char *prefix)
1636 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1638 method = __connman_ipconfig_method2string(ipconfig->method);
1640 key = g_strdup_printf("%smethod", prefix);
1641 g_key_file_set_string(keyfile, identifier, key, method);
1644 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1645 g_key_file_set_integer(keyfile, identifier,
1646 key, ipconfig->address->prefixlen);
1649 key = g_strdup_printf("%slocal_address", prefix);
1650 if (ipconfig->address->local != NULL)
1651 g_key_file_set_string(keyfile, identifier,
1652 key, ipconfig->address->local);
1655 key = g_strdup_printf("%speer_address", prefix);
1656 if (ipconfig->address->peer != NULL)
1657 g_key_file_set_string(keyfile, identifier,
1658 key, ipconfig->address->peer);
1661 key = g_strdup_printf("%sbroadcast_address", prefix);
1662 if (ipconfig->address->broadcast != NULL)
1663 g_key_file_set_string(keyfile, identifier,
1664 key, ipconfig->address->broadcast);
1667 key = g_strdup_printf("%sgateway", prefix);
1668 if (ipconfig->address->gateway != NULL)
1669 g_key_file_set_string(keyfile, identifier,
1670 key, ipconfig->address->gateway);
1676 int __connman_ipconfig_init(void)
1680 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1681 NULL, free_ipdevice);
1686 void __connman_ipconfig_cleanup(void)
1690 g_hash_table_destroy(ipdevice_hash);
1691 ipdevice_hash = NULL;