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>
31 #define IFF_LOWER_UP 0x10000
38 struct connman_ipconfig {
42 struct connman_ipconfig *origin;
44 const struct connman_ipconfig_ops *ops;
47 enum connman_ipconfig_method method;
48 struct connman_ipaddress *address;
49 struct connman_ipaddress *system;
52 struct connman_ipdevice {
65 struct connman_ipconfig *config;
67 struct connman_ipconfig_driver *driver;
68 struct connman_ipconfig *driver_config;
71 static GHashTable *ipdevice_hash = NULL;
72 static GList *ipconfig_list = NULL;
74 struct connman_ipaddress *connman_ipaddress_alloc(void)
76 struct connman_ipaddress *ipaddress;
78 ipaddress = g_try_new0(struct connman_ipaddress, 1);
79 if (ipaddress == NULL)
85 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
87 if (ipaddress == NULL)
90 g_free(ipaddress->broadcast);
91 g_free(ipaddress->peer);
92 g_free(ipaddress->local);
93 g_free(ipaddress->gateway);
97 static unsigned char netmask2prefixlen(const char *netmask)
99 unsigned char bits = 0;
100 in_addr_t mask = inet_network(netmask);
101 in_addr_t host = ~mask;
103 /* a valid netmask must be 2^n - 1 */
104 if ((host & (host + 1)) != 0)
107 for (; mask; mask <<= 1)
113 void connman_ipaddress_set(struct connman_ipaddress *ipaddress,
114 const char *address, const char *netmask, const char *gateway)
116 if (ipaddress == NULL)
120 ipaddress->prefixlen = netmask2prefixlen(netmask);
122 ipaddress->prefixlen = 32;
124 g_free(ipaddress->local);
125 ipaddress->local = g_strdup(address);
127 g_free(ipaddress->gateway);
128 ipaddress->gateway = g_strdup(gateway);
131 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
133 if (ipaddress == NULL)
136 ipaddress->prefixlen = 0;
138 g_free(ipaddress->local);
139 ipaddress->local = NULL;
141 g_free(ipaddress->peer);
142 ipaddress->peer = NULL;
144 g_free(ipaddress->broadcast);
145 ipaddress->broadcast = NULL;
147 g_free(ipaddress->gateway);
148 ipaddress->gateway = NULL;
151 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
152 struct connman_ipaddress *source)
154 if (ipaddress == NULL || source == NULL)
157 ipaddress->prefixlen = source->prefixlen;
159 g_free(ipaddress->local);
160 ipaddress->local = g_strdup(source->local);
162 g_free(ipaddress->peer);
163 ipaddress->peer = g_strdup(source->peer);
165 g_free(ipaddress->broadcast);
166 ipaddress->broadcast = g_strdup(source->broadcast);
168 g_free(ipaddress->gateway);
169 ipaddress->gateway = g_strdup(source->gateway);
172 static void free_address_list(struct connman_ipdevice *ipdevice)
176 for (list = ipdevice->address_list; list; list = list->next) {
177 struct connman_ipaddress *ipaddress = list->data;
179 connman_ipaddress_free(ipaddress);
183 g_slist_free(ipdevice->address_list);
184 ipdevice->address_list = NULL;
187 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
188 unsigned char prefixlen, const char *local)
192 for (list = ipdevice->address_list; list; list = list->next) {
193 struct connman_ipaddress *ipaddress = list->data;
195 if (g_strcmp0(ipaddress->local, local) == 0 &&
196 ipaddress->prefixlen == prefixlen)
203 static const char *type2str(unsigned short type)
208 case ARPHRD_LOOPBACK:
221 static const char *scope2str(unsigned char scope)
233 static void free_ipdevice(gpointer data)
235 struct connman_ipdevice *ipdevice = data;
237 connman_info("%s {remove} index %d", ipdevice->ifname,
240 if (ipdevice->config != NULL)
241 connman_ipconfig_unref(ipdevice->config);
243 free_address_list(ipdevice);
244 g_free(ipdevice->gateway);
246 g_free(ipdevice->address);
247 g_free(ipdevice->ifname);
251 static GSList *driver_list = NULL;
253 static gint compare_priority(gconstpointer a, gconstpointer b)
255 const struct connman_ipconfig_driver *driver1 = a;
256 const struct connman_ipconfig_driver *driver2 = b;
258 return driver2->priority - driver1->priority;
262 * connman_ipconfig_driver_register:
263 * @driver: IP configuration driver
265 * Register a new IP configuration driver
267 * Returns: %0 on success
269 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
271 DBG("driver %p name %s", driver, driver->name);
273 driver_list = g_slist_insert_sorted(driver_list, driver,
280 * connman_ipconfig_driver_unregister:
281 * @driver: IP configuration driver
283 * Remove a previously registered IP configuration driver.
285 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
287 DBG("driver %p name %s", driver, driver->name);
289 driver_list = g_slist_remove(driver_list, driver);
292 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
296 DBG("ipconfig %p", ipdevice->config);
298 if (ipdevice->config == NULL)
301 switch (ipdevice->config->method) {
302 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
303 case CONNMAN_IPCONFIG_METHOD_OFF:
304 case CONNMAN_IPCONFIG_METHOD_FIXED:
305 case CONNMAN_IPCONFIG_METHOD_MANUAL:
307 case CONNMAN_IPCONFIG_METHOD_DHCP:
311 if (ipdevice->driver != NULL)
314 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
315 if (ipdevice->driver_config == NULL)
318 for (list = driver_list; list; list = list->next) {
319 struct connman_ipconfig_driver *driver = list->data;
321 if (driver->request(ipdevice->driver_config) == 0) {
322 ipdevice->driver = driver;
327 if (ipdevice->driver == NULL) {
328 connman_ipconfig_unref(ipdevice->driver_config);
329 ipdevice->driver_config = NULL;
333 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
335 DBG("ipconfig %p", ipdevice->config);
337 if (ipdevice->config == NULL)
340 if (ipdevice->driver == NULL)
343 ipdevice->driver->release(ipdevice->driver_config);
345 ipdevice->driver = NULL;
347 connman_ipconfig_unref(ipdevice->driver_config);
348 ipdevice->driver_config = NULL;
350 connman_inet_clear_address(ipdevice->index);
353 static void update_stats(struct connman_ipdevice *ipdevice,
354 struct rtnl_link_stats *stats)
356 if (stats->rx_packets == 0 && stats->tx_packets == 0)
359 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
360 stats->rx_packets, stats->rx_bytes);
361 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
362 stats->tx_packets, stats->tx_bytes);
364 if (ipdevice->config == NULL)
367 ipdevice->rx_bytes = stats->rx_bytes;
368 ipdevice->tx_bytes = stats->tx_bytes;
370 __connman_counter_notify(ipdevice->config,
371 ipdevice->rx_bytes, ipdevice->tx_bytes);
374 void __connman_ipconfig_newlink(int index, unsigned short type,
375 unsigned int flags, const char *address,
377 struct rtnl_link_stats *stats)
379 struct connman_ipdevice *ipdevice;
382 gboolean up = FALSE, down = FALSE;
383 gboolean lower_up = FALSE, lower_down = FALSE;
385 DBG("index %d", index);
387 if (type == ARPHRD_LOOPBACK)
390 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
391 if (ipdevice != NULL)
394 ipdevice = g_try_new0(struct connman_ipdevice, 1);
395 if (ipdevice == NULL)
398 ipdevice->index = index;
399 ipdevice->ifname = connman_inet_ifname(index);
400 ipdevice->type = type;
402 ipdevice->address = g_strdup(address);
404 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
406 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
407 index, type, type2str(type));
412 update_stats(ipdevice, stats);
414 if (flags == ipdevice->flags)
417 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
424 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
425 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
426 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
427 (IFF_RUNNING | IFF_LOWER_UP))
429 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
433 ipdevice->flags = flags;
435 str = g_string_new(NULL);
440 g_string_append(str, "UP");
442 g_string_append(str, "DOWN");
444 if (flags & IFF_RUNNING)
445 g_string_append(str, ",RUNNING");
447 if (flags & IFF_LOWER_UP)
448 g_string_append(str, ",LOWER_UP");
450 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
453 g_string_free(str, TRUE);
455 for (list = g_list_first(ipconfig_list); list;
456 list = g_list_next(list)) {
457 struct connman_ipconfig *ipconfig = list->data;
459 if (index != ipconfig->index)
462 if (ipconfig->ops == NULL)
465 if (up == TRUE && ipconfig->ops->up)
466 ipconfig->ops->up(ipconfig);
467 if (lower_up == TRUE && ipconfig->ops->lower_up)
468 ipconfig->ops->lower_up(ipconfig);
470 if (lower_down == TRUE && ipconfig->ops->lower_down)
471 ipconfig->ops->lower_down(ipconfig);
472 if (down == TRUE && ipconfig->ops->down)
473 ipconfig->ops->down(ipconfig);
477 __connman_ipconfig_lower_up(ipdevice);
479 __connman_ipconfig_lower_down(ipdevice);
482 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
484 struct connman_ipdevice *ipdevice;
487 DBG("index %d", index);
489 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
490 if (ipdevice == NULL)
493 update_stats(ipdevice, stats);
495 for (list = g_list_first(ipconfig_list); list;
496 list = g_list_next(list)) {
497 struct connman_ipconfig *ipconfig = list->data;
499 if (index != ipconfig->index)
502 ipconfig->index = -1;
504 if (ipconfig->ops == NULL)
507 if (ipconfig->ops->lower_down)
508 ipconfig->ops->lower_down(ipconfig);
509 if (ipconfig->ops->down)
510 ipconfig->ops->down(ipconfig);
513 __connman_ipconfig_lower_down(ipdevice);
515 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
518 void __connman_ipconfig_newaddr(int index, const char *label,
519 unsigned char prefixlen, const char *address)
521 struct connman_ipdevice *ipdevice;
522 struct connman_ipaddress *ipaddress;
525 DBG("index %d", index);
527 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
528 if (ipdevice == NULL)
531 ipaddress = connman_ipaddress_alloc();
532 if (ipaddress == NULL)
535 ipaddress->prefixlen = prefixlen;
536 ipaddress->local = g_strdup(address);
538 ipdevice->address_list = g_slist_append(ipdevice->address_list,
541 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
542 address, prefixlen, label);
544 if (ipdevice->config != NULL)
545 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
547 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
550 if (g_slist_length(ipdevice->address_list) > 1)
553 for (list = g_list_first(ipconfig_list); list;
554 list = g_list_next(list)) {
555 struct connman_ipconfig *ipconfig = list->data;
557 if (index != ipconfig->index)
560 if (ipconfig->ops == NULL)
563 if (ipconfig->ops->ip_bound)
564 ipconfig->ops->ip_bound(ipconfig);
568 void __connman_ipconfig_deladdr(int index, const char *label,
569 unsigned char prefixlen, const char *address)
571 struct connman_ipdevice *ipdevice;
572 struct connman_ipaddress *ipaddress;
575 DBG("index %d", index);
577 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
578 if (ipdevice == NULL)
581 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
582 if (ipaddress == NULL)
585 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
588 connman_ipaddress_free(ipaddress);
590 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
591 address, prefixlen, label);
593 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
596 if (g_slist_length(ipdevice->address_list) > 0)
599 for (list = g_list_first(ipconfig_list); list;
600 list = g_list_next(list)) {
601 struct connman_ipconfig *ipconfig = list->data;
603 if (index != ipconfig->index)
606 if (ipconfig->ops == NULL)
609 if (ipconfig->ops->ip_release)
610 ipconfig->ops->ip_release(ipconfig);
614 void __connman_ipconfig_newroute(int index, unsigned char scope,
615 const char *dst, const char *gateway)
617 struct connman_ipdevice *ipdevice;
619 DBG("index %d", index);
621 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
622 if (ipdevice == NULL)
625 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
628 g_free(ipdevice->gateway);
629 ipdevice->gateway = g_strdup(gateway);
631 if (ipdevice->config != NULL &&
632 ipdevice->config->system != NULL) {
633 g_free(ipdevice->config->system->gateway);
634 ipdevice->config->system->gateway = g_strdup(gateway);
637 for (list = ipdevice->address_list; list; list = list->next) {
638 struct connman_ipaddress *ipaddress = list->data;
640 g_free(ipaddress->gateway);
641 ipaddress->gateway = g_strdup(gateway);
645 connman_info("%s {add} route %s gw %s scope %u <%s>",
646 ipdevice->ifname, dst, gateway,
647 scope, scope2str(scope));
650 void __connman_ipconfig_delroute(int index, unsigned char scope,
651 const char *dst, const char *gateway)
653 struct connman_ipdevice *ipdevice;
655 DBG("index %d", index);
657 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
658 if (ipdevice == NULL)
661 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
664 g_free(ipdevice->gateway);
665 ipdevice->gateway = NULL;
667 if (ipdevice->config != NULL &&
668 ipdevice->config->system != NULL) {
669 g_free(ipdevice->config->system->gateway);
670 ipdevice->config->system->gateway = NULL;
673 for (list = ipdevice->address_list; list; list = list->next) {
674 struct connman_ipaddress *ipaddress = list->data;
676 g_free(ipaddress->gateway);
677 ipaddress->gateway = NULL;
681 connman_info("%s {del} route %s gw %s scope %u <%s>",
682 ipdevice->ifname, dst, gateway,
683 scope, scope2str(scope));
686 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
691 keys = g_hash_table_get_keys(ipdevice_hash);
695 for (list = g_list_first(keys); list; list = g_list_next(list)) {
696 int index = GPOINTER_TO_INT(list->data);
698 function(index, user_data);
704 unsigned short __connman_ipconfig_get_type(int index)
706 struct connman_ipdevice *ipdevice;
708 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
709 if (ipdevice == NULL)
712 return ipdevice->type;
715 unsigned int __connman_ipconfig_get_flags(int index)
717 struct connman_ipdevice *ipdevice;
719 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
720 if (ipdevice == NULL)
723 return ipdevice->flags;
726 const char *__connman_ipconfig_get_gateway(int index)
728 struct connman_ipdevice *ipdevice;
730 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
731 if (ipdevice == NULL)
734 return ipdevice->gateway;
737 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
739 ipconfig->index = index;
743 * connman_ipconfig_create:
745 * Allocate a new ipconfig structure.
747 * Returns: a newly-allocated #connman_ipconfig structure
749 struct connman_ipconfig *connman_ipconfig_create(int index)
751 struct connman_ipconfig *ipconfig;
753 DBG("index %d", index);
755 ipconfig = g_try_new0(struct connman_ipconfig, 1);
756 if (ipconfig == NULL)
759 ipconfig->refcount = 1;
761 ipconfig->index = index;
763 ipconfig->address = connman_ipaddress_alloc();
764 if (ipconfig->address == NULL) {
769 ipconfig->system = connman_ipaddress_alloc();
771 DBG("ipconfig %p", ipconfig);
777 * connman_ipconfig_clone:
779 * Clone an ipconfig structure and create new reference.
781 * Returns: a newly-allocated #connman_ipconfig structure
783 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
785 struct connman_ipconfig *ipconfig_clone;
787 DBG("ipconfig %p", ipconfig);
789 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
790 if (ipconfig_clone == NULL)
793 ipconfig_clone->refcount = 1;
795 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
797 ipconfig_clone->index = -1;
799 return ipconfig_clone;
803 * connman_ipconfig_ref:
804 * @ipconfig: ipconfig structure
806 * Increase reference counter of ipconfig
808 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
810 g_atomic_int_inc(&ipconfig->refcount);
816 * connman_ipconfig_unref:
817 * @ipconfig: ipconfig structure
819 * Decrease reference counter of ipconfig
821 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
823 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
824 __connman_ipconfig_disable(ipconfig);
826 connman_ipconfig_set_ops(ipconfig, NULL);
828 if (ipconfig->origin != NULL) {
829 connman_ipconfig_unref(ipconfig->origin);
830 ipconfig->origin = NULL;
833 connman_ipaddress_free(ipconfig->system);
834 connman_ipaddress_free(ipconfig->address);
840 * connman_ipconfig_get_data:
841 * @ipconfig: ipconfig structure
843 * Get private data pointer
845 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
847 return ipconfig->ops_data;
851 * connman_ipconfig_set_data:
852 * @ipconfig: ipconfig structure
853 * @data: data pointer
855 * Set private data pointer
857 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
859 ipconfig->ops_data = data;
863 * connman_ipconfig_get_index:
864 * @ipconfig: ipconfig structure
866 * Get interface index
868 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
870 if (ipconfig == NULL)
873 if (ipconfig->origin != NULL)
874 return ipconfig->origin->index;
876 return ipconfig->index;
880 * connman_ipconfig_get_ifname:
881 * @ipconfig: ipconfig structure
885 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
887 struct connman_ipdevice *ipdevice;
889 if (ipconfig == NULL)
892 if (ipconfig->index < 0)
895 ipdevice = g_hash_table_lookup(ipdevice_hash,
896 GINT_TO_POINTER(ipconfig->index));
897 if (ipdevice == NULL)
900 return ipdevice->ifname;
904 * connman_ipconfig_set_ops:
905 * @ipconfig: ipconfig structure
906 * @ops: operation callbacks
908 * Set the operation callbacks
910 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
911 const struct connman_ipconfig_ops *ops)
917 * connman_ipconfig_set_method:
918 * @ipconfig: ipconfig structure
919 * @method: configuration method
921 * Set the configuration method
923 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
924 enum connman_ipconfig_method method)
926 ipconfig->method = method;
931 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
933 if (ipconfig == NULL)
934 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
936 return ipconfig->method;
940 * connman_ipconfig_bind:
941 * @ipconfig: ipconfig structure
942 * @ipaddress: ipaddress structure
944 * Bind IP address details to configuration
946 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
947 struct connman_ipaddress *ipaddress)
949 struct connman_ipconfig *origin;
951 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
953 connman_ipaddress_copy(origin->address, ipaddress);
955 connman_inet_set_address(origin->index, origin->address);
958 /* FIXME: The element soulution should be removed in the future */
959 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
960 struct connman_element *parent)
962 struct connman_element *connection;
964 connection = connman_element_create(NULL);
966 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
967 connection->index = ipconfig->index;
968 connection->ipv4.gateway = ipconfig->address->gateway;
970 if (connman_element_register(connection, parent) < 0)
971 connman_element_unref(connection);
976 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
980 switch (ipconfig->method) {
981 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
982 case CONNMAN_IPCONFIG_METHOD_OFF:
983 case CONNMAN_IPCONFIG_METHOD_FIXED:
984 case CONNMAN_IPCONFIG_METHOD_DHCP:
986 case CONNMAN_IPCONFIG_METHOD_MANUAL:
987 return connman_inet_set_address(ipconfig->index,
994 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
998 if (ipconfig == NULL)
1001 DBG("method %d", ipconfig->method);
1003 switch (ipconfig->method) {
1004 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1005 case CONNMAN_IPCONFIG_METHOD_OFF:
1006 case CONNMAN_IPCONFIG_METHOD_FIXED:
1007 case CONNMAN_IPCONFIG_METHOD_DHCP:
1009 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1010 return connman_inet_clear_address(ipconfig->index);
1017 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1019 struct connman_ipdevice *ipdevice;
1020 gboolean up = FALSE, down = FALSE;
1021 gboolean lower_up = FALSE, lower_down = FALSE;
1023 DBG("ipconfig %p", ipconfig);
1025 if (ipconfig == NULL || ipconfig->index < 0)
1028 ipdevice = g_hash_table_lookup(ipdevice_hash,
1029 GINT_TO_POINTER(ipconfig->index));
1030 if (ipdevice == NULL)
1033 if (ipdevice->config == ipconfig)
1036 if (ipdevice->config != NULL) {
1037 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1039 connman_ipaddress_clear(ipdevice->config->system);
1041 connman_ipconfig_unref(ipdevice->config);
1044 ipdevice->config = connman_ipconfig_ref(ipconfig);
1046 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1048 if (ipdevice->flags & IFF_UP)
1053 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1054 (IFF_RUNNING | IFF_LOWER_UP))
1056 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1059 if (up == TRUE && ipconfig->ops->up)
1060 ipconfig->ops->up(ipconfig);
1061 if (lower_up == TRUE && ipconfig->ops->lower_up)
1062 ipconfig->ops->lower_up(ipconfig);
1064 if (lower_down == TRUE && ipconfig->ops->lower_down)
1065 ipconfig->ops->lower_down(ipconfig);
1066 if (down == TRUE && ipconfig->ops->down)
1067 ipconfig->ops->down(ipconfig);
1072 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1074 struct connman_ipdevice *ipdevice;
1076 DBG("ipconfig %p", ipconfig);
1078 if (ipconfig == NULL || ipconfig->index < 0)
1081 ipdevice = g_hash_table_lookup(ipdevice_hash,
1082 GINT_TO_POINTER(ipconfig->index));
1083 if (ipdevice == NULL)
1086 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1089 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1091 connman_ipaddress_clear(ipdevice->config->system);
1093 connman_ipconfig_unref(ipdevice->config);
1094 ipdevice->config = NULL;
1099 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1102 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1104 case CONNMAN_IPCONFIG_METHOD_OFF:
1106 case CONNMAN_IPCONFIG_METHOD_FIXED:
1108 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1110 case CONNMAN_IPCONFIG_METHOD_DHCP:
1117 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1119 if (g_strcmp0(method, "off") == 0)
1120 return CONNMAN_IPCONFIG_METHOD_OFF;
1121 else if (g_strcmp0(method, "fixed") == 0)
1122 return CONNMAN_IPCONFIG_METHOD_FIXED;
1123 else if (g_strcmp0(method, "manual") == 0)
1124 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1125 else if (g_strcmp0(method, "dhcp") == 0)
1126 return CONNMAN_IPCONFIG_METHOD_DHCP;
1128 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1131 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1132 DBusMessageIter *iter)
1136 str = __connman_ipconfig_method2string(ipconfig->method);
1140 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1142 if (ipconfig->system == NULL)
1145 if (ipconfig->system->local != NULL) {
1147 struct in_addr netmask;
1150 connman_dbus_dict_append_basic(iter, "Address",
1151 DBUS_TYPE_STRING, &ipconfig->system->local);
1153 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1154 netmask.s_addr = htonl(addr);
1155 mask = inet_ntoa(netmask);
1156 connman_dbus_dict_append_basic(iter, "Netmask",
1157 DBUS_TYPE_STRING, &mask);
1160 if (ipconfig->system->gateway != NULL)
1161 connman_dbus_dict_append_basic(iter, "Gateway",
1162 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1165 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1166 DBusMessageIter *iter)
1170 str = __connman_ipconfig_method2string(ipconfig->method);
1174 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1176 switch (ipconfig->method) {
1177 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1178 case CONNMAN_IPCONFIG_METHOD_OFF:
1179 case CONNMAN_IPCONFIG_METHOD_FIXED:
1180 case CONNMAN_IPCONFIG_METHOD_DHCP:
1182 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1186 if (ipconfig->address == NULL)
1189 if (ipconfig->address->local != NULL) {
1191 struct in_addr netmask;
1194 connman_dbus_dict_append_basic(iter, "Address",
1195 DBUS_TYPE_STRING, &ipconfig->address->local);
1197 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1198 netmask.s_addr = htonl(addr);
1199 mask = inet_ntoa(netmask);
1200 connman_dbus_dict_append_basic(iter, "Netmask",
1201 DBUS_TYPE_STRING, &mask);
1204 if (ipconfig->address->gateway != NULL)
1205 connman_dbus_dict_append_basic(iter, "Gateway",
1206 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1209 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1210 DBusMessageIter *array)
1212 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1213 const char *address = NULL, *netmask = NULL, *gateway = NULL;
1214 DBusMessageIter dict;
1216 DBG("ipconfig %p", ipconfig);
1218 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1221 dbus_message_iter_recurse(array, &dict);
1223 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1224 DBusMessageIter entry;
1228 dbus_message_iter_recurse(&dict, &entry);
1230 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1233 dbus_message_iter_get_basic(&entry, &key);
1234 dbus_message_iter_next(&entry);
1236 type = dbus_message_iter_get_arg_type(&entry);
1238 if (g_str_equal(key, "Method") == TRUE) {
1241 if (type != DBUS_TYPE_STRING)
1244 dbus_message_iter_get_basic(&entry, &str);
1245 method = __connman_ipconfig_string2method(str);
1246 } else if (g_str_equal(key, "Address") == TRUE) {
1247 if (type != DBUS_TYPE_STRING)
1250 dbus_message_iter_get_basic(&entry, &address);
1251 } else if (g_str_equal(key, "Netmask") == TRUE) {
1252 if (type != DBUS_TYPE_STRING)
1255 dbus_message_iter_get_basic(&entry, &netmask);
1256 } else if (g_str_equal(key, "Gateway") == TRUE) {
1257 if (type != DBUS_TYPE_STRING)
1260 dbus_message_iter_get_basic(&entry, &gateway);
1262 dbus_message_iter_next(&dict);
1265 DBG("method %d address %s netmask %s gateway %s",
1266 method, address, netmask, gateway);
1269 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1270 case CONNMAN_IPCONFIG_METHOD_OFF:
1271 case CONNMAN_IPCONFIG_METHOD_FIXED:
1274 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1275 if (address == NULL)
1278 ipconfig->method = method;
1279 connman_ipaddress_set(ipconfig->address,
1280 address, netmask, gateway);
1283 case CONNMAN_IPCONFIG_METHOD_DHCP:
1284 if (ipconfig->method == method)
1287 ipconfig->method = method;
1294 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1295 DBusMessageIter *iter)
1297 const char *method = "direct";
1299 connman_dbus_dict_append_basic(iter, "Method",
1300 DBUS_TYPE_STRING, &method);
1303 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1304 DBusMessageIter *iter)
1306 struct connman_ipdevice *ipdevice;
1307 const char *method = "auto";
1309 connman_dbus_dict_append_basic(iter, "Method",
1310 DBUS_TYPE_STRING, &method);
1312 ipdevice = g_hash_table_lookup(ipdevice_hash,
1313 GINT_TO_POINTER(ipconfig->index));
1314 if (ipdevice == NULL)
1317 if (ipdevice->ifname != NULL)
1318 connman_dbus_dict_append_basic(iter, "Interface",
1319 DBUS_TYPE_STRING, &ipdevice->ifname);
1321 if (ipdevice->address != NULL)
1322 connman_dbus_dict_append_basic(iter, "Address",
1323 DBUS_TYPE_STRING, &ipdevice->address);
1325 if (ipdevice->mtu > 0)
1326 connman_dbus_dict_append_basic(iter, "MTU",
1327 DBUS_TYPE_UINT16, &ipdevice->mtu);
1330 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1331 GKeyFile *keyfile, const char *identifier, const char *prefix)
1336 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1338 key = g_strdup_printf("%smethod", prefix);
1339 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1341 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1343 ipconfig->method = __connman_ipconfig_string2method(method);
1346 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1347 ipconfig->address->prefixlen = g_key_file_get_integer(
1348 keyfile, identifier, key, NULL);
1351 key = g_strdup_printf("%slocal_address", prefix);
1352 ipconfig->address->local = g_key_file_get_string(
1353 keyfile, identifier, key, NULL);
1356 key = g_strdup_printf("%speer_address", prefix);
1357 ipconfig->address->peer = g_key_file_get_string(
1358 keyfile, identifier, key, NULL);
1361 key = g_strdup_printf("%sbroadcast_address", prefix);
1362 ipconfig->address->broadcast = g_key_file_get_string(
1363 keyfile, identifier, key, NULL);
1366 key = g_strdup_printf("%sgateway", prefix);
1367 ipconfig->address->gateway = g_key_file_get_string(
1368 keyfile, identifier, key, NULL);
1374 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1375 GKeyFile *keyfile, const char *identifier, const char *prefix)
1380 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1382 method = __connman_ipconfig_method2string(ipconfig->method);
1384 key = g_strdup_printf("%smethod", prefix);
1385 g_key_file_set_string(keyfile, identifier, key, method);
1388 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1389 g_key_file_set_integer(keyfile, identifier,
1390 key, ipconfig->address->prefixlen);
1393 key = g_strdup_printf("%slocal_address", prefix);
1394 if (ipconfig->address->local != NULL)
1395 g_key_file_set_string(keyfile, identifier,
1396 key, ipconfig->address->local);
1399 key = g_strdup_printf("%speer_address", prefix);
1400 if (ipconfig->address->peer != NULL)
1401 g_key_file_set_string(keyfile, identifier,
1402 key, ipconfig->address->peer);
1405 key = g_strdup_printf("%sbroadcast_address", prefix);
1406 if (ipconfig->address->broadcast != NULL)
1407 g_key_file_set_string(keyfile, identifier,
1408 key, ipconfig->address->broadcast);
1411 key = g_strdup_printf("%sgateway", prefix);
1412 if (ipconfig->address->gateway != NULL)
1413 g_key_file_set_string(keyfile, identifier,
1414 key, ipconfig->address->gateway);
1420 int __connman_ipconfig_init(void)
1424 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1425 NULL, free_ipdevice);
1430 void __connman_ipconfig_cleanup(void)
1434 g_hash_table_destroy(ipdevice_hash);
1435 ipdevice_hash = NULL;