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;
297 DBG("index %d", index);
299 if (type == ARPHRD_LOOPBACK)
302 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
303 if (ipdevice != NULL)
306 ifname = connman_inet_ifname(index);
308 if (__connman_element_device_isfiltered(ifname) == TRUE) {
309 connman_info("Ignoring interface %s (filtered)", ifname);
314 ipdevice = g_try_new0(struct connman_ipdevice, 1);
315 if (ipdevice == NULL) {
320 ipdevice->index = index;
321 ipdevice->ifname = ifname;
322 ipdevice->type = type;
324 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
326 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
327 index, type, type2str(type));
330 if (flags == ipdevice->flags)
333 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
340 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
341 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
342 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
343 (IFF_RUNNING | IFF_LOWER_UP))
345 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
349 ipdevice->flags = flags;
351 str = g_string_new(NULL);
356 g_string_append(str, "UP");
358 g_string_append(str, "DOWN");
360 if (flags & IFF_RUNNING)
361 g_string_append(str, ",RUNNING");
363 if (flags & IFF_LOWER_UP)
364 g_string_append(str, ",LOWER_UP");
366 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
369 g_string_free(str, TRUE);
371 for (list = g_list_first(ipconfig_list); list;
372 list = g_list_next(list)) {
373 struct connman_ipconfig *ipconfig = list->data;
375 if (index != ipconfig->index)
378 if (up == TRUE && ipconfig->ops->up)
379 ipconfig->ops->up(ipconfig);
380 if (lower_up == TRUE && ipconfig->ops->lower_up)
381 ipconfig->ops->lower_up(ipconfig);
383 if (lower_down == TRUE && ipconfig->ops->lower_down)
384 ipconfig->ops->lower_down(ipconfig);
385 if (down == TRUE && ipconfig->ops->down)
386 ipconfig->ops->down(ipconfig);
390 __connman_ipconfig_lower_up(ipdevice);
392 __connman_ipconfig_lower_down(ipdevice);
395 void __connman_ipconfig_dellink(int index)
397 struct connman_ipdevice *ipdevice;
400 DBG("index %d", index);
402 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
403 if (ipdevice == NULL)
406 for (list = g_list_first(ipconfig_list); list;
407 list = g_list_next(list)) {
408 struct connman_ipconfig *ipconfig = list->data;
410 if (index != ipconfig->index)
413 ipconfig->index = -1;
415 if (ipconfig->ops->lower_down)
416 ipconfig->ops->lower_down(ipconfig);
417 if (ipconfig->ops->down)
418 ipconfig->ops->down(ipconfig);
421 __connman_ipconfig_lower_down(ipdevice);
423 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
426 void __connman_ipconfig_newaddr(int index, const char *label,
427 unsigned char prefixlen, const char *address)
429 struct connman_ipdevice *ipdevice;
430 struct connman_ipaddress *ipaddress;
433 DBG("index %d", index);
435 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
436 if (ipdevice == NULL)
439 ipaddress = connman_ipaddress_alloc();
440 if (ipaddress == NULL)
443 ipaddress->prefixlen = prefixlen;
444 ipaddress->local = g_strdup(address);
446 ipdevice->address_list = g_slist_append(ipdevice->address_list,
449 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
450 address, prefixlen, label);
452 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
455 if (g_slist_length(ipdevice->address_list) > 1)
458 for (list = g_list_first(ipconfig_list); list;
459 list = g_list_next(list)) {
460 struct connman_ipconfig *ipconfig = list->data;
462 if (index != ipconfig->index)
465 if (ipconfig->ops->ip_bound)
466 ipconfig->ops->ip_bound(ipconfig);
470 void __connman_ipconfig_deladdr(int index, const char *label,
471 unsigned char prefixlen, const char *address)
473 struct connman_ipdevice *ipdevice;
474 struct connman_ipaddress *ipaddress;
477 DBG("index %d", index);
479 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
480 if (ipdevice == NULL)
483 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
484 if (ipaddress == NULL)
487 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
490 connman_ipaddress_free(ipaddress);
492 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
493 address, prefixlen, label);
495 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
498 if (g_slist_length(ipdevice->address_list) > 0)
501 for (list = g_list_first(ipconfig_list); list;
502 list = g_list_next(list)) {
503 struct connman_ipconfig *ipconfig = list->data;
505 if (index != ipconfig->index)
508 if (ipconfig->ops->ip_release)
509 ipconfig->ops->ip_release(ipconfig);
513 void __connman_ipconfig_newroute(int index, unsigned char scope,
514 const char *dst, const char *gateway)
516 struct connman_ipdevice *ipdevice;
518 DBG("index %d", index);
520 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
521 if (ipdevice == NULL)
524 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
525 g_free(ipdevice->gateway);
526 ipdevice->gateway = g_strdup(gateway);
529 connman_info("%s {add} route %s gw %s scope %u <%s>",
530 ipdevice->ifname, dst, gateway,
531 scope, scope2str(scope));
534 void __connman_ipconfig_delroute(int index, unsigned char scope,
535 const char *dst, const char *gateway)
537 struct connman_ipdevice *ipdevice;
539 DBG("index %d", index);
541 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
542 if (ipdevice == NULL)
545 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
546 g_free(ipdevice->gateway);
547 ipdevice->gateway = NULL;
550 connman_info("%s {del} route %s gw %s scope %u <%s>",
551 ipdevice->ifname, dst, gateway,
552 scope, scope2str(scope));
555 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
560 keys = g_hash_table_get_keys(ipdevice_hash);
564 for (list = g_list_first(keys); list; list = g_list_next(list)) {
565 int index = GPOINTER_TO_INT(list->data);
567 function(index, user_data);
573 unsigned short __connman_ipconfig_get_type(int index)
575 struct connman_ipdevice *ipdevice;
577 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
578 if (ipdevice == NULL)
581 return ipdevice->type;
584 unsigned int __connman_ipconfig_get_flags(int index)
586 struct connman_ipdevice *ipdevice;
588 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
589 if (ipdevice == NULL)
592 return ipdevice->flags;
595 const char *__connman_ipconfig_get_gateway(int index)
597 struct connman_ipdevice *ipdevice;
599 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
600 if (ipdevice == NULL)
603 return ipdevice->gateway;
607 * connman_ipconfig_create:
609 * Allocate a new ipconfig structure.
611 * Returns: a newly-allocated #connman_ipconfig structure
613 struct connman_ipconfig *connman_ipconfig_create(int index)
615 struct connman_ipconfig *ipconfig;
617 DBG("index %d", index);
619 ipconfig = g_try_new0(struct connman_ipconfig, 1);
620 if (ipconfig == NULL)
623 ipconfig->refcount = 1;
625 ipconfig->index = index;
627 ipconfig->address = connman_ipaddress_alloc();
628 if (ipconfig->address == NULL) {
633 DBG("ipconfig %p", ipconfig);
639 * connman_ipconfig_clone:
641 * Clone an ipconfig structure and create new reference.
643 * Returns: a newly-allocated #connman_ipconfig structure
645 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
647 struct connman_ipconfig *ipconfig_clone;
649 DBG("ipconfig %p", ipconfig);
651 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
652 if (ipconfig_clone == NULL)
655 ipconfig_clone->refcount = 1;
657 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
659 ipconfig_clone->index = -1;
661 return ipconfig_clone;
665 * connman_ipconfig_ref:
666 * @ipconfig: ipconfig structure
668 * Increase reference counter of ipconfig
670 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
672 g_atomic_int_inc(&ipconfig->refcount);
678 * connman_ipconfig_unref:
679 * @ipconfig: ipconfig structure
681 * Decrease reference counter of ipconfig
683 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
685 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
686 connman_ipconfig_set_ops(ipconfig, NULL);
688 if (ipconfig->origin != NULL) {
689 connman_ipconfig_unref(ipconfig->origin);
690 ipconfig->origin = NULL;
693 connman_ipaddress_free(ipconfig->address);
699 * connman_ipconfig_get_data:
700 * @ipconfig: ipconfig structure
702 * Get private data pointer
704 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
706 return ipconfig->ops_data;
710 * connman_ipconfig_set_data:
711 * @ipconfig: ipconfig structure
712 * @data: data pointer
714 * Set private data pointer
716 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
718 ipconfig->ops_data = data;
722 * connman_ipconfig_get_index:
723 * @ipconfig: ipconfig structure
725 * Get interface index
727 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
729 if (ipconfig->origin != NULL)
730 return ipconfig->origin->index;
732 return ipconfig->index;
736 * connman_ipconfig_get_ifname:
737 * @ipconfig: ipconfig structure
741 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
743 struct connman_ipdevice *ipdevice;
745 if (ipconfig->index < 0)
748 ipdevice = g_hash_table_lookup(ipdevice_hash,
749 GINT_TO_POINTER(ipconfig->index));
750 if (ipdevice == NULL)
753 return ipdevice->ifname;
757 * connman_ipconfig_set_ops:
758 * @ipconfig: ipconfig structure
759 * @ops: operation callbacks
761 * Set the operation callbacks
763 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
764 const struct connman_ipconfig_ops *ops)
766 if (ipconfig->ops != NULL)
767 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
772 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
776 * connman_ipconfig_set_method:
777 * @ipconfig: ipconfig structure
778 * @method: configuration method
780 * Set the configuration method
782 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
783 enum connman_ipconfig_method method)
785 ipconfig->method = method;
791 * connman_ipconfig_bind:
792 * @ipconfig: ipconfig structure
793 * @ipaddress: ipaddress structure
795 * Bind IP address details to configuration
797 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
798 struct connman_ipaddress *ipaddress)
800 struct connman_ipconfig *origin;
802 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
804 connman_ipaddress_copy(origin->address, ipaddress);
806 connman_inet_set_address(origin->index, origin->address);
809 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
811 struct connman_ipdevice *ipdevice;
813 DBG("ipconfig %p", ipconfig);
815 if (ipconfig == NULL || ipconfig->index < 0)
818 ipdevice = g_hash_table_lookup(ipdevice_hash,
819 GINT_TO_POINTER(ipconfig->index));
820 if (ipdevice == NULL)
823 if (ipdevice->config != NULL)
824 connman_ipconfig_unref(ipdevice->config);
826 ipdevice->config = connman_ipconfig_ref(ipconfig);
831 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
833 struct connman_ipdevice *ipdevice;
835 DBG("ipconfig %p", ipconfig);
837 if (ipconfig == NULL || ipconfig->index < 0)
840 ipdevice = g_hash_table_lookup(ipdevice_hash,
841 GINT_TO_POINTER(ipconfig->index));
842 if (ipdevice == NULL)
845 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
848 connman_ipconfig_unref(ipdevice->config);
849 ipdevice->config = NULL;
854 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
857 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
859 case CONNMAN_IPCONFIG_METHOD_IGNORE:
861 case CONNMAN_IPCONFIG_METHOD_STATIC:
863 case CONNMAN_IPCONFIG_METHOD_DHCP:
870 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
872 if (g_strcmp0(method, "ignore") == 0)
873 return CONNMAN_IPCONFIG_METHOD_IGNORE;
874 else if (g_strcmp0(method, "static") == 0)
875 return CONNMAN_IPCONFIG_METHOD_STATIC;
876 else if (g_strcmp0(method, "dhcp") == 0)
877 return CONNMAN_IPCONFIG_METHOD_DHCP;
879 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
882 static void append_basic(DBusMessageIter *iter, const char *prefix,
883 const char *key, int type, void *val)
887 if (prefix == NULL) {
888 connman_dbus_dict_append_basic(iter, key, type, val);
892 str = g_strdup_printf("%s%s", prefix, key);
894 connman_dbus_dict_append_basic(iter, str, type, val);
899 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
900 DBusMessageIter *iter, const char *prefix)
904 str = __connman_ipconfig_method2string(ipconfig->method);
908 append_basic(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
911 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
912 const char *key, DBusMessageIter *value)
914 int type = dbus_message_iter_get_arg_type(value);
916 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
918 if (g_strcmp0(key, "Method") == 0) {
921 if (type != DBUS_TYPE_STRING)
924 dbus_message_iter_get_basic(value, &method);
926 ipconfig->method = __connman_ipconfig_string2method(method);
933 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
934 GKeyFile *keyfile, const char *identifier, const char *prefix)
936 DBG("ipconfig %p identifier %s", ipconfig, identifier);
941 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
942 GKeyFile *keyfile, const char *identifier, const char *prefix)
944 DBG("ipconfig %p identifier %s", ipconfig, identifier);
949 int __connman_ipconfig_init(void)
953 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
954 NULL, free_ipdevice);
959 void __connman_ipconfig_cleanup(void)
963 g_hash_table_destroy(ipdevice_hash);
964 ipdevice_hash = NULL;