5 * Copyright (C) 2007-2009 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>
30 #define IFF_LOWER_UP 0x10000
37 struct connman_ipconfig {
41 struct connman_ipconfig *origin;
43 const struct connman_ipconfig_ops *ops;
46 enum connman_ipconfig_method method;
47 struct connman_ipaddress *address;
50 struct connman_ipdevice {
59 struct connman_ipconfig *config;
61 struct connman_ipconfig_driver *driver;
62 struct connman_ipconfig *driver_config;
65 static GHashTable *ipdevice_hash = NULL;
66 static GList *ipconfig_list = NULL;
68 struct connman_ipaddress *connman_ipaddress_alloc(void)
70 struct connman_ipaddress *ipaddress;
72 ipaddress = g_try_new0(struct connman_ipaddress, 1);
73 if (ipaddress == NULL)
79 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
81 if (ipaddress == NULL)
84 g_free(ipaddress->broadcast);
85 g_free(ipaddress->peer);
86 g_free(ipaddress->local);
90 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
91 struct connman_ipaddress *source)
93 if (ipaddress == NULL || source == NULL)
96 ipaddress->prefixlen = source->prefixlen;
98 g_free(ipaddress->local);
99 ipaddress->local = g_strdup(source->local);
101 g_free(ipaddress->peer);
102 ipaddress->peer = g_strdup(source->peer);
104 g_free(ipaddress->broadcast);
105 ipaddress->broadcast = g_strdup(source->broadcast);
108 static void free_address_list(struct connman_ipdevice *ipdevice)
112 for (list = ipdevice->address_list; list; list = list->next) {
113 struct connman_ipaddress *ipaddress = list->data;
115 connman_ipaddress_free(ipaddress);
119 g_slist_free(ipdevice->address_list);
120 ipdevice->address_list = NULL;
123 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
124 unsigned char prefixlen, const char *local)
128 for (list = ipdevice->address_list; list; list = list->next) {
129 struct connman_ipaddress *ipaddress = list->data;
131 if (g_strcmp0(ipaddress->local, local) == 0 &&
132 ipaddress->prefixlen == prefixlen)
139 static const char *type2str(unsigned short type)
144 case ARPHRD_LOOPBACK:
157 static const char *scope2str(unsigned char scope)
169 static void free_ipdevice(gpointer data)
171 struct connman_ipdevice *ipdevice = data;
173 connman_info("%s {remove} index %d", ipdevice->ifname,
176 if (ipdevice->config != NULL)
177 connman_ipconfig_unref(ipdevice->config);
179 free_address_list(ipdevice);
180 g_free(ipdevice->gateway);
182 g_free(ipdevice->ifname);
186 static GSList *driver_list = NULL;
188 static gint compare_priority(gconstpointer a, gconstpointer b)
190 const struct connman_ipconfig_driver *driver1 = a;
191 const struct connman_ipconfig_driver *driver2 = b;
193 return driver2->priority - driver1->priority;
197 * connman_ipconfig_driver_register:
198 * @driver: IP configuration driver
200 * Register a new IP configuration driver
202 * Returns: %0 on success
204 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
206 DBG("driver %p name %s", driver, driver->name);
208 driver_list = g_slist_insert_sorted(driver_list, driver,
215 * connman_ipconfig_driver_unregister:
216 * @driver: IP configuration driver
218 * Remove a previously registered IP configuration driver.
220 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
222 DBG("driver %p name %s", driver, driver->name);
224 driver_list = g_slist_remove(driver_list, driver);
227 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
231 DBG("ipconfig %p", ipdevice->config);
233 if (ipdevice->config == NULL)
236 switch (ipdevice->config->method) {
237 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
238 case CONNMAN_IPCONFIG_METHOD_IGNORE:
239 case CONNMAN_IPCONFIG_METHOD_STATIC:
241 case CONNMAN_IPCONFIG_METHOD_DHCP:
245 if (ipdevice->driver != NULL)
248 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
249 if (ipdevice->driver_config == NULL)
252 for (list = driver_list; list; list = list->next) {
253 struct connman_ipconfig_driver *driver = list->data;
255 if (driver->request(ipdevice->driver_config) == 0) {
256 ipdevice->driver = driver;
261 if (ipdevice->driver == NULL) {
262 connman_ipconfig_unref(ipdevice->driver_config);
263 ipdevice->driver_config = NULL;
267 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
269 DBG("ipconfig %p", ipdevice->config);
271 if (ipdevice->config == NULL)
274 if (ipdevice->driver == NULL)
277 ipdevice->driver->release(ipdevice->driver_config);
279 ipdevice->driver = NULL;
281 connman_ipconfig_unref(ipdevice->driver_config);
282 ipdevice->driver_config = NULL;
284 connman_inet_clear_address(ipdevice->index);
287 void __connman_ipconfig_newlink(int index, unsigned short type,
290 struct connman_ipdevice *ipdevice;
293 gboolean up = FALSE, down = FALSE;
294 gboolean lower_up = FALSE, lower_down = FALSE;
296 DBG("index %d", index);
298 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
299 if (ipdevice != NULL)
302 ipdevice = g_try_new0(struct connman_ipdevice, 1);
303 if (ipdevice == NULL)
306 ipdevice->index = index;
307 ipdevice->ifname = connman_inet_ifname(index);
308 ipdevice->type = type;
310 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
312 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
313 index, type, type2str(type));
316 if (flags == ipdevice->flags)
319 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
326 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
327 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
328 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
329 (IFF_RUNNING | IFF_LOWER_UP))
331 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
335 connman_inet_clear_address(index);
337 ipdevice->flags = flags;
339 str = g_string_new(NULL);
344 g_string_append(str, "UP");
346 g_string_append(str, "DOWN");
348 if (flags & IFF_RUNNING)
349 g_string_append(str, ",RUNNING");
351 if (flags & IFF_LOWER_UP)
352 g_string_append(str, ",LOWER_UP");
354 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
357 g_string_free(str, TRUE);
359 for (list = g_list_first(ipconfig_list); list;
360 list = g_list_next(list)) {
361 struct connman_ipconfig *ipconfig = list->data;
363 if (index != ipconfig->index)
366 if (up == TRUE && ipconfig->ops->up)
367 ipconfig->ops->up(ipconfig);
368 if (lower_up == TRUE && ipconfig->ops->lower_up)
369 ipconfig->ops->lower_up(ipconfig);
371 if (lower_down == TRUE && ipconfig->ops->lower_down)
372 ipconfig->ops->lower_down(ipconfig);
373 if (down == TRUE && ipconfig->ops->down)
374 ipconfig->ops->down(ipconfig);
378 __connman_ipconfig_lower_up(ipdevice);
380 __connman_ipconfig_lower_down(ipdevice);
383 void __connman_ipconfig_dellink(int index)
385 struct connman_ipdevice *ipdevice;
388 DBG("index %d", index);
390 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
391 if (ipdevice == NULL)
394 for (list = g_list_first(ipconfig_list); list;
395 list = g_list_next(list)) {
396 struct connman_ipconfig *ipconfig = list->data;
398 if (index != ipconfig->index)
401 ipconfig->index = -1;
403 if (ipconfig->ops->lower_down)
404 ipconfig->ops->lower_down(ipconfig);
405 if (ipconfig->ops->down)
406 ipconfig->ops->down(ipconfig);
409 __connman_ipconfig_lower_down(ipdevice);
411 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
414 void __connman_ipconfig_newaddr(int index, const char *label,
415 unsigned char prefixlen, const char *address)
417 struct connman_ipdevice *ipdevice;
418 struct connman_ipaddress *ipaddress;
421 DBG("index %d", index);
423 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
424 if (ipdevice == NULL)
427 ipaddress = connman_ipaddress_alloc();
428 if (ipaddress == NULL)
431 ipaddress->prefixlen = prefixlen;
432 ipaddress->local = g_strdup(address);
434 ipdevice->address_list = g_slist_append(ipdevice->address_list,
437 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
438 address, prefixlen, label);
440 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
443 if (g_slist_length(ipdevice->address_list) > 1)
446 for (list = g_list_first(ipconfig_list); list;
447 list = g_list_next(list)) {
448 struct connman_ipconfig *ipconfig = list->data;
450 if (index != ipconfig->index)
453 if (ipconfig->ops->ip_bound)
454 ipconfig->ops->ip_bound(ipconfig);
458 void __connman_ipconfig_deladdr(int index, const char *label,
459 unsigned char prefixlen, const char *address)
461 struct connman_ipdevice *ipdevice;
462 struct connman_ipaddress *ipaddress;
465 DBG("index %d", index);
467 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
468 if (ipdevice == NULL)
471 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
472 if (ipaddress == NULL)
475 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
478 connman_ipaddress_free(ipaddress);
480 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
481 address, prefixlen, label);
483 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
486 if (g_slist_length(ipdevice->address_list) > 0)
489 for (list = g_list_first(ipconfig_list); list;
490 list = g_list_next(list)) {
491 struct connman_ipconfig *ipconfig = list->data;
493 if (index != ipconfig->index)
496 if (ipconfig->ops->ip_release)
497 ipconfig->ops->ip_release(ipconfig);
501 void __connman_ipconfig_newroute(int index, unsigned char scope,
502 const char *dst, const char *gateway)
504 struct connman_ipdevice *ipdevice;
506 DBG("index %d", index);
508 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
509 if (ipdevice == NULL)
512 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
513 g_free(ipdevice->gateway);
514 ipdevice->gateway = g_strdup(gateway);
517 connman_info("%s {add} route %s gw %s scope %u <%s>",
518 ipdevice->ifname, dst, gateway,
519 scope, scope2str(scope));
522 void __connman_ipconfig_delroute(int index, unsigned char scope,
523 const char *dst, const char *gateway)
525 struct connman_ipdevice *ipdevice;
527 DBG("index %d", index);
529 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
530 if (ipdevice == NULL)
533 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
534 g_free(ipdevice->gateway);
535 ipdevice->gateway = NULL;
538 connman_info("%s {del} route %s gw %s scope %u <%s>",
539 ipdevice->ifname, dst, gateway,
540 scope, scope2str(scope));
543 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
548 keys = g_hash_table_get_keys(ipdevice_hash);
552 for (list = g_list_first(keys); list; list = g_list_next(list)) {
553 int index = GPOINTER_TO_INT(list->data);
555 function(index, user_data);
561 unsigned short __connman_ipconfig_get_type(int index)
563 struct connman_ipdevice *ipdevice;
565 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
566 if (ipdevice == NULL)
569 return ipdevice->type;
572 unsigned int __connman_ipconfig_get_flags(int index)
574 struct connman_ipdevice *ipdevice;
576 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
577 if (ipdevice == NULL)
580 return ipdevice->flags;
583 const char *__connman_ipconfig_get_gateway(int index)
585 struct connman_ipdevice *ipdevice;
587 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
588 if (ipdevice == NULL)
591 return ipdevice->gateway;
595 * connman_ipconfig_create:
597 * Allocate a new ipconfig structure.
599 * Returns: a newly-allocated #connman_ipconfig structure
601 struct connman_ipconfig *connman_ipconfig_create(int index)
603 struct connman_ipconfig *ipconfig;
605 DBG("index %d", index);
607 ipconfig = g_try_new0(struct connman_ipconfig, 1);
608 if (ipconfig == NULL)
611 ipconfig->refcount = 1;
613 ipconfig->index = index;
615 ipconfig->address = connman_ipaddress_alloc();
616 if (ipconfig->address == NULL) {
621 DBG("ipconfig %p", ipconfig);
627 * connman_ipconfig_clone:
629 * Clone an ipconfig structure and create new reference.
631 * Returns: a newly-allocated #connman_ipconfig structure
633 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
635 struct connman_ipconfig *ipconfig_clone;
637 DBG("ipconfig %p", ipconfig);
639 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
640 if (ipconfig_clone == NULL)
643 ipconfig_clone->refcount = 1;
645 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
647 ipconfig_clone->index = -1;
649 return ipconfig_clone;
653 * connman_ipconfig_ref:
654 * @ipconfig: ipconfig structure
656 * Increase reference counter of ipconfig
658 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
660 g_atomic_int_inc(&ipconfig->refcount);
666 * connman_ipconfig_unref:
667 * @ipconfig: ipconfig structure
669 * Decrease reference counter of ipconfig
671 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
673 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
674 connman_ipconfig_set_ops(ipconfig, NULL);
676 if (ipconfig->origin != NULL) {
677 connman_ipconfig_unref(ipconfig->origin);
678 ipconfig->origin = NULL;
681 connman_ipaddress_free(ipconfig->address);
687 * connman_ipconfig_get_data:
688 * @ipconfig: ipconfig structure
690 * Get private data pointer
692 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
694 return ipconfig->ops_data;
698 * connman_ipconfig_set_data:
699 * @ipconfig: ipconfig structure
700 * @data: data pointer
702 * Set private data pointer
704 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
706 ipconfig->ops_data = data;
710 * connman_ipconfig_get_index:
711 * @ipconfig: ipconfig structure
713 * Get interface index
715 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
717 if (ipconfig->origin != NULL)
718 return ipconfig->origin->index;
720 return ipconfig->index;
724 * connman_ipconfig_get_ifname:
725 * @ipconfig: ipconfig structure
729 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
731 struct connman_ipdevice *ipdevice;
733 if (ipconfig->index < 0)
736 ipdevice = g_hash_table_lookup(ipdevice_hash,
737 GINT_TO_POINTER(ipconfig->index));
738 if (ipdevice == NULL)
741 return ipdevice->ifname;
745 * connman_ipconfig_set_ops:
746 * @ipconfig: ipconfig structure
747 * @ops: operation callbacks
749 * Set the operation callbacks
751 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
752 const struct connman_ipconfig_ops *ops)
754 if (ipconfig->ops != NULL)
755 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
760 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
764 * connman_ipconfig_set_method:
765 * @ipconfig: ipconfig structure
766 * @method: configuration method
768 * Set the configuration method
770 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
771 enum connman_ipconfig_method method)
773 ipconfig->method = method;
779 * connman_ipconfig_bind:
780 * @ipconfig: ipconfig structure
781 * @ipaddress: ipaddress structure
783 * Bind IP address details to configuration
785 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
786 struct connman_ipaddress *ipaddress)
788 struct connman_ipconfig *origin;
790 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
792 connman_ipaddress_copy(origin->address, ipaddress);
794 connman_inet_set_address(origin->index, origin->address);
797 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
799 struct connman_ipdevice *ipdevice;
801 DBG("ipconfig %p", ipconfig);
803 if (ipconfig == NULL || ipconfig->index < 0)
806 ipdevice = g_hash_table_lookup(ipdevice_hash,
807 GINT_TO_POINTER(ipconfig->index));
808 if (ipdevice == NULL)
811 if (ipdevice->config != NULL)
812 connman_ipconfig_unref(ipdevice->config);
814 ipdevice->config = connman_ipconfig_ref(ipconfig);
819 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
821 struct connman_ipdevice *ipdevice;
823 DBG("ipconfig %p", ipconfig);
825 if (ipconfig == NULL || ipconfig->index < 0)
828 ipdevice = g_hash_table_lookup(ipdevice_hash,
829 GINT_TO_POINTER(ipconfig->index));
830 if (ipdevice == NULL)
833 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
836 connman_ipconfig_unref(ipdevice->config);
837 ipdevice->config = NULL;
842 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
845 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
847 case CONNMAN_IPCONFIG_METHOD_IGNORE:
849 case CONNMAN_IPCONFIG_METHOD_STATIC:
851 case CONNMAN_IPCONFIG_METHOD_DHCP:
858 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
860 if (g_strcmp0(method, "ignore") == 0)
861 return CONNMAN_IPCONFIG_METHOD_IGNORE;
862 else if (g_strcmp0(method, "static") == 0)
863 return CONNMAN_IPCONFIG_METHOD_STATIC;
864 else if (g_strcmp0(method, "dhcp") == 0)
865 return CONNMAN_IPCONFIG_METHOD_DHCP;
867 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
870 static void append_variant(DBusMessageIter *iter, const char *prefix,
871 const char *key, int type, void *val)
875 if (prefix == NULL) {
876 connman_dbus_dict_append_variant(iter, key, type, val);
880 str = g_strdup_printf("%s%s", prefix, key);
882 connman_dbus_dict_append_variant(iter, str, type, val);
887 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
888 DBusMessageIter *iter, const char *prefix)
892 str = __connman_ipconfig_method2string(ipconfig->method);
896 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
899 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
900 const char *key, DBusMessageIter *value)
902 int type = dbus_message_iter_get_arg_type(value);
904 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
906 if (g_strcmp0(key, "Method") == 0) {
909 if (type != DBUS_TYPE_STRING)
912 dbus_message_iter_get_basic(value, &method);
914 ipconfig->method = __connman_ipconfig_string2method(method);
921 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
922 GKeyFile *keyfile, const char *identifier, const char *prefix)
924 DBG("ipconfig %p identifier %s", ipconfig, identifier);
929 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
930 GKeyFile *keyfile, const char *identifier, const char *prefix)
932 DBG("ipconfig %p identifier %s", ipconfig, identifier);
937 int __connman_ipconfig_init(void)
941 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
942 NULL, free_ipdevice);
947 void __connman_ipconfig_cleanup(void)
951 g_hash_table_destroy(ipdevice_hash);
952 ipdevice_hash = NULL;