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 if (type == ARPHRD_LOOPBACK)
301 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
302 if (ipdevice != NULL)
305 ipdevice = g_try_new0(struct connman_ipdevice, 1);
306 if (ipdevice == NULL)
309 ipdevice->index = index;
310 ipdevice->ifname = connman_inet_ifname(index);
311 ipdevice->type = type;
313 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
315 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
316 index, type, type2str(type));
319 if (flags == ipdevice->flags)
322 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
329 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
330 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
331 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
332 (IFF_RUNNING | IFF_LOWER_UP))
334 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
338 connman_inet_clear_address(index);
340 ipdevice->flags = flags;
342 str = g_string_new(NULL);
347 g_string_append(str, "UP");
349 g_string_append(str, "DOWN");
351 if (flags & IFF_RUNNING)
352 g_string_append(str, ",RUNNING");
354 if (flags & IFF_LOWER_UP)
355 g_string_append(str, ",LOWER_UP");
357 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
360 g_string_free(str, TRUE);
362 for (list = g_list_first(ipconfig_list); list;
363 list = g_list_next(list)) {
364 struct connman_ipconfig *ipconfig = list->data;
366 if (index != ipconfig->index)
369 if (up == TRUE && ipconfig->ops->up)
370 ipconfig->ops->up(ipconfig);
371 if (lower_up == TRUE && ipconfig->ops->lower_up)
372 ipconfig->ops->lower_up(ipconfig);
374 if (lower_down == TRUE && ipconfig->ops->lower_down)
375 ipconfig->ops->lower_down(ipconfig);
376 if (down == TRUE && ipconfig->ops->down)
377 ipconfig->ops->down(ipconfig);
381 __connman_ipconfig_lower_up(ipdevice);
383 __connman_ipconfig_lower_down(ipdevice);
386 void __connman_ipconfig_dellink(int index)
388 struct connman_ipdevice *ipdevice;
391 DBG("index %d", index);
393 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
394 if (ipdevice == NULL)
397 for (list = g_list_first(ipconfig_list); list;
398 list = g_list_next(list)) {
399 struct connman_ipconfig *ipconfig = list->data;
401 if (index != ipconfig->index)
404 ipconfig->index = -1;
406 if (ipconfig->ops->lower_down)
407 ipconfig->ops->lower_down(ipconfig);
408 if (ipconfig->ops->down)
409 ipconfig->ops->down(ipconfig);
412 __connman_ipconfig_lower_down(ipdevice);
414 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
417 void __connman_ipconfig_newaddr(int index, const char *label,
418 unsigned char prefixlen, const char *address)
420 struct connman_ipdevice *ipdevice;
421 struct connman_ipaddress *ipaddress;
424 DBG("index %d", index);
426 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
427 if (ipdevice == NULL)
430 ipaddress = connman_ipaddress_alloc();
431 if (ipaddress == NULL)
434 ipaddress->prefixlen = prefixlen;
435 ipaddress->local = g_strdup(address);
437 ipdevice->address_list = g_slist_append(ipdevice->address_list,
440 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
441 address, prefixlen, label);
443 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
446 if (g_slist_length(ipdevice->address_list) > 1)
449 for (list = g_list_first(ipconfig_list); list;
450 list = g_list_next(list)) {
451 struct connman_ipconfig *ipconfig = list->data;
453 if (index != ipconfig->index)
456 if (ipconfig->ops->ip_bound)
457 ipconfig->ops->ip_bound(ipconfig);
461 void __connman_ipconfig_deladdr(int index, const char *label,
462 unsigned char prefixlen, const char *address)
464 struct connman_ipdevice *ipdevice;
465 struct connman_ipaddress *ipaddress;
468 DBG("index %d", index);
470 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
471 if (ipdevice == NULL)
474 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
475 if (ipaddress == NULL)
478 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
481 connman_ipaddress_free(ipaddress);
483 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
484 address, prefixlen, label);
486 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
489 if (g_slist_length(ipdevice->address_list) > 0)
492 for (list = g_list_first(ipconfig_list); list;
493 list = g_list_next(list)) {
494 struct connman_ipconfig *ipconfig = list->data;
496 if (index != ipconfig->index)
499 if (ipconfig->ops->ip_release)
500 ipconfig->ops->ip_release(ipconfig);
504 void __connman_ipconfig_newroute(int index, unsigned char scope,
505 const char *dst, const char *gateway)
507 struct connman_ipdevice *ipdevice;
509 DBG("index %d", index);
511 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
512 if (ipdevice == NULL)
515 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
516 g_free(ipdevice->gateway);
517 ipdevice->gateway = g_strdup(gateway);
520 connman_info("%s {add} route %s gw %s scope %u <%s>",
521 ipdevice->ifname, dst, gateway,
522 scope, scope2str(scope));
525 void __connman_ipconfig_delroute(int index, unsigned char scope,
526 const char *dst, const char *gateway)
528 struct connman_ipdevice *ipdevice;
530 DBG("index %d", index);
532 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
533 if (ipdevice == NULL)
536 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
537 g_free(ipdevice->gateway);
538 ipdevice->gateway = NULL;
541 connman_info("%s {del} route %s gw %s scope %u <%s>",
542 ipdevice->ifname, dst, gateway,
543 scope, scope2str(scope));
546 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
551 keys = g_hash_table_get_keys(ipdevice_hash);
555 for (list = g_list_first(keys); list; list = g_list_next(list)) {
556 int index = GPOINTER_TO_INT(list->data);
558 function(index, user_data);
564 unsigned short __connman_ipconfig_get_type(int index)
566 struct connman_ipdevice *ipdevice;
568 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
569 if (ipdevice == NULL)
572 return ipdevice->type;
575 unsigned int __connman_ipconfig_get_flags(int index)
577 struct connman_ipdevice *ipdevice;
579 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
580 if (ipdevice == NULL)
583 return ipdevice->flags;
586 const char *__connman_ipconfig_get_gateway(int index)
588 struct connman_ipdevice *ipdevice;
590 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
591 if (ipdevice == NULL)
594 return ipdevice->gateway;
598 * connman_ipconfig_create:
600 * Allocate a new ipconfig structure.
602 * Returns: a newly-allocated #connman_ipconfig structure
604 struct connman_ipconfig *connman_ipconfig_create(int index)
606 struct connman_ipconfig *ipconfig;
608 DBG("index %d", index);
610 ipconfig = g_try_new0(struct connman_ipconfig, 1);
611 if (ipconfig == NULL)
614 ipconfig->refcount = 1;
616 ipconfig->index = index;
618 ipconfig->address = connman_ipaddress_alloc();
619 if (ipconfig->address == NULL) {
624 DBG("ipconfig %p", ipconfig);
630 * connman_ipconfig_clone:
632 * Clone an ipconfig structure and create new reference.
634 * Returns: a newly-allocated #connman_ipconfig structure
636 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
638 struct connman_ipconfig *ipconfig_clone;
640 DBG("ipconfig %p", ipconfig);
642 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
643 if (ipconfig_clone == NULL)
646 ipconfig_clone->refcount = 1;
648 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
650 ipconfig_clone->index = -1;
652 return ipconfig_clone;
656 * connman_ipconfig_ref:
657 * @ipconfig: ipconfig structure
659 * Increase reference counter of ipconfig
661 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
663 g_atomic_int_inc(&ipconfig->refcount);
669 * connman_ipconfig_unref:
670 * @ipconfig: ipconfig structure
672 * Decrease reference counter of ipconfig
674 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
676 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
677 connman_ipconfig_set_ops(ipconfig, NULL);
679 if (ipconfig->origin != NULL) {
680 connman_ipconfig_unref(ipconfig->origin);
681 ipconfig->origin = NULL;
684 connman_ipaddress_free(ipconfig->address);
690 * connman_ipconfig_get_data:
691 * @ipconfig: ipconfig structure
693 * Get private data pointer
695 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
697 return ipconfig->ops_data;
701 * connman_ipconfig_set_data:
702 * @ipconfig: ipconfig structure
703 * @data: data pointer
705 * Set private data pointer
707 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
709 ipconfig->ops_data = data;
713 * connman_ipconfig_get_index:
714 * @ipconfig: ipconfig structure
716 * Get interface index
718 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
720 if (ipconfig->origin != NULL)
721 return ipconfig->origin->index;
723 return ipconfig->index;
727 * connman_ipconfig_get_ifname:
728 * @ipconfig: ipconfig structure
732 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
734 struct connman_ipdevice *ipdevice;
736 if (ipconfig->index < 0)
739 ipdevice = g_hash_table_lookup(ipdevice_hash,
740 GINT_TO_POINTER(ipconfig->index));
741 if (ipdevice == NULL)
744 return ipdevice->ifname;
748 * connman_ipconfig_set_ops:
749 * @ipconfig: ipconfig structure
750 * @ops: operation callbacks
752 * Set the operation callbacks
754 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
755 const struct connman_ipconfig_ops *ops)
757 if (ipconfig->ops != NULL)
758 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
763 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
767 * connman_ipconfig_set_method:
768 * @ipconfig: ipconfig structure
769 * @method: configuration method
771 * Set the configuration method
773 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
774 enum connman_ipconfig_method method)
776 ipconfig->method = method;
782 * connman_ipconfig_bind:
783 * @ipconfig: ipconfig structure
784 * @ipaddress: ipaddress structure
786 * Bind IP address details to configuration
788 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
789 struct connman_ipaddress *ipaddress)
791 struct connman_ipconfig *origin;
793 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
795 connman_ipaddress_copy(origin->address, ipaddress);
797 connman_inet_set_address(origin->index, origin->address);
800 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
802 struct connman_ipdevice *ipdevice;
804 DBG("ipconfig %p", ipconfig);
806 if (ipconfig == NULL || ipconfig->index < 0)
809 ipdevice = g_hash_table_lookup(ipdevice_hash,
810 GINT_TO_POINTER(ipconfig->index));
811 if (ipdevice == NULL)
814 if (ipdevice->config != NULL)
815 connman_ipconfig_unref(ipdevice->config);
817 ipdevice->config = connman_ipconfig_ref(ipconfig);
822 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
824 struct connman_ipdevice *ipdevice;
826 DBG("ipconfig %p", ipconfig);
828 if (ipconfig == NULL || ipconfig->index < 0)
831 ipdevice = g_hash_table_lookup(ipdevice_hash,
832 GINT_TO_POINTER(ipconfig->index));
833 if (ipdevice == NULL)
836 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
839 connman_ipconfig_unref(ipdevice->config);
840 ipdevice->config = NULL;
845 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
848 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
850 case CONNMAN_IPCONFIG_METHOD_IGNORE:
852 case CONNMAN_IPCONFIG_METHOD_STATIC:
854 case CONNMAN_IPCONFIG_METHOD_DHCP:
861 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
863 if (g_strcmp0(method, "ignore") == 0)
864 return CONNMAN_IPCONFIG_METHOD_IGNORE;
865 else if (g_strcmp0(method, "static") == 0)
866 return CONNMAN_IPCONFIG_METHOD_STATIC;
867 else if (g_strcmp0(method, "dhcp") == 0)
868 return CONNMAN_IPCONFIG_METHOD_DHCP;
870 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
873 static void append_variant(DBusMessageIter *iter, const char *prefix,
874 const char *key, int type, void *val)
878 if (prefix == NULL) {
879 connman_dbus_dict_append_variant(iter, key, type, val);
883 str = g_strdup_printf("%s%s", prefix, key);
885 connman_dbus_dict_append_variant(iter, str, type, val);
890 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
891 DBusMessageIter *iter, const char *prefix)
895 str = __connman_ipconfig_method2string(ipconfig->method);
899 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
902 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
903 const char *key, DBusMessageIter *value)
905 int type = dbus_message_iter_get_arg_type(value);
907 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
909 if (g_strcmp0(key, "Method") == 0) {
912 if (type != DBUS_TYPE_STRING)
915 dbus_message_iter_get_basic(value, &method);
917 ipconfig->method = __connman_ipconfig_string2method(method);
924 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
925 GKeyFile *keyfile, const char *identifier, const char *prefix)
927 DBG("ipconfig %p identifier %s", ipconfig, identifier);
932 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
933 GKeyFile *keyfile, const char *identifier, const char *prefix)
935 DBG("ipconfig %p identifier %s", ipconfig, identifier);
940 int __connman_ipconfig_init(void)
944 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
945 NULL, free_ipdevice);
950 void __connman_ipconfig_cleanup(void)
954 g_hash_table_destroy(ipdevice_hash);
955 ipdevice_hash = NULL;