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 if (ipdevice->gateway != NULL)
750 return ipdevice->gateway;
752 if (ipdevice->config != NULL &&
753 ipdevice->config->address != NULL)
754 return ipdevice->config->address->gateway;
759 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
761 ipconfig->index = index;
765 * connman_ipconfig_create:
767 * Allocate a new ipconfig structure.
769 * Returns: a newly-allocated #connman_ipconfig structure
771 struct connman_ipconfig *connman_ipconfig_create(int index)
773 struct connman_ipconfig *ipconfig;
775 DBG("index %d", index);
777 ipconfig = g_try_new0(struct connman_ipconfig, 1);
778 if (ipconfig == NULL)
781 ipconfig->refcount = 1;
783 ipconfig->index = index;
785 ipconfig->address = connman_ipaddress_alloc();
786 if (ipconfig->address == NULL) {
791 ipconfig->system = connman_ipaddress_alloc();
793 DBG("ipconfig %p", ipconfig);
799 * connman_ipconfig_clone:
801 * Clone an ipconfig structure and create new reference.
803 * Returns: a newly-allocated #connman_ipconfig structure
805 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
807 struct connman_ipconfig *ipconfig_clone;
809 DBG("ipconfig %p", ipconfig);
811 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
812 if (ipconfig_clone == NULL)
815 ipconfig_clone->refcount = 1;
817 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
819 ipconfig_clone->index = -1;
821 return ipconfig_clone;
825 * connman_ipconfig_ref:
826 * @ipconfig: ipconfig structure
828 * Increase reference counter of ipconfig
830 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
832 g_atomic_int_inc(&ipconfig->refcount);
838 * connman_ipconfig_unref:
839 * @ipconfig: ipconfig structure
841 * Decrease reference counter of ipconfig
843 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
845 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
846 __connman_ipconfig_disable(ipconfig);
848 connman_ipconfig_set_ops(ipconfig, NULL);
850 if (ipconfig->origin != NULL) {
851 connman_ipconfig_unref(ipconfig->origin);
852 ipconfig->origin = NULL;
855 connman_ipaddress_free(ipconfig->system);
856 connman_ipaddress_free(ipconfig->address);
862 * connman_ipconfig_get_data:
863 * @ipconfig: ipconfig structure
865 * Get private data pointer
867 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
869 return ipconfig->ops_data;
873 * connman_ipconfig_set_data:
874 * @ipconfig: ipconfig structure
875 * @data: data pointer
877 * Set private data pointer
879 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
881 ipconfig->ops_data = data;
885 * connman_ipconfig_get_index:
886 * @ipconfig: ipconfig structure
888 * Get interface index
890 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
892 if (ipconfig == NULL)
895 if (ipconfig->origin != NULL)
896 return ipconfig->origin->index;
898 return ipconfig->index;
902 * connman_ipconfig_get_ifname:
903 * @ipconfig: ipconfig structure
907 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
909 struct connman_ipdevice *ipdevice;
911 if (ipconfig == NULL)
914 if (ipconfig->index < 0)
917 ipdevice = g_hash_table_lookup(ipdevice_hash,
918 GINT_TO_POINTER(ipconfig->index));
919 if (ipdevice == NULL)
922 return ipdevice->ifname;
926 * connman_ipconfig_set_ops:
927 * @ipconfig: ipconfig structure
928 * @ops: operation callbacks
930 * Set the operation callbacks
932 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
933 const struct connman_ipconfig_ops *ops)
939 * connman_ipconfig_set_method:
940 * @ipconfig: ipconfig structure
941 * @method: configuration method
943 * Set the configuration method
945 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
946 enum connman_ipconfig_method method)
948 ipconfig->method = method;
953 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
955 if (ipconfig == NULL)
956 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
958 return ipconfig->method;
962 * connman_ipconfig_bind:
963 * @ipconfig: ipconfig structure
964 * @ipaddress: ipaddress structure
966 * Bind IP address details to configuration
968 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
969 struct connman_ipaddress *ipaddress)
971 struct connman_ipconfig *origin;
973 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
975 connman_ipaddress_copy(origin->address, ipaddress);
977 connman_inet_set_address(origin->index, origin->address);
980 /* FIXME: The element soulution should be removed in the future */
981 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
982 struct connman_element *parent)
984 struct connman_element *connection;
986 connection = connman_element_create(NULL);
988 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
989 connection->index = ipconfig->index;
990 connection->ipv4.gateway = ipconfig->address->gateway;
992 if (connman_element_register(connection, parent) < 0)
993 connman_element_unref(connection);
998 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1002 switch (ipconfig->method) {
1003 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1004 case CONNMAN_IPCONFIG_METHOD_OFF:
1005 case CONNMAN_IPCONFIG_METHOD_FIXED:
1006 case CONNMAN_IPCONFIG_METHOD_DHCP:
1008 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1009 return connman_inet_set_address(ipconfig->index,
1016 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1020 if (ipconfig == NULL)
1023 DBG("method %d", ipconfig->method);
1025 switch (ipconfig->method) {
1026 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1027 case CONNMAN_IPCONFIG_METHOD_OFF:
1028 case CONNMAN_IPCONFIG_METHOD_FIXED:
1029 case CONNMAN_IPCONFIG_METHOD_DHCP:
1031 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1032 return connman_inet_clear_address(ipconfig->index);
1039 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1041 struct connman_ipdevice *ipdevice;
1042 gboolean up = FALSE, down = FALSE;
1043 gboolean lower_up = FALSE, lower_down = FALSE;
1045 DBG("ipconfig %p", ipconfig);
1047 if (ipconfig == NULL || ipconfig->index < 0)
1050 ipdevice = g_hash_table_lookup(ipdevice_hash,
1051 GINT_TO_POINTER(ipconfig->index));
1052 if (ipdevice == NULL)
1055 if (ipdevice->config == ipconfig)
1058 if (ipdevice->config != NULL) {
1059 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1061 connman_ipaddress_clear(ipdevice->config->system);
1063 connman_ipconfig_unref(ipdevice->config);
1066 ipdevice->config = connman_ipconfig_ref(ipconfig);
1068 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1070 if (ipdevice->flags & IFF_UP)
1075 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1076 (IFF_RUNNING | IFF_LOWER_UP))
1078 else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1081 if (up == TRUE && ipconfig->ops->up)
1082 ipconfig->ops->up(ipconfig);
1083 if (lower_up == TRUE && ipconfig->ops->lower_up)
1084 ipconfig->ops->lower_up(ipconfig);
1086 if (lower_down == TRUE && ipconfig->ops->lower_down)
1087 ipconfig->ops->lower_down(ipconfig);
1088 if (down == TRUE && ipconfig->ops->down)
1089 ipconfig->ops->down(ipconfig);
1094 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1096 struct connman_ipdevice *ipdevice;
1098 DBG("ipconfig %p", ipconfig);
1100 if (ipconfig == NULL || ipconfig->index < 0)
1103 ipdevice = g_hash_table_lookup(ipdevice_hash,
1104 GINT_TO_POINTER(ipconfig->index));
1105 if (ipdevice == NULL)
1108 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1111 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1113 connman_ipaddress_clear(ipdevice->config->system);
1115 connman_ipconfig_unref(ipdevice->config);
1116 ipdevice->config = NULL;
1121 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1124 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1126 case CONNMAN_IPCONFIG_METHOD_OFF:
1128 case CONNMAN_IPCONFIG_METHOD_FIXED:
1130 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1132 case CONNMAN_IPCONFIG_METHOD_DHCP:
1139 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1141 if (g_strcmp0(method, "off") == 0)
1142 return CONNMAN_IPCONFIG_METHOD_OFF;
1143 else if (g_strcmp0(method, "fixed") == 0)
1144 return CONNMAN_IPCONFIG_METHOD_FIXED;
1145 else if (g_strcmp0(method, "manual") == 0)
1146 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1147 else if (g_strcmp0(method, "dhcp") == 0)
1148 return CONNMAN_IPCONFIG_METHOD_DHCP;
1150 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1153 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1154 DBusMessageIter *iter)
1158 str = __connman_ipconfig_method2string(ipconfig->method);
1162 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1164 if (ipconfig->system == NULL)
1167 if (ipconfig->system->local != NULL) {
1169 struct in_addr netmask;
1172 connman_dbus_dict_append_basic(iter, "Address",
1173 DBUS_TYPE_STRING, &ipconfig->system->local);
1175 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1176 netmask.s_addr = htonl(addr);
1177 mask = inet_ntoa(netmask);
1178 connman_dbus_dict_append_basic(iter, "Netmask",
1179 DBUS_TYPE_STRING, &mask);
1182 if (ipconfig->system->gateway != NULL)
1183 connman_dbus_dict_append_basic(iter, "Gateway",
1184 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1187 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1188 DBusMessageIter *iter)
1192 str = __connman_ipconfig_method2string(ipconfig->method);
1196 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1198 switch (ipconfig->method) {
1199 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1200 case CONNMAN_IPCONFIG_METHOD_OFF:
1201 case CONNMAN_IPCONFIG_METHOD_FIXED:
1202 case CONNMAN_IPCONFIG_METHOD_DHCP:
1204 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1208 if (ipconfig->address == NULL)
1211 if (ipconfig->address->local != NULL) {
1213 struct in_addr netmask;
1216 connman_dbus_dict_append_basic(iter, "Address",
1217 DBUS_TYPE_STRING, &ipconfig->address->local);
1219 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1220 netmask.s_addr = htonl(addr);
1221 mask = inet_ntoa(netmask);
1222 connman_dbus_dict_append_basic(iter, "Netmask",
1223 DBUS_TYPE_STRING, &mask);
1226 if (ipconfig->address->gateway != NULL)
1227 connman_dbus_dict_append_basic(iter, "Gateway",
1228 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1231 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1232 DBusMessageIter *array)
1234 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1235 const char *address = NULL, *netmask = NULL, *gateway = NULL;
1236 DBusMessageIter dict;
1238 DBG("ipconfig %p", ipconfig);
1240 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1243 dbus_message_iter_recurse(array, &dict);
1245 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1246 DBusMessageIter entry;
1250 dbus_message_iter_recurse(&dict, &entry);
1252 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1255 dbus_message_iter_get_basic(&entry, &key);
1256 dbus_message_iter_next(&entry);
1258 type = dbus_message_iter_get_arg_type(&entry);
1260 if (g_str_equal(key, "Method") == TRUE) {
1263 if (type != DBUS_TYPE_STRING)
1266 dbus_message_iter_get_basic(&entry, &str);
1267 method = __connman_ipconfig_string2method(str);
1268 } else if (g_str_equal(key, "Address") == TRUE) {
1269 if (type != DBUS_TYPE_STRING)
1272 dbus_message_iter_get_basic(&entry, &address);
1273 } else if (g_str_equal(key, "Netmask") == TRUE) {
1274 if (type != DBUS_TYPE_STRING)
1277 dbus_message_iter_get_basic(&entry, &netmask);
1278 } else if (g_str_equal(key, "Gateway") == TRUE) {
1279 if (type != DBUS_TYPE_STRING)
1282 dbus_message_iter_get_basic(&entry, &gateway);
1284 dbus_message_iter_next(&dict);
1287 DBG("method %d address %s netmask %s gateway %s",
1288 method, address, netmask, gateway);
1291 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1292 case CONNMAN_IPCONFIG_METHOD_OFF:
1293 case CONNMAN_IPCONFIG_METHOD_FIXED:
1296 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1297 if (address == NULL)
1300 ipconfig->method = method;
1301 connman_ipaddress_set(ipconfig->address,
1302 address, netmask, gateway);
1305 case CONNMAN_IPCONFIG_METHOD_DHCP:
1306 if (ipconfig->method == method)
1309 ipconfig->method = method;
1316 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1317 DBusMessageIter *iter)
1319 const char *method = "direct";
1321 connman_dbus_dict_append_basic(iter, "Method",
1322 DBUS_TYPE_STRING, &method);
1325 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1326 DBusMessageIter *iter)
1328 struct connman_ipdevice *ipdevice;
1329 const char *method = "auto";
1331 connman_dbus_dict_append_basic(iter, "Method",
1332 DBUS_TYPE_STRING, &method);
1334 ipdevice = g_hash_table_lookup(ipdevice_hash,
1335 GINT_TO_POINTER(ipconfig->index));
1336 if (ipdevice == NULL)
1339 if (ipdevice->ifname != NULL)
1340 connman_dbus_dict_append_basic(iter, "Interface",
1341 DBUS_TYPE_STRING, &ipdevice->ifname);
1343 if (ipdevice->address != NULL)
1344 connman_dbus_dict_append_basic(iter, "Address",
1345 DBUS_TYPE_STRING, &ipdevice->address);
1347 if (ipdevice->mtu > 0)
1348 connman_dbus_dict_append_basic(iter, "MTU",
1349 DBUS_TYPE_UINT16, &ipdevice->mtu);
1352 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1353 GKeyFile *keyfile, const char *identifier, const char *prefix)
1358 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1360 key = g_strdup_printf("%smethod", prefix);
1361 method = g_key_file_get_string(keyfile, identifier, key, NULL);
1363 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1365 ipconfig->method = __connman_ipconfig_string2method(method);
1368 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1369 ipconfig->address->prefixlen = g_key_file_get_integer(
1370 keyfile, identifier, key, NULL);
1373 key = g_strdup_printf("%slocal_address", prefix);
1374 ipconfig->address->local = g_key_file_get_string(
1375 keyfile, identifier, key, NULL);
1378 key = g_strdup_printf("%speer_address", prefix);
1379 ipconfig->address->peer = g_key_file_get_string(
1380 keyfile, identifier, key, NULL);
1383 key = g_strdup_printf("%sbroadcast_address", prefix);
1384 ipconfig->address->broadcast = g_key_file_get_string(
1385 keyfile, identifier, key, NULL);
1388 key = g_strdup_printf("%sgateway", prefix);
1389 ipconfig->address->gateway = g_key_file_get_string(
1390 keyfile, identifier, key, NULL);
1396 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1397 GKeyFile *keyfile, const char *identifier, const char *prefix)
1402 DBG("ipconfig %p identifier %s", ipconfig, identifier);
1404 method = __connman_ipconfig_method2string(ipconfig->method);
1406 key = g_strdup_printf("%smethod", prefix);
1407 g_key_file_set_string(keyfile, identifier, key, method);
1410 key = g_strdup_printf("%snetmask_prefixlen", prefix);
1411 g_key_file_set_integer(keyfile, identifier,
1412 key, ipconfig->address->prefixlen);
1415 key = g_strdup_printf("%slocal_address", prefix);
1416 if (ipconfig->address->local != NULL)
1417 g_key_file_set_string(keyfile, identifier,
1418 key, ipconfig->address->local);
1421 key = g_strdup_printf("%speer_address", prefix);
1422 if (ipconfig->address->peer != NULL)
1423 g_key_file_set_string(keyfile, identifier,
1424 key, ipconfig->address->peer);
1427 key = g_strdup_printf("%sbroadcast_address", prefix);
1428 if (ipconfig->address->broadcast != NULL)
1429 g_key_file_set_string(keyfile, identifier,
1430 key, ipconfig->address->broadcast);
1433 key = g_strdup_printf("%sgateway", prefix);
1434 if (ipconfig->address->gateway != NULL)
1435 g_key_file_set_string(keyfile, identifier,
1436 key, ipconfig->address->gateway);
1442 int __connman_ipconfig_init(void)
1446 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1447 NULL, free_ipdevice);
1452 void __connman_ipconfig_cleanup(void)
1456 g_hash_table_destroy(ipdevice_hash);
1457 ipdevice_hash = NULL;