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(void)
87 struct connman_ipaddress *ipaddress;
89 ipaddress = g_try_new0(struct connman_ipaddress, 1);
90 if (ipaddress == NULL)
93 ipaddress->prefixlen = 0;
94 ipaddress->local = NULL;
95 ipaddress->peer = NULL;
96 ipaddress->broadcast = NULL;
97 ipaddress->gateway = NULL;
102 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
104 if (ipaddress == NULL)
107 g_free(ipaddress->broadcast);
108 g_free(ipaddress->peer);
109 g_free(ipaddress->local);
110 g_free(ipaddress->gateway);
114 static unsigned char netmask2prefixlen(const char *netmask)
116 unsigned char bits = 0;
117 in_addr_t mask = inet_network(netmask);
118 in_addr_t host = ~mask;
120 /* a valid netmask must be 2^n - 1 */
121 if ((host & (host + 1)) != 0)
124 for (; mask; mask <<= 1)
130 static gboolean check_ipv6_address(const char *address)
132 unsigned char buf[sizeof(struct in6_addr)];
135 err = inet_pton(AF_INET6, address, buf);
142 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
143 const char *address, const char *gateway,
144 unsigned char prefix_length)
146 if (ipaddress == NULL)
149 if (check_ipv6_address(address) == FALSE)
152 if (check_ipv6_address(gateway) == FALSE)
155 DBG("prefix_len %d address %s gateway %s",
156 prefix_length, address, gateway);
158 ipaddress->prefixlen = prefix_length;
160 g_free(ipaddress->local);
161 ipaddress->local = g_strdup(address);
163 g_free(ipaddress->gateway);
164 ipaddress->gateway = g_strdup(gateway);
169 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
170 const char *address, const char *netmask, const char *gateway)
172 if (ipaddress == NULL)
176 ipaddress->prefixlen = netmask2prefixlen(netmask);
178 ipaddress->prefixlen = 32;
180 g_free(ipaddress->local);
181 ipaddress->local = g_strdup(address);
183 g_free(ipaddress->gateway);
184 ipaddress->gateway = g_strdup(gateway);
187 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
189 if (ipaddress == NULL)
192 ipaddress->prefixlen = 0;
194 g_free(ipaddress->local);
195 ipaddress->local = NULL;
197 g_free(ipaddress->peer);
198 ipaddress->peer = NULL;
200 g_free(ipaddress->broadcast);
201 ipaddress->broadcast = NULL;
203 g_free(ipaddress->gateway);
204 ipaddress->gateway = NULL;
207 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
208 struct connman_ipaddress *source)
210 if (ipaddress == NULL || source == NULL)
213 ipaddress->prefixlen = source->prefixlen;
215 g_free(ipaddress->local);
216 ipaddress->local = g_strdup(source->local);
218 g_free(ipaddress->peer);
219 ipaddress->peer = g_strdup(source->peer);
221 g_free(ipaddress->broadcast);
222 ipaddress->broadcast = g_strdup(source->broadcast);
224 g_free(ipaddress->gateway);
225 ipaddress->gateway = g_strdup(source->gateway);
228 static void free_address_list(struct connman_ipdevice *ipdevice)
232 for (list = ipdevice->address_list; list; list = list->next) {
233 struct connman_ipaddress *ipaddress = list->data;
235 connman_ipaddress_free(ipaddress);
239 g_slist_free(ipdevice->address_list);
240 ipdevice->address_list = NULL;
243 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
244 unsigned char prefixlen, const char *local)
248 for (list = ipdevice->address_list; list; list = list->next) {
249 struct connman_ipaddress *ipaddress = list->data;
251 if (g_strcmp0(ipaddress->local, local) == 0 &&
252 ipaddress->prefixlen == prefixlen)
259 static const char *type2str(unsigned short type)
264 case ARPHRD_LOOPBACK:
277 static const char *scope2str(unsigned char scope)
289 static void free_ipdevice(gpointer data)
291 struct connman_ipdevice *ipdevice = data;
293 connman_info("%s {remove} index %d", ipdevice->ifname,
296 if (ipdevice->config != NULL)
297 connman_ipconfig_unref(ipdevice->config);
299 free_address_list(ipdevice);
300 g_free(ipdevice->gateway);
302 g_free(ipdevice->address);
303 g_free(ipdevice->ifname);
307 static GSList *driver_list = NULL;
309 static gint compare_priority(gconstpointer a, gconstpointer b)
311 const struct connman_ipconfig_driver *driver1 = a;
312 const struct connman_ipconfig_driver *driver2 = b;
314 return driver2->priority - driver1->priority;
318 * connman_ipconfig_driver_register:
319 * @driver: IP configuration driver
321 * Register a new IP configuration driver
323 * Returns: %0 on success
325 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
327 DBG("driver %p name %s", driver, driver->name);
329 driver_list = g_slist_insert_sorted(driver_list, driver,
336 * connman_ipconfig_driver_unregister:
337 * @driver: IP configuration driver
339 * Remove a previously registered IP configuration driver.
341 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
343 DBG("driver %p name %s", driver, driver->name);
345 driver_list = g_slist_remove(driver_list, driver);
348 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
352 DBG("ipconfig %p", ipdevice->config);
354 if (ipdevice->config == NULL)
357 switch (ipdevice->config->method) {
358 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
359 case CONNMAN_IPCONFIG_METHOD_OFF:
360 case CONNMAN_IPCONFIG_METHOD_FIXED:
361 case CONNMAN_IPCONFIG_METHOD_MANUAL:
363 case CONNMAN_IPCONFIG_METHOD_DHCP:
367 if (ipdevice->driver != NULL)
370 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
371 if (ipdevice->driver_config == NULL)
374 for (list = driver_list; list; list = list->next) {
375 struct connman_ipconfig_driver *driver = list->data;
377 if (driver->request(ipdevice->driver_config) == 0) {
378 ipdevice->driver = driver;
383 if (ipdevice->driver == NULL) {
384 connman_ipconfig_unref(ipdevice->driver_config);
385 ipdevice->driver_config = NULL;
389 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
391 DBG("ipconfig %p", ipdevice->config);
393 if (ipdevice->config == NULL)
396 if (ipdevice->driver == NULL)
399 ipdevice->driver->release(ipdevice->driver_config);
401 ipdevice->driver = NULL;
403 connman_ipconfig_unref(ipdevice->driver_config);
404 ipdevice->driver_config = NULL;
406 connman_inet_clear_address(ipdevice->index);
407 connman_inet_clear_ipv6_address(ipdevice->index,
408 ipdevice->driver_config->address->local,
409 ipdevice->driver_config->address->prefixlen);
412 static void update_stats(struct connman_ipdevice *ipdevice,
413 struct rtnl_link_stats *stats)
415 if (stats->rx_packets == 0 && stats->tx_packets == 0)
418 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
419 stats->rx_packets, stats->rx_bytes);
420 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
421 stats->tx_packets, stats->tx_bytes);
423 if (ipdevice->config == NULL)
426 ipdevice->rx_packets = stats->rx_packets;
427 ipdevice->tx_packets = stats->tx_packets;
428 ipdevice->rx_bytes = stats->rx_bytes;
429 ipdevice->tx_bytes = stats->tx_bytes;
430 ipdevice->rx_errors = stats->rx_errors;
431 ipdevice->tx_errors = stats->tx_errors;
432 ipdevice->rx_dropped = stats->rx_dropped;
433 ipdevice->tx_dropped = stats->tx_dropped;
435 __connman_counter_notify(ipdevice->config,
436 ipdevice->rx_packets, ipdevice->tx_packets,
437 ipdevice->rx_bytes, ipdevice->tx_bytes,
438 ipdevice->rx_errors, ipdevice->tx_errors,
439 ipdevice->rx_dropped, ipdevice->tx_dropped);
442 void __connman_ipconfig_newlink(int index, unsigned short type,
443 unsigned int flags, const char *address,
445 struct rtnl_link_stats *stats)
447 struct connman_ipdevice *ipdevice;
450 gboolean up = FALSE, down = FALSE;
451 gboolean lower_up = FALSE, lower_down = FALSE;
453 DBG("index %d", index);
455 if (type == ARPHRD_LOOPBACK)
458 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
459 if (ipdevice != NULL)
462 ipdevice = g_try_new0(struct connman_ipdevice, 1);
463 if (ipdevice == NULL)
466 ipdevice->index = index;
467 ipdevice->ifname = connman_inet_ifname(index);
468 ipdevice->type = type;
470 ipdevice->address = g_strdup(address);
472 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
474 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
475 index, type, type2str(type));
480 update_stats(ipdevice, stats);
482 if (flags == ipdevice->flags)
485 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
492 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
493 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
494 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
495 (IFF_RUNNING | IFF_LOWER_UP))
497 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
501 ipdevice->flags = flags;
503 str = g_string_new(NULL);
508 g_string_append(str, "UP");
510 g_string_append(str, "DOWN");
512 if (flags & IFF_RUNNING)
513 g_string_append(str, ",RUNNING");
515 if (flags & IFF_LOWER_UP)
516 g_string_append(str, ",LOWER_UP");
518 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
521 g_string_free(str, TRUE);
523 for (list = g_list_first(ipconfig_list); list;
524 list = g_list_next(list)) {
525 struct connman_ipconfig *ipconfig = list->data;
527 if (index != ipconfig->index)
530 if (ipconfig->ops == NULL)
533 if (up == TRUE && ipconfig->ops->up)
534 ipconfig->ops->up(ipconfig);
535 if (lower_up == TRUE && ipconfig->ops->lower_up)
536 ipconfig->ops->lower_up(ipconfig);
538 if (lower_down == TRUE && ipconfig->ops->lower_down)
539 ipconfig->ops->lower_down(ipconfig);
540 if (down == TRUE && ipconfig->ops->down)
541 ipconfig->ops->down(ipconfig);
545 __connman_ipconfig_lower_up(ipdevice);
547 __connman_ipconfig_lower_down(ipdevice);
550 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
552 struct connman_ipdevice *ipdevice;
555 DBG("index %d", index);
557 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
558 if (ipdevice == NULL)
561 update_stats(ipdevice, stats);
563 for (list = g_list_first(ipconfig_list); list;
564 list = g_list_next(list)) {
565 struct connman_ipconfig *ipconfig = list->data;
567 if (index != ipconfig->index)
570 ipconfig->index = -1;
572 if (ipconfig->ops == NULL)
575 if (ipconfig->ops->lower_down)
576 ipconfig->ops->lower_down(ipconfig);
577 if (ipconfig->ops->down)
578 ipconfig->ops->down(ipconfig);
581 __connman_ipconfig_lower_down(ipdevice);
583 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
586 void __connman_ipconfig_newaddr(int index, const char *label,
587 unsigned char prefixlen, const char *address)
589 struct connman_ipdevice *ipdevice;
590 struct connman_ipaddress *ipaddress;
593 DBG("index %d", index);
595 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
596 if (ipdevice == NULL)
599 ipaddress = connman_ipaddress_alloc();
600 if (ipaddress == NULL)
603 ipaddress->prefixlen = prefixlen;
604 ipaddress->local = g_strdup(address);
606 ipdevice->address_list = g_slist_append(ipdevice->address_list,
609 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
610 address, prefixlen, label);
612 if (ipdevice->config != NULL)
613 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
615 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
618 if (g_slist_length(ipdevice->address_list) > 1)
621 for (list = g_list_first(ipconfig_list); list;
622 list = g_list_next(list)) {
623 struct connman_ipconfig *ipconfig = list->data;
625 if (index != ipconfig->index)
628 if (ipconfig->ops == NULL)
631 if (ipconfig->ops->ip_bound)
632 ipconfig->ops->ip_bound(ipconfig);
636 void __connman_ipconfig_deladdr(int index, const char *label,
637 unsigned char prefixlen, const char *address)
639 struct connman_ipdevice *ipdevice;
640 struct connman_ipaddress *ipaddress;
643 DBG("index %d", index);
645 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
646 if (ipdevice == NULL)
649 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
650 if (ipaddress == NULL)
653 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
656 connman_ipaddress_free(ipaddress);
658 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
659 address, prefixlen, label);
661 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
664 if (g_slist_length(ipdevice->address_list) > 0)
667 for (list = g_list_first(ipconfig_list); list;
668 list = g_list_next(list)) {
669 struct connman_ipconfig *ipconfig = list->data;
671 if (index != ipconfig->index)
674 if (ipconfig->ops == NULL)
677 if (ipconfig->ops->ip_release)
678 ipconfig->ops->ip_release(ipconfig);
682 void __connman_ipconfig_newroute(int index, unsigned char scope,
683 const char *dst, const char *gateway)
685 struct connman_ipdevice *ipdevice;
687 DBG("index %d", index);
689 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
690 if (ipdevice == NULL)
693 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
696 g_free(ipdevice->gateway);
697 ipdevice->gateway = g_strdup(gateway);
699 if (ipdevice->config != NULL &&
700 ipdevice->config->system != NULL) {
701 g_free(ipdevice->config->system->gateway);
702 ipdevice->config->system->gateway = g_strdup(gateway);
705 for (list = ipdevice->address_list; list; list = list->next) {
706 struct connman_ipaddress *ipaddress = list->data;
708 g_free(ipaddress->gateway);
709 ipaddress->gateway = g_strdup(gateway);
713 connman_info("%s {add} route %s gw %s scope %u <%s>",
714 ipdevice->ifname, dst, gateway,
715 scope, scope2str(scope));
718 void __connman_ipconfig_delroute(int index, unsigned char scope,
719 const char *dst, const char *gateway)
721 struct connman_ipdevice *ipdevice;
723 DBG("index %d", index);
725 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
726 if (ipdevice == NULL)
729 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
732 g_free(ipdevice->gateway);
733 ipdevice->gateway = NULL;
735 if (ipdevice->config != NULL &&
736 ipdevice->config->system != NULL) {
737 g_free(ipdevice->config->system->gateway);
738 ipdevice->config->system->gateway = NULL;
741 for (list = ipdevice->address_list; list; list = list->next) {
742 struct connman_ipaddress *ipaddress = list->data;
744 g_free(ipaddress->gateway);
745 ipaddress->gateway = NULL;
749 connman_info("%s {del} route %s gw %s scope %u <%s>",
750 ipdevice->ifname, dst, gateway,
751 scope, scope2str(scope));
754 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
759 keys = g_hash_table_get_keys(ipdevice_hash);
763 for (list = g_list_first(keys); list; list = g_list_next(list)) {
764 int index = GPOINTER_TO_INT(list->data);
766 function(index, user_data);
772 unsigned short __connman_ipconfig_get_type(int index)
774 struct connman_ipdevice *ipdevice;
776 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
777 if (ipdevice == NULL)
780 return ipdevice->type;
783 unsigned int __connman_ipconfig_get_flags(int index)
785 struct connman_ipdevice *ipdevice;
787 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
788 if (ipdevice == NULL)
791 return ipdevice->flags;
794 const char *__connman_ipconfig_get_gateway(int index)
796 struct connman_ipdevice *ipdevice;
798 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
799 if (ipdevice == NULL)
802 if (ipdevice->gateway != NULL)
803 return ipdevice->gateway;
805 if (ipdevice->config != NULL &&
806 ipdevice->config->address != NULL)
807 return ipdevice->config->address->gateway;
812 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
814 ipconfig->index = index;
816 if (ipconfig->ipv6 != NULL)
817 ipconfig->ipv6->index = index;
820 static struct connman_ipconfig *create_ipv6config(int index)
822 struct connman_ipconfig *ipv6config;
824 DBG("index %d", index);
826 ipv6config = g_try_new0(struct connman_ipconfig, 1);
827 if (ipv6config == NULL)
830 ipv6config->index = index;
831 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
833 ipv6config->address = connman_ipaddress_alloc();
834 if (ipv6config->address == NULL) {
839 ipv6config->system = connman_ipaddress_alloc();
841 ipv6config->ipv6 = NULL;
843 DBG("ipconfig %p", ipv6config);
849 * connman_ipconfig_create:
851 * Allocate a new ipconfig structure.
853 * Returns: a newly-allocated #connman_ipconfig structure
855 struct connman_ipconfig *connman_ipconfig_create(int index)
857 struct connman_ipconfig *ipconfig;
859 DBG("index %d", index);
861 ipconfig = g_try_new0(struct connman_ipconfig, 1);
862 if (ipconfig == NULL)
865 ipconfig->refcount = 1;
867 ipconfig->index = index;
868 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
870 ipconfig->address = connman_ipaddress_alloc();
871 if (ipconfig->address == NULL) {
876 ipconfig->system = connman_ipaddress_alloc();
878 ipconfig->ipv6 = create_ipv6config(index);
880 DBG("ipconfig %p", ipconfig);
886 * connman_ipconfig_clone:
888 * Clone an ipconfig structure and create new reference.
890 * Returns: a newly-allocated #connman_ipconfig structure
892 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
894 struct connman_ipconfig *ipconfig_clone;
896 DBG("ipconfig %p", ipconfig);
898 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
899 if (ipconfig_clone == NULL)
902 ipconfig_clone->refcount = 1;
904 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
906 ipconfig_clone->index = -1;
908 return ipconfig_clone;
912 * connman_ipconfig_ref:
913 * @ipconfig: ipconfig structure
915 * Increase reference counter of ipconfig
917 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
919 g_atomic_int_inc(&ipconfig->refcount);
924 static void free_ipv6config(struct connman_ipconfig *ipconfig)
926 if (ipconfig == NULL)
929 connman_ipconfig_set_ops(ipconfig, NULL);
930 connman_ipaddress_free(ipconfig->system);
931 connman_ipaddress_free(ipconfig->address);
932 g_free(ipconfig->ipv6);
936 * connman_ipconfig_unref:
937 * @ipconfig: ipconfig structure
939 * Decrease reference counter of ipconfig
941 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
943 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
944 __connman_ipconfig_disable(ipconfig);
946 connman_ipconfig_set_ops(ipconfig, NULL);
948 if (ipconfig->origin != NULL) {
949 connman_ipconfig_unref(ipconfig->origin);
950 ipconfig->origin = NULL;
953 connman_ipaddress_free(ipconfig->system);
954 connman_ipaddress_free(ipconfig->address);
955 free_ipv6config(ipconfig->ipv6);
961 * connman_ipconfig_get_data:
962 * @ipconfig: ipconfig structure
964 * Get private data pointer
966 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
968 return ipconfig->ops_data;
972 * connman_ipconfig_set_data:
973 * @ipconfig: ipconfig structure
974 * @data: data pointer
976 * Set private data pointer
978 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
980 ipconfig->ops_data = data;
984 * connman_ipconfig_get_index:
985 * @ipconfig: ipconfig structure
987 * Get interface index
989 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
991 if (ipconfig == NULL)
994 if (ipconfig->origin != NULL)
995 return ipconfig->origin->index;
997 return ipconfig->index;
1001 * connman_ipconfig_get_ifname:
1002 * @ipconfig: ipconfig structure
1004 * Get interface name
1006 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1008 struct connman_ipdevice *ipdevice;
1010 if (ipconfig == NULL)
1013 if (ipconfig->index < 0)
1016 ipdevice = g_hash_table_lookup(ipdevice_hash,
1017 GINT_TO_POINTER(ipconfig->index));
1018 if (ipdevice == NULL)
1021 return ipdevice->ifname;
1025 * connman_ipconfig_set_ops:
1026 * @ipconfig: ipconfig structure
1027 * @ops: operation callbacks
1029 * Set the operation callbacks
1031 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1032 const struct connman_ipconfig_ops *ops)
1034 ipconfig->ops = ops;
1037 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1038 struct connman_ipconfig *ipconfig)
1040 if (ipconfig == NULL)
1043 return ipconfig->ipv6;
1047 * connman_ipconfig_set_method:
1048 * @ipconfig: ipconfig structure
1049 * @method: configuration method
1051 * Set the configuration method
1053 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1054 enum connman_ipconfig_method method)
1056 ipconfig->method = method;
1061 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1063 if (ipconfig == NULL)
1064 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1066 return ipconfig->method;
1070 * connman_ipconfig_bind:
1071 * @ipconfig: ipconfig structure
1072 * @ipaddress: ipaddress structure
1074 * Bind IP address details to configuration
1076 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1077 struct connman_ipaddress *ipaddress)
1079 struct connman_ipconfig *origin;
1081 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1083 connman_ipaddress_copy(origin->address, ipaddress);
1085 connman_inet_set_address(origin->index, origin->address);
1088 void __connman_ipconfig_set_element_ipv6_gateway(
1089 struct connman_ipconfig *ipconfig,
1090 struct connman_element *element)
1092 element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1096 * FIXME: The element soulution should be removed in the future
1097 * Set IPv4 and IPv6 gateway
1099 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1100 struct connman_element *parent)
1102 struct connman_element *connection;
1104 connection = connman_element_create(NULL);
1106 DBG("ipconfig %p", ipconfig);
1108 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1109 connection->index = ipconfig->index;
1110 connection->ipv4.gateway = ipconfig->address->gateway;
1111 connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1113 if (connman_element_register(connection, parent) < 0)
1114 connman_element_unref(connection);
1119 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1123 switch (ipconfig->method) {
1124 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1125 case CONNMAN_IPCONFIG_METHOD_OFF:
1126 case CONNMAN_IPCONFIG_METHOD_FIXED:
1127 case CONNMAN_IPCONFIG_METHOD_DHCP:
1129 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1130 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1131 return connman_inet_set_address(ipconfig->index,
1133 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1134 return connman_inet_set_ipv6_address(
1135 ipconfig->index, ipconfig->address);
1141 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1145 if (ipconfig == NULL)
1148 DBG("method %d", ipconfig->method);
1150 switch (ipconfig->method) {
1151 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1152 case CONNMAN_IPCONFIG_METHOD_OFF:
1153 case CONNMAN_IPCONFIG_METHOD_FIXED:
1154 case CONNMAN_IPCONFIG_METHOD_DHCP:
1156 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1157 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1158 return connman_inet_clear_address(ipconfig->index);
1159 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1160 return connman_inet_clear_ipv6_address(
1162 ipconfig->address->local,
1163 ipconfig->address->prefixlen);
1169 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1171 struct connman_ipdevice *ipdevice;
1172 gboolean up = FALSE, down = FALSE;
1173 gboolean lower_up = FALSE, lower_down = FALSE;
1175 DBG("ipconfig %p", ipconfig);
1177 if (ipconfig == NULL || ipconfig->index < 0)
1180 ipdevice = g_hash_table_lookup(ipdevice_hash,
1181 GINT_TO_POINTER(ipconfig->index));
1182 if (ipdevice == NULL)
1185 if (ipdevice->config == ipconfig)
1188 if (ipdevice->config != NULL) {
1189 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1191 connman_ipaddress_clear(ipdevice->config->system);
1193 connman_ipconfig_unref(ipdevice->config);
1196 ipdevice->config = connman_ipconfig_ref(ipconfig);
1198 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1200 if (ipdevice->flags & IFF_UP)
1205 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1206 (IFF_RUNNING | IFF_LOWER_UP))
1208 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1211 if (up == TRUE && ipconfig->ops->up)
1212 ipconfig->ops->up(ipconfig);
1213 if (lower_up == TRUE && ipconfig->ops->lower_up)
1214 ipconfig->ops->lower_up(ipconfig);
1216 if (lower_down == TRUE && ipconfig->ops->lower_down)
1217 ipconfig->ops->lower_down(ipconfig);
1218 if (down == TRUE && ipconfig->ops->down)
1219 ipconfig->ops->down(ipconfig);
1224 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1226 struct connman_ipdevice *ipdevice;
1228 DBG("ipconfig %p", ipconfig);
1230 if (ipconfig == NULL || ipconfig->index < 0)
1233 ipdevice = g_hash_table_lookup(ipdevice_hash,
1234 GINT_TO_POINTER(ipconfig->index));
1235 if (ipdevice == NULL)
1238 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1241 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1243 connman_ipaddress_clear(ipdevice->config->system);
1244 connman_ipaddress_clear(ipdevice->config->ipv6->system);
1246 connman_ipconfig_unref(ipdevice->config);
1247 ipdevice->config = NULL;
1252 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1255 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1257 case CONNMAN_IPCONFIG_METHOD_OFF:
1259 case CONNMAN_IPCONFIG_METHOD_FIXED:
1261 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1263 case CONNMAN_IPCONFIG_METHOD_DHCP:
1270 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1272 if (g_strcmp0(method, "off") == 0)
1273 return CONNMAN_IPCONFIG_METHOD_OFF;
1274 else if (g_strcmp0(method, "fixed") == 0)
1275 return CONNMAN_IPCONFIG_METHOD_FIXED;
1276 else if (g_strcmp0(method, "manual") == 0)
1277 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1278 else if (g_strcmp0(method, "dhcp") == 0)
1279 return CONNMAN_IPCONFIG_METHOD_DHCP;
1281 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1284 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1285 DBusMessageIter *iter)
1291 str = __connman_ipconfig_method2string(ipconfig->method);
1295 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1297 if (ipconfig->system == NULL)
1300 if (ipconfig->system->local != NULL) {
1302 struct in_addr netmask;
1305 connman_dbus_dict_append_basic(iter, "Address",
1306 DBUS_TYPE_STRING, &ipconfig->system->local);
1308 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1309 netmask.s_addr = htonl(addr);
1310 mask = inet_ntoa(netmask);
1311 connman_dbus_dict_append_basic(iter, "Netmask",
1312 DBUS_TYPE_STRING, &mask);
1315 if (ipconfig->system->gateway != NULL)
1316 connman_dbus_dict_append_basic(iter, "Gateway",
1317 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1320 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1321 DBusMessageIter *iter)
1327 str = __connman_ipconfig_method2string(ipconfig->method);
1331 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1333 if (ipconfig->system == NULL)
1336 if (ipconfig->system->local != NULL) {
1337 connman_dbus_dict_append_basic(iter, "Address",
1338 DBUS_TYPE_STRING, &ipconfig->system->local);
1339 connman_dbus_dict_append_basic(iter, "PrefixLength",
1341 &ipconfig->system->prefixlen);
1344 if (ipconfig->system->gateway != NULL)
1345 connman_dbus_dict_append_basic(iter, "Gateway",
1346 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1349 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1350 DBusMessageIter *iter)
1356 str = __connman_ipconfig_method2string(ipconfig->method);
1360 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1362 switch (ipconfig->method) {
1363 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1364 case CONNMAN_IPCONFIG_METHOD_OFF:
1365 case CONNMAN_IPCONFIG_METHOD_DHCP:
1367 case CONNMAN_IPCONFIG_METHOD_FIXED:
1368 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1372 if (ipconfig->address == NULL)
1375 if (ipconfig->address->local != NULL) {
1376 connman_dbus_dict_append_basic(iter, "Address",
1377 DBUS_TYPE_STRING, &ipconfig->address->local);
1378 connman_dbus_dict_append_basic(iter, "PrefixLength",
1380 &ipconfig->address->prefixlen);
1383 if (ipconfig->address->gateway != NULL)
1384 connman_dbus_dict_append_basic(iter, "Gateway",
1385 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1388 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1389 DBusMessageIter *iter)
1395 str = __connman_ipconfig_method2string(ipconfig->method);
1399 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1401 switch (ipconfig->method) {
1402 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1403 case CONNMAN_IPCONFIG_METHOD_OFF:
1404 case CONNMAN_IPCONFIG_METHOD_FIXED:
1405 case CONNMAN_IPCONFIG_METHOD_DHCP:
1407 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1411 if (ipconfig->address == NULL)
1414 if (ipconfig->address->local != NULL) {
1416 struct in_addr netmask;
1419 connman_dbus_dict_append_basic(iter, "Address",
1420 DBUS_TYPE_STRING, &ipconfig->address->local);
1422 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1423 netmask.s_addr = htonl(addr);
1424 mask = inet_ntoa(netmask);
1425 connman_dbus_dict_append_basic(iter, "Netmask",
1426 DBUS_TYPE_STRING, &mask);
1429 if (ipconfig->address->gateway != NULL)
1430 connman_dbus_dict_append_basic(iter, "Gateway",
1431 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1434 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1435 enum connman_ipconfig_type type, DBusMessageIter *array)
1437 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1438 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1439 *prefix_length_string = NULL;
1440 int prefix_length = 0;
1441 DBusMessageIter dict;
1443 DBG("ipconfig %p type %d", ipconfig, type);
1445 if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1446 type != CONNMAN_IPCONFIG_TYPE_IPV6)
1449 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1452 dbus_message_iter_recurse(array, &dict);
1454 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1455 DBusMessageIter entry;
1459 dbus_message_iter_recurse(&dict, &entry);
1461 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1464 dbus_message_iter_get_basic(&entry, &key);
1465 dbus_message_iter_next(&entry);
1467 type = dbus_message_iter_get_arg_type(&entry);
1469 if (g_str_equal(key, "Method") == TRUE) {
1472 if (type != DBUS_TYPE_STRING)
1475 dbus_message_iter_get_basic(&entry, &str);
1476 method = __connman_ipconfig_string2method(str);
1477 } else if (g_str_equal(key, "Address") == TRUE) {
1478 if (type != DBUS_TYPE_STRING)
1481 dbus_message_iter_get_basic(&entry, &address);
1482 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1483 if (type != DBUS_TYPE_STRING)
1486 dbus_message_iter_get_basic(&entry,
1487 &prefix_length_string);
1489 prefix_length = atoi(prefix_length_string);
1490 if (prefix_length < 0 || prefix_length > 128)
1493 } else if (g_str_equal(key, "Netmask") == TRUE) {
1494 if (type != DBUS_TYPE_STRING)
1497 dbus_message_iter_get_basic(&entry, &netmask);
1498 } else if (g_str_equal(key, "Gateway") == TRUE) {
1499 if (type != DBUS_TYPE_STRING)
1502 dbus_message_iter_get_basic(&entry, &gateway);
1504 dbus_message_iter_next(&dict);
1507 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1508 method, address, netmask, gateway, prefix_length);
1511 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1512 case CONNMAN_IPCONFIG_METHOD_OFF:
1513 case CONNMAN_IPCONFIG_METHOD_FIXED:
1516 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1517 if (address == NULL)
1520 ipconfig->method = method;
1522 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1523 connman_ipaddress_set_ipv4(ipconfig->address,
1524 address, netmask, gateway);
1526 return connman_ipaddress_set_ipv6(
1527 ipconfig->address, address,
1528 gateway, prefix_length);
1531 case CONNMAN_IPCONFIG_METHOD_DHCP:
1532 if (ipconfig->method == method)
1535 ipconfig->method = method;
1542 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1543 DBusMessageIter *iter)
1545 const char *method = "direct";
1547 connman_dbus_dict_append_basic(iter, "Method",
1548 DBUS_TYPE_STRING, &method);
1551 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1552 DBusMessageIter *iter)
1554 struct connman_ipdevice *ipdevice;
1555 const char *method = "auto";
1557 connman_dbus_dict_append_basic(iter, "Method",
1558 DBUS_TYPE_STRING, &method);
1560 ipdevice = g_hash_table_lookup(ipdevice_hash,
1561 GINT_TO_POINTER(ipconfig->index));
1562 if (ipdevice == NULL)
1565 if (ipdevice->ifname != NULL)
1566 connman_dbus_dict_append_basic(iter, "Interface",
1567 DBUS_TYPE_STRING, &ipdevice->ifname);
1569 if (ipdevice->address != NULL)
1570 connman_dbus_dict_append_basic(iter, "Address",
1571 DBUS_TYPE_STRING, &ipdevice->address);
1573 if (ipdevice->mtu > 0)
1574 connman_dbus_dict_append_basic(iter, "MTU",
1575 DBUS_TYPE_UINT16, &ipdevice->mtu);
1578 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1579 GKeyFile *keyfile, const char *identifier, const char *prefix)
1584 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1586 key = g_strdup_printf("%smethod", prefix);
1587 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1589 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1591 ipconfig->method = __connman_ipconfig_string2method(method);
1594 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1595 ipconfig->address->prefixlen = g_key_file_get_integer(
1596 keyfile, identifier, key, NULL);
1599 key = g_strdup_printf("%slocal_address", prefix);
1600 ipconfig->address->local = g_key_file_get_string(
1601 keyfile, identifier, key, NULL);
1604 key = g_strdup_printf("%speer_address", prefix);
1605 ipconfig->address->peer = g_key_file_get_string(
1606 keyfile, identifier, key, NULL);
1609 key = g_strdup_printf("%sbroadcast_address", prefix);
1610 ipconfig->address->broadcast = g_key_file_get_string(
1611 keyfile, identifier, key, NULL);
1614 key = g_strdup_printf("%sgateway", prefix);
1615 ipconfig->address->gateway = g_key_file_get_string(
1616 keyfile, identifier, key, NULL);
1622 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1623 GKeyFile *keyfile, const char *identifier, const char *prefix)
1628 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1630 method = __connman_ipconfig_method2string(ipconfig->method);
1632 key = g_strdup_printf("%smethod", prefix);
1633 g_key_file_set_string(keyfile, identifier, key, method);
1636 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1637 g_key_file_set_integer(keyfile, identifier,
1638 key, ipconfig->address->prefixlen);
1641 key = g_strdup_printf("%slocal_address", prefix);
1642 if (ipconfig->address->local != NULL)
1643 g_key_file_set_string(keyfile, identifier,
1644 key, ipconfig->address->local);
1647 key = g_strdup_printf("%speer_address", prefix);
1648 if (ipconfig->address->peer != NULL)
1649 g_key_file_set_string(keyfile, identifier,
1650 key, ipconfig->address->peer);
1653 key = g_strdup_printf("%sbroadcast_address", prefix);
1654 if (ipconfig->address->broadcast != NULL)
1655 g_key_file_set_string(keyfile, identifier,
1656 key, ipconfig->address->broadcast);
1659 key = g_strdup_printf("%sgateway", prefix);
1660 if (ipconfig->address->gateway != NULL)
1661 g_key_file_set_string(keyfile, identifier,
1662 key, ipconfig->address->gateway);
1668 int __connman_ipconfig_init(void)
1672 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1673 NULL, free_ipdevice);
1678 void __connman_ipconfig_cleanup(void)
1682 g_hash_table_destroy(ipdevice_hash);
1683 ipdevice_hash = NULL;