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, int family, 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 if (family == AF_INET6 && ipdevice->config->ipv6 != NULL)
614 connman_ipaddress_copy(ipdevice->config->ipv6->system,
617 connman_ipaddress_copy(ipdevice->config->system,
621 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
624 if (g_slist_length(ipdevice->address_list) > 1)
627 for (list = g_list_first(ipconfig_list); list;
628 list = g_list_next(list)) {
629 struct connman_ipconfig *ipconfig = list->data;
631 if (index != ipconfig->index)
634 if (ipconfig->ops == NULL)
637 if (ipconfig->ops->ip_bound)
638 ipconfig->ops->ip_bound(ipconfig);
642 void __connman_ipconfig_deladdr(int index, int family, const char *label,
643 unsigned char prefixlen, const char *address)
645 struct connman_ipdevice *ipdevice;
646 struct connman_ipaddress *ipaddress;
649 DBG("index %d", index);
651 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
652 if (ipdevice == NULL)
655 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
656 if (ipaddress == NULL)
659 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
662 connman_ipaddress_free(ipaddress);
664 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
665 address, prefixlen, label);
667 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
670 if (g_slist_length(ipdevice->address_list) > 0)
673 for (list = g_list_first(ipconfig_list); list;
674 list = g_list_next(list)) {
675 struct connman_ipconfig *ipconfig = list->data;
677 if (index != ipconfig->index)
680 if (ipconfig->ops == NULL)
683 if (ipconfig->ops->ip_release)
684 ipconfig->ops->ip_release(ipconfig);
688 void __connman_ipconfig_newroute(int index, unsigned char scope,
689 const char *dst, const char *gateway)
691 struct connman_ipdevice *ipdevice;
693 DBG("index %d", index);
695 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
696 if (ipdevice == NULL)
699 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
702 g_free(ipdevice->gateway);
703 ipdevice->gateway = g_strdup(gateway);
705 if (ipdevice->config != NULL &&
706 ipdevice->config->system != NULL) {
707 g_free(ipdevice->config->system->gateway);
708 ipdevice->config->system->gateway = g_strdup(gateway);
711 for (list = ipdevice->address_list; list; list = list->next) {
712 struct connman_ipaddress *ipaddress = list->data;
714 g_free(ipaddress->gateway);
715 ipaddress->gateway = g_strdup(gateway);
719 connman_info("%s {add} route %s gw %s scope %u <%s>",
720 ipdevice->ifname, dst, gateway,
721 scope, scope2str(scope));
724 void __connman_ipconfig_delroute(int index, unsigned char scope,
725 const char *dst, const char *gateway)
727 struct connman_ipdevice *ipdevice;
729 DBG("index %d", index);
731 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
732 if (ipdevice == NULL)
735 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
738 g_free(ipdevice->gateway);
739 ipdevice->gateway = NULL;
741 if (ipdevice->config != NULL &&
742 ipdevice->config->system != NULL) {
743 g_free(ipdevice->config->system->gateway);
744 ipdevice->config->system->gateway = NULL;
747 for (list = ipdevice->address_list; list; list = list->next) {
748 struct connman_ipaddress *ipaddress = list->data;
750 g_free(ipaddress->gateway);
751 ipaddress->gateway = NULL;
755 connman_info("%s {del} route %s gw %s scope %u <%s>",
756 ipdevice->ifname, dst, gateway,
757 scope, scope2str(scope));
760 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
765 keys = g_hash_table_get_keys(ipdevice_hash);
769 for (list = g_list_first(keys); list; list = g_list_next(list)) {
770 int index = GPOINTER_TO_INT(list->data);
772 function(index, user_data);
778 unsigned short __connman_ipconfig_get_type(int index)
780 struct connman_ipdevice *ipdevice;
782 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
783 if (ipdevice == NULL)
786 return ipdevice->type;
789 unsigned int __connman_ipconfig_get_flags(int index)
791 struct connman_ipdevice *ipdevice;
793 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
794 if (ipdevice == NULL)
797 return ipdevice->flags;
800 const char *__connman_ipconfig_get_gateway(int index)
802 struct connman_ipdevice *ipdevice;
804 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
805 if (ipdevice == NULL)
808 if (ipdevice->gateway != NULL)
809 return ipdevice->gateway;
811 if (ipdevice->config != NULL &&
812 ipdevice->config->address != NULL)
813 return ipdevice->config->address->gateway;
818 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
820 ipconfig->index = index;
822 if (ipconfig->ipv6 != NULL)
823 ipconfig->ipv6->index = index;
826 static struct connman_ipconfig *create_ipv6config(int index)
828 struct connman_ipconfig *ipv6config;
830 DBG("index %d", index);
832 ipv6config = g_try_new0(struct connman_ipconfig, 1);
833 if (ipv6config == NULL)
836 ipv6config->index = index;
837 ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
839 ipv6config->address = connman_ipaddress_alloc();
840 if (ipv6config->address == NULL) {
845 ipv6config->system = connman_ipaddress_alloc();
847 ipv6config->ipv6 = NULL;
849 DBG("ipconfig %p", ipv6config);
855 * connman_ipconfig_create:
857 * Allocate a new ipconfig structure.
859 * Returns: a newly-allocated #connman_ipconfig structure
861 struct connman_ipconfig *connman_ipconfig_create(int index)
863 struct connman_ipconfig *ipconfig;
865 DBG("index %d", index);
867 ipconfig = g_try_new0(struct connman_ipconfig, 1);
868 if (ipconfig == NULL)
871 ipconfig->refcount = 1;
873 ipconfig->index = index;
874 ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
876 ipconfig->address = connman_ipaddress_alloc();
877 if (ipconfig->address == NULL) {
882 ipconfig->system = connman_ipaddress_alloc();
884 ipconfig->ipv6 = create_ipv6config(index);
886 DBG("ipconfig %p", ipconfig);
892 * connman_ipconfig_clone:
894 * Clone an ipconfig structure and create new reference.
896 * Returns: a newly-allocated #connman_ipconfig structure
898 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
900 struct connman_ipconfig *ipconfig_clone;
902 DBG("ipconfig %p", ipconfig);
904 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
905 if (ipconfig_clone == NULL)
908 ipconfig_clone->refcount = 1;
910 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
912 ipconfig_clone->index = -1;
914 return ipconfig_clone;
918 * connman_ipconfig_ref:
919 * @ipconfig: ipconfig structure
921 * Increase reference counter of ipconfig
923 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
925 g_atomic_int_inc(&ipconfig->refcount);
930 static void free_ipv6config(struct connman_ipconfig *ipconfig)
932 if (ipconfig == NULL)
935 connman_ipconfig_set_ops(ipconfig, NULL);
936 connman_ipaddress_free(ipconfig->system);
937 connman_ipaddress_free(ipconfig->address);
938 g_free(ipconfig->ipv6);
942 * connman_ipconfig_unref:
943 * @ipconfig: ipconfig structure
945 * Decrease reference counter of ipconfig
947 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
949 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
950 __connman_ipconfig_disable(ipconfig);
952 connman_ipconfig_set_ops(ipconfig, NULL);
954 if (ipconfig->origin != NULL) {
955 connman_ipconfig_unref(ipconfig->origin);
956 ipconfig->origin = NULL;
959 connman_ipaddress_free(ipconfig->system);
960 connman_ipaddress_free(ipconfig->address);
961 free_ipv6config(ipconfig->ipv6);
967 * connman_ipconfig_get_data:
968 * @ipconfig: ipconfig structure
970 * Get private data pointer
972 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
974 return ipconfig->ops_data;
978 * connman_ipconfig_set_data:
979 * @ipconfig: ipconfig structure
980 * @data: data pointer
982 * Set private data pointer
984 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
986 ipconfig->ops_data = data;
990 * connman_ipconfig_get_index:
991 * @ipconfig: ipconfig structure
993 * Get interface index
995 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
997 if (ipconfig == NULL)
1000 if (ipconfig->origin != NULL)
1001 return ipconfig->origin->index;
1003 return ipconfig->index;
1007 * connman_ipconfig_get_ifname:
1008 * @ipconfig: ipconfig structure
1010 * Get interface name
1012 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1014 struct connman_ipdevice *ipdevice;
1016 if (ipconfig == NULL)
1019 if (ipconfig->index < 0)
1022 ipdevice = g_hash_table_lookup(ipdevice_hash,
1023 GINT_TO_POINTER(ipconfig->index));
1024 if (ipdevice == NULL)
1027 return ipdevice->ifname;
1031 * connman_ipconfig_set_ops:
1032 * @ipconfig: ipconfig structure
1033 * @ops: operation callbacks
1035 * Set the operation callbacks
1037 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1038 const struct connman_ipconfig_ops *ops)
1040 ipconfig->ops = ops;
1043 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1044 struct connman_ipconfig *ipconfig)
1046 if (ipconfig == NULL)
1049 return ipconfig->ipv6;
1053 * connman_ipconfig_set_method:
1054 * @ipconfig: ipconfig structure
1055 * @method: configuration method
1057 * Set the configuration method
1059 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1060 enum connman_ipconfig_method method)
1062 ipconfig->method = method;
1067 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1069 if (ipconfig == NULL)
1070 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1072 return ipconfig->method;
1076 * connman_ipconfig_bind:
1077 * @ipconfig: ipconfig structure
1078 * @ipaddress: ipaddress structure
1080 * Bind IP address details to configuration
1082 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1083 struct connman_ipaddress *ipaddress)
1085 struct connman_ipconfig *origin;
1087 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1089 connman_ipaddress_copy(origin->address, ipaddress);
1091 connman_inet_set_address(origin->index, origin->address);
1094 void __connman_ipconfig_set_element_ipv6_gateway(
1095 struct connman_ipconfig *ipconfig,
1096 struct connman_element *element)
1098 element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1102 * FIXME: The element soulution should be removed in the future
1103 * Set IPv4 and IPv6 gateway
1105 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1106 struct connman_element *parent)
1108 struct connman_element *connection;
1110 connection = connman_element_create(NULL);
1112 DBG("ipconfig %p", ipconfig);
1114 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
1115 connection->index = ipconfig->index;
1116 connection->ipv4.gateway = ipconfig->address->gateway;
1117 connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1119 if (connman_element_register(connection, parent) < 0)
1120 connman_element_unref(connection);
1125 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1129 switch (ipconfig->method) {
1130 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1131 case CONNMAN_IPCONFIG_METHOD_OFF:
1132 case CONNMAN_IPCONFIG_METHOD_FIXED:
1133 case CONNMAN_IPCONFIG_METHOD_DHCP:
1135 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1136 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1137 return connman_inet_set_address(ipconfig->index,
1139 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1140 return connman_inet_set_ipv6_address(
1141 ipconfig->index, ipconfig->address);
1147 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1151 if (ipconfig == NULL)
1154 DBG("method %d", ipconfig->method);
1156 switch (ipconfig->method) {
1157 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1158 case CONNMAN_IPCONFIG_METHOD_OFF:
1159 case CONNMAN_IPCONFIG_METHOD_FIXED:
1160 case CONNMAN_IPCONFIG_METHOD_DHCP:
1162 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1163 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1164 return connman_inet_clear_address(ipconfig->index);
1165 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1166 return connman_inet_clear_ipv6_address(
1168 ipconfig->address->local,
1169 ipconfig->address->prefixlen);
1175 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1177 struct connman_ipdevice *ipdevice;
1178 gboolean up = FALSE, down = FALSE;
1179 gboolean lower_up = FALSE, lower_down = FALSE;
1181 DBG("ipconfig %p", ipconfig);
1183 if (ipconfig == NULL || ipconfig->index < 0)
1186 ipdevice = g_hash_table_lookup(ipdevice_hash,
1187 GINT_TO_POINTER(ipconfig->index));
1188 if (ipdevice == NULL)
1191 if (ipdevice->config == ipconfig)
1194 if (ipdevice->config != NULL) {
1195 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1197 connman_ipaddress_clear(ipdevice->config->system);
1199 connman_ipconfig_unref(ipdevice->config);
1202 ipdevice->config = connman_ipconfig_ref(ipconfig);
1204 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1206 if (ipdevice->flags & IFF_UP)
1211 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1212 (IFF_RUNNING | IFF_LOWER_UP))
1214 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1217 if (up == TRUE && ipconfig->ops->up)
1218 ipconfig->ops->up(ipconfig);
1219 if (lower_up == TRUE && ipconfig->ops->lower_up)
1220 ipconfig->ops->lower_up(ipconfig);
1222 if (lower_down == TRUE && ipconfig->ops->lower_down)
1223 ipconfig->ops->lower_down(ipconfig);
1224 if (down == TRUE && ipconfig->ops->down)
1225 ipconfig->ops->down(ipconfig);
1230 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1232 struct connman_ipdevice *ipdevice;
1234 DBG("ipconfig %p", ipconfig);
1236 if (ipconfig == NULL || ipconfig->index < 0)
1239 ipdevice = g_hash_table_lookup(ipdevice_hash,
1240 GINT_TO_POINTER(ipconfig->index));
1241 if (ipdevice == NULL)
1244 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1247 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1249 connman_ipaddress_clear(ipdevice->config->system);
1250 connman_ipaddress_clear(ipdevice->config->ipv6->system);
1252 connman_ipconfig_unref(ipdevice->config);
1253 ipdevice->config = NULL;
1258 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1261 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1263 case CONNMAN_IPCONFIG_METHOD_OFF:
1265 case CONNMAN_IPCONFIG_METHOD_FIXED:
1267 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1269 case CONNMAN_IPCONFIG_METHOD_DHCP:
1276 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1278 if (g_strcmp0(method, "off") == 0)
1279 return CONNMAN_IPCONFIG_METHOD_OFF;
1280 else if (g_strcmp0(method, "fixed") == 0)
1281 return CONNMAN_IPCONFIG_METHOD_FIXED;
1282 else if (g_strcmp0(method, "manual") == 0)
1283 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1284 else if (g_strcmp0(method, "dhcp") == 0)
1285 return CONNMAN_IPCONFIG_METHOD_DHCP;
1287 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1290 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1291 DBusMessageIter *iter)
1297 str = __connman_ipconfig_method2string(ipconfig->method);
1301 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1303 if (ipconfig->system == NULL)
1306 if (ipconfig->system->local != NULL) {
1308 struct in_addr netmask;
1311 connman_dbus_dict_append_basic(iter, "Address",
1312 DBUS_TYPE_STRING, &ipconfig->system->local);
1314 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1315 netmask.s_addr = htonl(addr);
1316 mask = inet_ntoa(netmask);
1317 connman_dbus_dict_append_basic(iter, "Netmask",
1318 DBUS_TYPE_STRING, &mask);
1321 if (ipconfig->system->gateway != NULL)
1322 connman_dbus_dict_append_basic(iter, "Gateway",
1323 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1326 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1327 DBusMessageIter *iter)
1333 str = __connman_ipconfig_method2string(ipconfig->method);
1337 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1339 if (ipconfig->system == NULL)
1342 if (ipconfig->system->local != NULL) {
1343 connman_dbus_dict_append_basic(iter, "Address",
1344 DBUS_TYPE_STRING, &ipconfig->system->local);
1345 connman_dbus_dict_append_basic(iter, "PrefixLength",
1347 &ipconfig->system->prefixlen);
1350 if (ipconfig->system->gateway != NULL)
1351 connman_dbus_dict_append_basic(iter, "Gateway",
1352 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1355 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1356 DBusMessageIter *iter)
1362 str = __connman_ipconfig_method2string(ipconfig->method);
1366 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1368 switch (ipconfig->method) {
1369 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1370 case CONNMAN_IPCONFIG_METHOD_OFF:
1371 case CONNMAN_IPCONFIG_METHOD_DHCP:
1373 case CONNMAN_IPCONFIG_METHOD_FIXED:
1374 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1378 if (ipconfig->address == NULL)
1381 if (ipconfig->address->local != NULL) {
1382 connman_dbus_dict_append_basic(iter, "Address",
1383 DBUS_TYPE_STRING, &ipconfig->address->local);
1384 connman_dbus_dict_append_basic(iter, "PrefixLength",
1386 &ipconfig->address->prefixlen);
1389 if (ipconfig->address->gateway != NULL)
1390 connman_dbus_dict_append_basic(iter, "Gateway",
1391 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1394 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1395 DBusMessageIter *iter)
1401 str = __connman_ipconfig_method2string(ipconfig->method);
1405 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1407 switch (ipconfig->method) {
1408 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1409 case CONNMAN_IPCONFIG_METHOD_OFF:
1410 case CONNMAN_IPCONFIG_METHOD_FIXED:
1411 case CONNMAN_IPCONFIG_METHOD_DHCP:
1413 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1417 if (ipconfig->address == NULL)
1420 if (ipconfig->address->local != NULL) {
1422 struct in_addr netmask;
1425 connman_dbus_dict_append_basic(iter, "Address",
1426 DBUS_TYPE_STRING, &ipconfig->address->local);
1428 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1429 netmask.s_addr = htonl(addr);
1430 mask = inet_ntoa(netmask);
1431 connman_dbus_dict_append_basic(iter, "Netmask",
1432 DBUS_TYPE_STRING, &mask);
1435 if (ipconfig->address->gateway != NULL)
1436 connman_dbus_dict_append_basic(iter, "Gateway",
1437 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1440 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1441 enum connman_ipconfig_type type, DBusMessageIter *array)
1443 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1444 const char *address = NULL, *netmask = NULL, *gateway = NULL,
1445 *prefix_length_string = NULL;
1446 int prefix_length = 0;
1447 DBusMessageIter dict;
1449 DBG("ipconfig %p type %d", ipconfig, type);
1451 if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1452 type != CONNMAN_IPCONFIG_TYPE_IPV6)
1455 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1458 dbus_message_iter_recurse(array, &dict);
1460 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1461 DBusMessageIter entry;
1465 dbus_message_iter_recurse(&dict, &entry);
1467 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1470 dbus_message_iter_get_basic(&entry, &key);
1471 dbus_message_iter_next(&entry);
1473 type = dbus_message_iter_get_arg_type(&entry);
1475 if (g_str_equal(key, "Method") == TRUE) {
1478 if (type != DBUS_TYPE_STRING)
1481 dbus_message_iter_get_basic(&entry, &str);
1482 method = __connman_ipconfig_string2method(str);
1483 } else if (g_str_equal(key, "Address") == TRUE) {
1484 if (type != DBUS_TYPE_STRING)
1487 dbus_message_iter_get_basic(&entry, &address);
1488 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1489 if (type != DBUS_TYPE_STRING)
1492 dbus_message_iter_get_basic(&entry,
1493 &prefix_length_string);
1495 prefix_length = atoi(prefix_length_string);
1496 if (prefix_length < 0 || prefix_length > 128)
1499 } else if (g_str_equal(key, "Netmask") == TRUE) {
1500 if (type != DBUS_TYPE_STRING)
1503 dbus_message_iter_get_basic(&entry, &netmask);
1504 } else if (g_str_equal(key, "Gateway") == TRUE) {
1505 if (type != DBUS_TYPE_STRING)
1508 dbus_message_iter_get_basic(&entry, &gateway);
1510 dbus_message_iter_next(&dict);
1513 DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1514 method, address, netmask, gateway, prefix_length);
1517 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1518 case CONNMAN_IPCONFIG_METHOD_OFF:
1519 case CONNMAN_IPCONFIG_METHOD_FIXED:
1522 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1523 if (address == NULL)
1526 ipconfig->method = method;
1528 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1529 connman_ipaddress_set_ipv4(ipconfig->address,
1530 address, netmask, gateway);
1532 return connman_ipaddress_set_ipv6(
1533 ipconfig->address, address,
1534 gateway, prefix_length);
1537 case CONNMAN_IPCONFIG_METHOD_DHCP:
1538 if (ipconfig->method == method)
1541 ipconfig->method = method;
1548 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1549 DBusMessageIter *iter)
1551 const char *method = "direct";
1553 connman_dbus_dict_append_basic(iter, "Method",
1554 DBUS_TYPE_STRING, &method);
1557 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1558 DBusMessageIter *iter)
1560 struct connman_ipdevice *ipdevice;
1561 const char *method = "auto";
1563 connman_dbus_dict_append_basic(iter, "Method",
1564 DBUS_TYPE_STRING, &method);
1566 ipdevice = g_hash_table_lookup(ipdevice_hash,
1567 GINT_TO_POINTER(ipconfig->index));
1568 if (ipdevice == NULL)
1571 if (ipdevice->ifname != NULL)
1572 connman_dbus_dict_append_basic(iter, "Interface",
1573 DBUS_TYPE_STRING, &ipdevice->ifname);
1575 if (ipdevice->address != NULL)
1576 connman_dbus_dict_append_basic(iter, "Address",
1577 DBUS_TYPE_STRING, &ipdevice->address);
1579 if (ipdevice->mtu > 0)
1580 connman_dbus_dict_append_basic(iter, "MTU",
1581 DBUS_TYPE_UINT16, &ipdevice->mtu);
1584 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1585 GKeyFile *keyfile, const char *identifier, const char *prefix)
1590 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1592 key = g_strdup_printf("%smethod", prefix);
1593 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1595 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1597 ipconfig->method = __connman_ipconfig_string2method(method);
1600 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1601 ipconfig->address->prefixlen = g_key_file_get_integer(
1602 keyfile, identifier, key, NULL);
1605 key = g_strdup_printf("%slocal_address", prefix);
1606 ipconfig->address->local = g_key_file_get_string(
1607 keyfile, identifier, key, NULL);
1610 key = g_strdup_printf("%speer_address", prefix);
1611 ipconfig->address->peer = g_key_file_get_string(
1612 keyfile, identifier, key, NULL);
1615 key = g_strdup_printf("%sbroadcast_address", prefix);
1616 ipconfig->address->broadcast = g_key_file_get_string(
1617 keyfile, identifier, key, NULL);
1620 key = g_strdup_printf("%sgateway", prefix);
1621 ipconfig->address->gateway = g_key_file_get_string(
1622 keyfile, identifier, key, NULL);
1628 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1629 GKeyFile *keyfile, const char *identifier, const char *prefix)
1634 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1636 method = __connman_ipconfig_method2string(ipconfig->method);
1638 key = g_strdup_printf("%smethod", prefix);
1639 g_key_file_set_string(keyfile, identifier, key, method);
1642 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1643 g_key_file_set_integer(keyfile, identifier,
1644 key, ipconfig->address->prefixlen);
1647 key = g_strdup_printf("%slocal_address", prefix);
1648 if (ipconfig->address->local != NULL)
1649 g_key_file_set_string(keyfile, identifier,
1650 key, ipconfig->address->local);
1653 key = g_strdup_printf("%speer_address", prefix);
1654 if (ipconfig->address->peer != NULL)
1655 g_key_file_set_string(keyfile, identifier,
1656 key, ipconfig->address->peer);
1659 key = g_strdup_printf("%sbroadcast_address", prefix);
1660 if (ipconfig->address->broadcast != NULL)
1661 g_key_file_set_string(keyfile, identifier,
1662 key, ipconfig->address->broadcast);
1665 key = g_strdup_printf("%sgateway", prefix);
1666 if (ipconfig->address->gateway != NULL)
1667 g_key_file_set_string(keyfile, identifier,
1668 key, ipconfig->address->gateway);
1674 int __connman_ipconfig_init(void)
1678 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1679 NULL, free_ipdevice);
1684 void __connman_ipconfig_cleanup(void)
1688 g_hash_table_destroy(ipdevice_hash);
1689 ipdevice_hash = NULL;