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 {
71 struct connman_ipconfig *config;
73 struct connman_ipconfig_driver *driver;
74 struct connman_ipconfig *driver_config;
77 static GHashTable *ipdevice_hash = NULL;
78 static GList *ipconfig_list = NULL;
80 struct connman_ipaddress *connman_ipaddress_alloc(void)
82 struct connman_ipaddress *ipaddress;
84 ipaddress = g_try_new0(struct connman_ipaddress, 1);
85 if (ipaddress == NULL)
91 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
93 if (ipaddress == NULL)
96 g_free(ipaddress->broadcast);
97 g_free(ipaddress->peer);
98 g_free(ipaddress->local);
99 g_free(ipaddress->gateway);
103 static unsigned char netmask2prefixlen(const char *netmask)
105 unsigned char bits = 0;
106 in_addr_t mask = inet_network(netmask);
107 in_addr_t host = ~mask;
109 /* a valid netmask must be 2^n - 1 */
110 if ((host & (host + 1)) != 0)
113 for (; mask; mask <<= 1)
119 void connman_ipaddress_set(struct connman_ipaddress *ipaddress,
120 const char *address, const char *netmask, const char *gateway)
122 if (ipaddress == NULL)
126 ipaddress->prefixlen = netmask2prefixlen(netmask);
128 ipaddress->prefixlen = 32;
130 g_free(ipaddress->local);
131 ipaddress->local = g_strdup(address);
133 g_free(ipaddress->gateway);
134 ipaddress->gateway = g_strdup(gateway);
137 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
139 if (ipaddress == NULL)
142 ipaddress->prefixlen = 0;
144 g_free(ipaddress->local);
145 ipaddress->local = NULL;
147 g_free(ipaddress->peer);
148 ipaddress->peer = NULL;
150 g_free(ipaddress->broadcast);
151 ipaddress->broadcast = NULL;
153 g_free(ipaddress->gateway);
154 ipaddress->gateway = NULL;
157 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
158 struct connman_ipaddress *source)
160 if (ipaddress == NULL || source == NULL)
163 ipaddress->prefixlen = source->prefixlen;
165 g_free(ipaddress->local);
166 ipaddress->local = g_strdup(source->local);
168 g_free(ipaddress->peer);
169 ipaddress->peer = g_strdup(source->peer);
171 g_free(ipaddress->broadcast);
172 ipaddress->broadcast = g_strdup(source->broadcast);
174 g_free(ipaddress->gateway);
175 ipaddress->gateway = g_strdup(source->gateway);
178 static void free_address_list(struct connman_ipdevice *ipdevice)
182 for (list = ipdevice->address_list; list; list = list->next) {
183 struct connman_ipaddress *ipaddress = list->data;
185 connman_ipaddress_free(ipaddress);
189 g_slist_free(ipdevice->address_list);
190 ipdevice->address_list = NULL;
193 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
194 unsigned char prefixlen, const char *local)
198 for (list = ipdevice->address_list; list; list = list->next) {
199 struct connman_ipaddress *ipaddress = list->data;
201 if (g_strcmp0(ipaddress->local, local) == 0 &&
202 ipaddress->prefixlen == prefixlen)
209 static const char *type2str(unsigned short type)
214 case ARPHRD_LOOPBACK:
227 static const char *scope2str(unsigned char scope)
239 static void free_ipdevice(gpointer data)
241 struct connman_ipdevice *ipdevice = data;
243 connman_info("%s {remove} index %d", ipdevice->ifname,
246 if (ipdevice->config != NULL)
247 connman_ipconfig_unref(ipdevice->config);
249 free_address_list(ipdevice);
250 g_free(ipdevice->gateway);
252 g_free(ipdevice->address);
253 g_free(ipdevice->ifname);
257 static GSList *driver_list = NULL;
259 static gint compare_priority(gconstpointer a, gconstpointer b)
261 const struct connman_ipconfig_driver *driver1 = a;
262 const struct connman_ipconfig_driver *driver2 = b;
264 return driver2->priority - driver1->priority;
268 * connman_ipconfig_driver_register:
269 * @driver: IP configuration driver
271 * Register a new IP configuration driver
273 * Returns: %0 on success
275 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
277 DBG("driver %p name %s", driver, driver->name);
279 driver_list = g_slist_insert_sorted(driver_list, driver,
286 * connman_ipconfig_driver_unregister:
287 * @driver: IP configuration driver
289 * Remove a previously registered IP configuration driver.
291 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
293 DBG("driver %p name %s", driver, driver->name);
295 driver_list = g_slist_remove(driver_list, driver);
298 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
302 DBG("ipconfig %p", ipdevice->config);
304 if (ipdevice->config == NULL)
307 switch (ipdevice->config->method) {
308 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
309 case CONNMAN_IPCONFIG_METHOD_OFF:
310 case CONNMAN_IPCONFIG_METHOD_FIXED:
311 case CONNMAN_IPCONFIG_METHOD_MANUAL:
313 case CONNMAN_IPCONFIG_METHOD_DHCP:
317 if (ipdevice->driver != NULL)
320 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
321 if (ipdevice->driver_config == NULL)
324 for (list = driver_list; list; list = list->next) {
325 struct connman_ipconfig_driver *driver = list->data;
327 if (driver->request(ipdevice->driver_config) == 0) {
328 ipdevice->driver = driver;
333 if (ipdevice->driver == NULL) {
334 connman_ipconfig_unref(ipdevice->driver_config);
335 ipdevice->driver_config = NULL;
339 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
341 DBG("ipconfig %p", ipdevice->config);
343 if (ipdevice->config == NULL)
346 if (ipdevice->driver == NULL)
349 ipdevice->driver->release(ipdevice->driver_config);
351 ipdevice->driver = NULL;
353 connman_ipconfig_unref(ipdevice->driver_config);
354 ipdevice->driver_config = NULL;
356 connman_inet_clear_address(ipdevice->index);
359 static void update_stats(struct connman_ipdevice *ipdevice,
360 struct rtnl_link_stats *stats)
362 if (stats->rx_packets == 0 && stats->tx_packets == 0)
365 connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
366 stats->rx_packets, stats->rx_bytes);
367 connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
368 stats->tx_packets, stats->tx_bytes);
370 if (ipdevice->config == NULL)
373 ipdevice->rx_packets = stats->rx_packets;
374 ipdevice->tx_packets = stats->tx_packets;
375 ipdevice->rx_bytes = stats->rx_bytes;
376 ipdevice->tx_bytes = stats->tx_bytes;
377 ipdevice->rx_errors = stats->rx_errors;
378 ipdevice->tx_errors = stats->tx_errors;
379 ipdevice->rx_dropped = stats->rx_dropped;
380 ipdevice->tx_dropped = stats->tx_dropped;
382 __connman_counter_notify(ipdevice->config,
383 ipdevice->rx_packets, ipdevice->tx_packets,
384 ipdevice->rx_bytes, ipdevice->tx_bytes,
385 ipdevice->rx_errors, ipdevice->tx_errors,
386 ipdevice->rx_dropped, ipdevice->tx_dropped);
389 void __connman_ipconfig_newlink(int index, unsigned short type,
390 unsigned int flags, const char *address,
392 struct rtnl_link_stats *stats)
394 struct connman_ipdevice *ipdevice;
397 gboolean up = FALSE, down = FALSE;
398 gboolean lower_up = FALSE, lower_down = FALSE;
400 DBG("index %d", index);
402 if (type == ARPHRD_LOOPBACK)
405 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
406 if (ipdevice != NULL)
409 ipdevice = g_try_new0(struct connman_ipdevice, 1);
410 if (ipdevice == NULL)
413 ipdevice->index = index;
414 ipdevice->ifname = connman_inet_ifname(index);
415 ipdevice->type = type;
417 ipdevice->address = g_strdup(address);
419 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
421 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
422 index, type, type2str(type));
427 update_stats(ipdevice, stats);
429 if (flags == ipdevice->flags)
432 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
439 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
440 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
441 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
442 (IFF_RUNNING | IFF_LOWER_UP))
444 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
448 ipdevice->flags = flags;
450 str = g_string_new(NULL);
455 g_string_append(str, "UP");
457 g_string_append(str, "DOWN");
459 if (flags & IFF_RUNNING)
460 g_string_append(str, ",RUNNING");
462 if (flags & IFF_LOWER_UP)
463 g_string_append(str, ",LOWER_UP");
465 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
468 g_string_free(str, TRUE);
470 for (list = g_list_first(ipconfig_list); list;
471 list = g_list_next(list)) {
472 struct connman_ipconfig *ipconfig = list->data;
474 if (index != ipconfig->index)
477 if (ipconfig->ops == NULL)
480 if (up == TRUE && ipconfig->ops->up)
481 ipconfig->ops->up(ipconfig);
482 if (lower_up == TRUE && ipconfig->ops->lower_up)
483 ipconfig->ops->lower_up(ipconfig);
485 if (lower_down == TRUE && ipconfig->ops->lower_down)
486 ipconfig->ops->lower_down(ipconfig);
487 if (down == TRUE && ipconfig->ops->down)
488 ipconfig->ops->down(ipconfig);
492 __connman_ipconfig_lower_up(ipdevice);
494 __connman_ipconfig_lower_down(ipdevice);
497 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
499 struct connman_ipdevice *ipdevice;
502 DBG("index %d", index);
504 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
505 if (ipdevice == NULL)
508 update_stats(ipdevice, stats);
510 for (list = g_list_first(ipconfig_list); list;
511 list = g_list_next(list)) {
512 struct connman_ipconfig *ipconfig = list->data;
514 if (index != ipconfig->index)
517 ipconfig->index = -1;
519 if (ipconfig->ops == NULL)
522 if (ipconfig->ops->lower_down)
523 ipconfig->ops->lower_down(ipconfig);
524 if (ipconfig->ops->down)
525 ipconfig->ops->down(ipconfig);
528 __connman_ipconfig_lower_down(ipdevice);
530 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
533 void __connman_ipconfig_newaddr(int index, const char *label,
534 unsigned char prefixlen, const char *address)
536 struct connman_ipdevice *ipdevice;
537 struct connman_ipaddress *ipaddress;
540 DBG("index %d", index);
542 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
543 if (ipdevice == NULL)
546 ipaddress = connman_ipaddress_alloc();
547 if (ipaddress == NULL)
550 ipaddress->prefixlen = prefixlen;
551 ipaddress->local = g_strdup(address);
553 ipdevice->address_list = g_slist_append(ipdevice->address_list,
556 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
557 address, prefixlen, label);
559 if (ipdevice->config != NULL)
560 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
562 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
565 if (g_slist_length(ipdevice->address_list) > 1)
568 for (list = g_list_first(ipconfig_list); list;
569 list = g_list_next(list)) {
570 struct connman_ipconfig *ipconfig = list->data;
572 if (index != ipconfig->index)
575 if (ipconfig->ops == NULL)
578 if (ipconfig->ops->ip_bound)
579 ipconfig->ops->ip_bound(ipconfig);
583 void __connman_ipconfig_deladdr(int index, const char *label,
584 unsigned char prefixlen, const char *address)
586 struct connman_ipdevice *ipdevice;
587 struct connman_ipaddress *ipaddress;
590 DBG("index %d", index);
592 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
593 if (ipdevice == NULL)
596 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
597 if (ipaddress == NULL)
600 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
603 connman_ipaddress_free(ipaddress);
605 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
606 address, prefixlen, label);
608 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
611 if (g_slist_length(ipdevice->address_list) > 0)
614 for (list = g_list_first(ipconfig_list); list;
615 list = g_list_next(list)) {
616 struct connman_ipconfig *ipconfig = list->data;
618 if (index != ipconfig->index)
621 if (ipconfig->ops == NULL)
624 if (ipconfig->ops->ip_release)
625 ipconfig->ops->ip_release(ipconfig);
629 void __connman_ipconfig_newroute(int index, unsigned char scope,
630 const char *dst, const char *gateway)
632 struct connman_ipdevice *ipdevice;
634 DBG("index %d", index);
636 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
637 if (ipdevice == NULL)
640 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
643 g_free(ipdevice->gateway);
644 ipdevice->gateway = g_strdup(gateway);
646 if (ipdevice->config != NULL &&
647 ipdevice->config->system != NULL) {
648 g_free(ipdevice->config->system->gateway);
649 ipdevice->config->system->gateway = g_strdup(gateway);
652 for (list = ipdevice->address_list; list; list = list->next) {
653 struct connman_ipaddress *ipaddress = list->data;
655 g_free(ipaddress->gateway);
656 ipaddress->gateway = g_strdup(gateway);
660 connman_info("%s {add} route %s gw %s scope %u <%s>",
661 ipdevice->ifname, dst, gateway,
662 scope, scope2str(scope));
665 void __connman_ipconfig_delroute(int index, unsigned char scope,
666 const char *dst, const char *gateway)
668 struct connman_ipdevice *ipdevice;
670 DBG("index %d", index);
672 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
673 if (ipdevice == NULL)
676 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
679 g_free(ipdevice->gateway);
680 ipdevice->gateway = NULL;
682 if (ipdevice->config != NULL &&
683 ipdevice->config->system != NULL) {
684 g_free(ipdevice->config->system->gateway);
685 ipdevice->config->system->gateway = NULL;
688 for (list = ipdevice->address_list; list; list = list->next) {
689 struct connman_ipaddress *ipaddress = list->data;
691 g_free(ipaddress->gateway);
692 ipaddress->gateway = NULL;
696 connman_info("%s {del} route %s gw %s scope %u <%s>",
697 ipdevice->ifname, dst, gateway,
698 scope, scope2str(scope));
701 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
706 keys = g_hash_table_get_keys(ipdevice_hash);
710 for (list = g_list_first(keys); list; list = g_list_next(list)) {
711 int index = GPOINTER_TO_INT(list->data);
713 function(index, user_data);
719 unsigned short __connman_ipconfig_get_type(int index)
721 struct connman_ipdevice *ipdevice;
723 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
724 if (ipdevice == NULL)
727 return ipdevice->type;
730 unsigned int __connman_ipconfig_get_flags(int index)
732 struct connman_ipdevice *ipdevice;
734 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
735 if (ipdevice == NULL)
738 return ipdevice->flags;
741 const char *__connman_ipconfig_get_gateway(int index)
743 struct connman_ipdevice *ipdevice;
745 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
746 if (ipdevice == NULL)
749 return ipdevice->gateway;
752 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
754 ipconfig->index = index;
758 * connman_ipconfig_create:
760 * Allocate a new ipconfig structure.
762 * Returns: a newly-allocated #connman_ipconfig structure
764 struct connman_ipconfig *connman_ipconfig_create(int index)
766 struct connman_ipconfig *ipconfig;
768 DBG("index %d", index);
770 ipconfig = g_try_new0(struct connman_ipconfig, 1);
771 if (ipconfig == NULL)
774 ipconfig->refcount = 1;
776 ipconfig->index = index;
778 ipconfig->address = connman_ipaddress_alloc();
779 if (ipconfig->address == NULL) {
784 ipconfig->system = connman_ipaddress_alloc();
786 DBG("ipconfig %p", ipconfig);
792 * connman_ipconfig_clone:
794 * Clone an ipconfig structure and create new reference.
796 * Returns: a newly-allocated #connman_ipconfig structure
798 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
800 struct connman_ipconfig *ipconfig_clone;
802 DBG("ipconfig %p", ipconfig);
804 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
805 if (ipconfig_clone == NULL)
808 ipconfig_clone->refcount = 1;
810 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
812 ipconfig_clone->index = -1;
814 return ipconfig_clone;
818 * connman_ipconfig_ref:
819 * @ipconfig: ipconfig structure
821 * Increase reference counter of ipconfig
823 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
825 g_atomic_int_inc(&ipconfig->refcount);
831 * connman_ipconfig_unref:
832 * @ipconfig: ipconfig structure
834 * Decrease reference counter of ipconfig
836 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
838 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
839 __connman_ipconfig_disable(ipconfig);
841 connman_ipconfig_set_ops(ipconfig, NULL);
843 if (ipconfig->origin != NULL) {
844 connman_ipconfig_unref(ipconfig->origin);
845 ipconfig->origin = NULL;
848 connman_ipaddress_free(ipconfig->system);
849 connman_ipaddress_free(ipconfig->address);
855 * connman_ipconfig_get_data:
856 * @ipconfig: ipconfig structure
858 * Get private data pointer
860 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
862 return ipconfig->ops_data;
866 * connman_ipconfig_set_data:
867 * @ipconfig: ipconfig structure
868 * @data: data pointer
870 * Set private data pointer
872 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
874 ipconfig->ops_data = data;
878 * connman_ipconfig_get_index:
879 * @ipconfig: ipconfig structure
881 * Get interface index
883 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
885 if (ipconfig == NULL)
888 if (ipconfig->origin != NULL)
889 return ipconfig->origin->index;
891 return ipconfig->index;
895 * connman_ipconfig_get_ifname:
896 * @ipconfig: ipconfig structure
900 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
902 struct connman_ipdevice *ipdevice;
904 if (ipconfig == NULL)
907 if (ipconfig->index < 0)
910 ipdevice = g_hash_table_lookup(ipdevice_hash,
911 GINT_TO_POINTER(ipconfig->index));
912 if (ipdevice == NULL)
915 return ipdevice->ifname;
919 * connman_ipconfig_set_ops:
920 * @ipconfig: ipconfig structure
921 * @ops: operation callbacks
923 * Set the operation callbacks
925 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
926 const struct connman_ipconfig_ops *ops)
932 * connman_ipconfig_set_method:
933 * @ipconfig: ipconfig structure
934 * @method: configuration method
936 * Set the configuration method
938 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
939 enum connman_ipconfig_method method)
941 ipconfig->method = method;
946 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
948 if (ipconfig == NULL)
949 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
951 return ipconfig->method;
955 * connman_ipconfig_bind:
956 * @ipconfig: ipconfig structure
957 * @ipaddress: ipaddress structure
959 * Bind IP address details to configuration
961 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
962 struct connman_ipaddress *ipaddress)
964 struct connman_ipconfig *origin;
966 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
968 connman_ipaddress_copy(origin->address, ipaddress);
970 connman_inet_set_address(origin->index, origin->address);
973 /* FIXME: The element soulution should be removed in the future */
974 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
975 struct connman_element *parent)
977 struct connman_element *connection;
979 connection = connman_element_create(NULL);
981 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
982 connection->index = ipconfig->index;
983 connection->ipv4.gateway = ipconfig->address->gateway;
985 if (connman_element_register(connection, parent) < 0)
986 connman_element_unref(connection);
991 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
995 switch (ipconfig->method) {
996 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
997 case CONNMAN_IPCONFIG_METHOD_OFF:
998 case CONNMAN_IPCONFIG_METHOD_FIXED:
999 case CONNMAN_IPCONFIG_METHOD_DHCP:
1001 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1002 return connman_inet_set_address(ipconfig->index,
1009 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1013 if (ipconfig == NULL)
1016 DBG("method %d", ipconfig->method);
1018 switch (ipconfig->method) {
1019 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1020 case CONNMAN_IPCONFIG_METHOD_OFF:
1021 case CONNMAN_IPCONFIG_METHOD_FIXED:
1022 case CONNMAN_IPCONFIG_METHOD_DHCP:
1024 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1025 return connman_inet_clear_address(ipconfig->index);
1032 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1034 struct connman_ipdevice *ipdevice;
1035 gboolean up = FALSE, down = FALSE;
1036 gboolean lower_up = FALSE, lower_down = FALSE;
1038 DBG("ipconfig %p", ipconfig);
1040 if (ipconfig == NULL || ipconfig->index < 0)
1043 ipdevice = g_hash_table_lookup(ipdevice_hash,
1044 GINT_TO_POINTER(ipconfig->index));
1045 if (ipdevice == NULL)
1048 if (ipdevice->config == ipconfig)
1051 if (ipdevice->config != NULL) {
1052 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1054 connman_ipaddress_clear(ipdevice->config->system);
1056 connman_ipconfig_unref(ipdevice->config);
1059 ipdevice->config = connman_ipconfig_ref(ipconfig);
1061 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1063 if (ipdevice->flags & IFF_UP)
1068 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1069 (IFF_RUNNING | IFF_LOWER_UP))
1071 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1074 if (up == TRUE && ipconfig->ops->up)
1075 ipconfig->ops->up(ipconfig);
1076 if (lower_up == TRUE && ipconfig->ops->lower_up)
1077 ipconfig->ops->lower_up(ipconfig);
1079 if (lower_down == TRUE && ipconfig->ops->lower_down)
1080 ipconfig->ops->lower_down(ipconfig);
1081 if (down == TRUE && ipconfig->ops->down)
1082 ipconfig->ops->down(ipconfig);
1087 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1089 struct connman_ipdevice *ipdevice;
1091 DBG("ipconfig %p", ipconfig);
1093 if (ipconfig == NULL || ipconfig->index < 0)
1096 ipdevice = g_hash_table_lookup(ipdevice_hash,
1097 GINT_TO_POINTER(ipconfig->index));
1098 if (ipdevice == NULL)
1101 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1104 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1106 connman_ipaddress_clear(ipdevice->config->system);
1108 connman_ipconfig_unref(ipdevice->config);
1109 ipdevice->config = NULL;
1114 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1117 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1119 case CONNMAN_IPCONFIG_METHOD_OFF:
1121 case CONNMAN_IPCONFIG_METHOD_FIXED:
1123 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1125 case CONNMAN_IPCONFIG_METHOD_DHCP:
1132 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1134 if (g_strcmp0(method, "off") == 0)
1135 return CONNMAN_IPCONFIG_METHOD_OFF;
1136 else if (g_strcmp0(method, "fixed") == 0)
1137 return CONNMAN_IPCONFIG_METHOD_FIXED;
1138 else if (g_strcmp0(method, "manual") == 0)
1139 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1140 else if (g_strcmp0(method, "dhcp") == 0)
1141 return CONNMAN_IPCONFIG_METHOD_DHCP;
1143 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1146 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1147 DBusMessageIter *iter)
1151 str = __connman_ipconfig_method2string(ipconfig->method);
1155 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1157 if (ipconfig->system == NULL)
1160 if (ipconfig->system->local != NULL) {
1162 struct in_addr netmask;
1165 connman_dbus_dict_append_basic(iter, "Address",
1166 DBUS_TYPE_STRING, &ipconfig->system->local);
1168 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1169 netmask.s_addr = htonl(addr);
1170 mask = inet_ntoa(netmask);
1171 connman_dbus_dict_append_basic(iter, "Netmask",
1172 DBUS_TYPE_STRING, &mask);
1175 if (ipconfig->system->gateway != NULL)
1176 connman_dbus_dict_append_basic(iter, "Gateway",
1177 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1180 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1181 DBusMessageIter *iter)
1185 str = __connman_ipconfig_method2string(ipconfig->method);
1189 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1191 switch (ipconfig->method) {
1192 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1193 case CONNMAN_IPCONFIG_METHOD_OFF:
1194 case CONNMAN_IPCONFIG_METHOD_FIXED:
1195 case CONNMAN_IPCONFIG_METHOD_DHCP:
1197 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1201 if (ipconfig->address == NULL)
1204 if (ipconfig->address->local != NULL) {
1206 struct in_addr netmask;
1209 connman_dbus_dict_append_basic(iter, "Address",
1210 DBUS_TYPE_STRING, &ipconfig->address->local);
1212 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1213 netmask.s_addr = htonl(addr);
1214 mask = inet_ntoa(netmask);
1215 connman_dbus_dict_append_basic(iter, "Netmask",
1216 DBUS_TYPE_STRING, &mask);
1219 if (ipconfig->address->gateway != NULL)
1220 connman_dbus_dict_append_basic(iter, "Gateway",
1221 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1224 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1225 DBusMessageIter *array)
1227 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1228 const char *address = NULL, *netmask = NULL, *gateway = NULL;
1229 DBusMessageIter dict;
1231 DBG("ipconfig %p", ipconfig);
1233 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1236 dbus_message_iter_recurse(array, &dict);
1238 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1239 DBusMessageIter entry;
1243 dbus_message_iter_recurse(&dict, &entry);
1245 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1248 dbus_message_iter_get_basic(&entry, &key);
1249 dbus_message_iter_next(&entry);
1251 type = dbus_message_iter_get_arg_type(&entry);
1253 if (g_str_equal(key, "Method") == TRUE) {
1256 if (type != DBUS_TYPE_STRING)
1259 dbus_message_iter_get_basic(&entry, &str);
1260 method = __connman_ipconfig_string2method(str);
1261 } else if (g_str_equal(key, "Address") == TRUE) {
1262 if (type != DBUS_TYPE_STRING)
1265 dbus_message_iter_get_basic(&entry, &address);
1266 } else if (g_str_equal(key, "Netmask") == TRUE) {
1267 if (type != DBUS_TYPE_STRING)
1270 dbus_message_iter_get_basic(&entry, &netmask);
1271 } else if (g_str_equal(key, "Gateway") == TRUE) {
1272 if (type != DBUS_TYPE_STRING)
1275 dbus_message_iter_get_basic(&entry, &gateway);
1277 dbus_message_iter_next(&dict);
1280 DBG("method %d address %s netmask %s gateway %s",
1281 method, address, netmask, gateway);
1284 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1285 case CONNMAN_IPCONFIG_METHOD_OFF:
1286 case CONNMAN_IPCONFIG_METHOD_FIXED:
1289 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1290 if (address == NULL)
1293 ipconfig->method = method;
1294 connman_ipaddress_set(ipconfig->address,
1295 address, netmask, gateway);
1298 case CONNMAN_IPCONFIG_METHOD_DHCP:
1299 if (ipconfig->method == method)
1302 ipconfig->method = method;
1309 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1310 DBusMessageIter *iter)
1312 const char *method = "direct";
1314 connman_dbus_dict_append_basic(iter, "Method",
1315 DBUS_TYPE_STRING, &method);
1318 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1319 DBusMessageIter *iter)
1321 struct connman_ipdevice *ipdevice;
1322 const char *method = "auto";
1324 connman_dbus_dict_append_basic(iter, "Method",
1325 DBUS_TYPE_STRING, &method);
1327 ipdevice = g_hash_table_lookup(ipdevice_hash,
1328 GINT_TO_POINTER(ipconfig->index));
1329 if (ipdevice == NULL)
1332 if (ipdevice->ifname != NULL)
1333 connman_dbus_dict_append_basic(iter, "Interface",
1334 DBUS_TYPE_STRING, &ipdevice->ifname);
1336 if (ipdevice->address != NULL)
1337 connman_dbus_dict_append_basic(iter, "Address",
1338 DBUS_TYPE_STRING, &ipdevice->address);
1340 if (ipdevice->mtu > 0)
1341 connman_dbus_dict_append_basic(iter, "MTU",
1342 DBUS_TYPE_UINT16, &ipdevice->mtu);
1345 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1346 GKeyFile *keyfile, const char *identifier, const char *prefix)
1351 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1353 key = g_strdup_printf("%smethod", prefix);
1354 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1356 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1358 ipconfig->method = __connman_ipconfig_string2method(method);
1361 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1362 ipconfig->address->prefixlen = g_key_file_get_integer(
1363 keyfile, identifier, key, NULL);
1366 key = g_strdup_printf("%slocal_address", prefix);
1367 ipconfig->address->local = g_key_file_get_string(
1368 keyfile, identifier, key, NULL);
1371 key = g_strdup_printf("%speer_address", prefix);
1372 ipconfig->address->peer = g_key_file_get_string(
1373 keyfile, identifier, key, NULL);
1376 key = g_strdup_printf("%sbroadcast_address", prefix);
1377 ipconfig->address->broadcast = g_key_file_get_string(
1378 keyfile, identifier, key, NULL);
1381 key = g_strdup_printf("%sgateway", prefix);
1382 ipconfig->address->gateway = g_key_file_get_string(
1383 keyfile, identifier, key, NULL);
1389 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1390 GKeyFile *keyfile, const char *identifier, const char *prefix)
1395 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1397 method = __connman_ipconfig_method2string(ipconfig->method);
1399 key = g_strdup_printf("%smethod", prefix);
1400 g_key_file_set_string(keyfile, identifier, key, method);
1403 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1404 g_key_file_set_integer(keyfile, identifier,
1405 key, ipconfig->address->prefixlen);
1408 key = g_strdup_printf("%slocal_address", prefix);
1409 if (ipconfig->address->local != NULL)
1410 g_key_file_set_string(keyfile, identifier,
1411 key, ipconfig->address->local);
1414 key = g_strdup_printf("%speer_address", prefix);
1415 if (ipconfig->address->peer != NULL)
1416 g_key_file_set_string(keyfile, identifier,
1417 key, ipconfig->address->peer);
1420 key = g_strdup_printf("%sbroadcast_address", prefix);
1421 if (ipconfig->address->broadcast != NULL)
1422 g_key_file_set_string(keyfile, identifier,
1423 key, ipconfig->address->broadcast);
1426 key = g_strdup_printf("%sgateway", prefix);
1427 if (ipconfig->address->gateway != NULL)
1428 g_key_file_set_string(keyfile, identifier,
1429 key, ipconfig->address->gateway);
1435 int __connman_ipconfig_init(void)
1439 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1440 NULL, free_ipdevice);
1445 void __connman_ipconfig_cleanup(void)
1449 g_hash_table_destroy(ipdevice_hash);
1450 ipdevice_hash = NULL;