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;
53 struct connman_ipdevice {
62 struct connman_ipconfig *config;
64 struct connman_ipconfig_driver *driver;
65 struct connman_ipconfig *driver_config;
68 static GHashTable *ipdevice_hash = NULL;
69 static GList *ipconfig_list = NULL;
71 struct connman_ipaddress *connman_ipaddress_alloc(void)
73 struct connman_ipaddress *ipaddress;
75 ipaddress = g_try_new0(struct connman_ipaddress, 1);
76 if (ipaddress == NULL)
82 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
84 if (ipaddress == NULL)
87 g_free(ipaddress->broadcast);
88 g_free(ipaddress->peer);
89 g_free(ipaddress->local);
93 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
94 struct connman_ipaddress *source)
96 if (ipaddress == NULL || source == NULL)
99 ipaddress->prefixlen = source->prefixlen;
101 g_free(ipaddress->local);
102 ipaddress->local = g_strdup(source->local);
104 g_free(ipaddress->peer);
105 ipaddress->peer = g_strdup(source->peer);
107 g_free(ipaddress->broadcast);
108 ipaddress->broadcast = g_strdup(source->broadcast);
111 static void free_address_list(struct connman_ipdevice *ipdevice)
115 for (list = ipdevice->address_list; list; list = list->next) {
116 struct connman_ipaddress *ipaddress = list->data;
118 connman_ipaddress_free(ipaddress);
122 g_slist_free(ipdevice->address_list);
123 ipdevice->address_list = NULL;
126 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
127 unsigned char prefixlen, const char *local)
131 for (list = ipdevice->address_list; list; list = list->next) {
132 struct connman_ipaddress *ipaddress = list->data;
134 if (g_strcmp0(ipaddress->local, local) == 0 &&
135 ipaddress->prefixlen == prefixlen)
142 static const char *type2str(unsigned short type)
147 case ARPHRD_LOOPBACK:
160 static const char *scope2str(unsigned char scope)
172 static void free_ipdevice(gpointer data)
174 struct connman_ipdevice *ipdevice = data;
176 connman_info("%s {remove} index %d", ipdevice->ifname,
179 if (ipdevice->config != NULL)
180 connman_ipconfig_unref(ipdevice->config);
182 free_address_list(ipdevice);
183 g_free(ipdevice->gateway);
185 g_free(ipdevice->ifname);
189 static GSList *driver_list = NULL;
191 static gint compare_priority(gconstpointer a, gconstpointer b)
193 const struct connman_ipconfig_driver *driver1 = a;
194 const struct connman_ipconfig_driver *driver2 = b;
196 return driver2->priority - driver1->priority;
200 * connman_ipconfig_driver_register:
201 * @driver: IP configuration driver
203 * Register a new IP configuration driver
205 * Returns: %0 on success
207 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
209 DBG("driver %p name %s", driver, driver->name);
211 driver_list = g_slist_insert_sorted(driver_list, driver,
218 * connman_ipconfig_driver_unregister:
219 * @driver: IP configuration driver
221 * Remove a previously registered IP configuration driver.
223 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
225 DBG("driver %p name %s", driver, driver->name);
227 driver_list = g_slist_remove(driver_list, driver);
230 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
234 DBG("ipconfig %p", ipdevice->config);
236 if (ipdevice->config == NULL)
239 switch (ipdevice->config->method) {
240 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
241 case CONNMAN_IPCONFIG_METHOD_IGNORE:
242 case CONNMAN_IPCONFIG_METHOD_STATIC:
244 case CONNMAN_IPCONFIG_METHOD_DHCP:
248 if (ipdevice->driver != NULL)
251 ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
252 if (ipdevice->driver_config == NULL)
255 for (list = driver_list; list; list = list->next) {
256 struct connman_ipconfig_driver *driver = list->data;
258 if (driver->request(ipdevice->driver_config) == 0) {
259 ipdevice->driver = driver;
264 if (ipdevice->driver == NULL) {
265 connman_ipconfig_unref(ipdevice->driver_config);
266 ipdevice->driver_config = NULL;
270 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
272 DBG("ipconfig %p", ipdevice->config);
274 if (ipdevice->config == NULL)
277 if (ipdevice->driver == NULL)
280 ipdevice->driver->release(ipdevice->driver_config);
282 ipdevice->driver = NULL;
284 connman_ipconfig_unref(ipdevice->driver_config);
285 ipdevice->driver_config = NULL;
287 connman_inet_clear_address(ipdevice->index);
290 void __connman_ipconfig_newlink(int index, unsigned short type,
291 unsigned int flags, const char *address,
294 struct connman_ipdevice *ipdevice;
297 gboolean up = FALSE, down = FALSE;
298 gboolean lower_up = FALSE, lower_down = FALSE;
301 DBG("index %d", index);
303 if (type == ARPHRD_LOOPBACK)
306 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
307 if (ipdevice != NULL)
310 ifname = connman_inet_ifname(index);
312 if (__connman_element_device_isfiltered(ifname) == TRUE) {
313 connman_info("Ignoring interface %s (filtered)", ifname);
318 ipdevice = g_try_new0(struct connman_ipdevice, 1);
319 if (ipdevice == NULL) {
324 ipdevice->index = index;
325 ipdevice->ifname = ifname;
326 ipdevice->type = type;
328 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
330 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
331 index, type, type2str(type));
334 if (flags == ipdevice->flags)
337 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
344 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
345 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
346 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
347 (IFF_RUNNING | IFF_LOWER_UP))
349 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
353 ipdevice->flags = flags;
355 str = g_string_new(NULL);
360 g_string_append(str, "UP");
362 g_string_append(str, "DOWN");
364 if (flags & IFF_RUNNING)
365 g_string_append(str, ",RUNNING");
367 if (flags & IFF_LOWER_UP)
368 g_string_append(str, ",LOWER_UP");
370 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
373 g_string_free(str, TRUE);
375 for (list = g_list_first(ipconfig_list); list;
376 list = g_list_next(list)) {
377 struct connman_ipconfig *ipconfig = list->data;
379 if (index != ipconfig->index)
382 g_free(ipconfig->eth);
383 ipconfig->eth = g_strdup(address);
386 if (up == TRUE && ipconfig->ops->up)
387 ipconfig->ops->up(ipconfig);
388 if (lower_up == TRUE && ipconfig->ops->lower_up)
389 ipconfig->ops->lower_up(ipconfig);
391 if (lower_down == TRUE && ipconfig->ops->lower_down)
392 ipconfig->ops->lower_down(ipconfig);
393 if (down == TRUE && ipconfig->ops->down)
394 ipconfig->ops->down(ipconfig);
398 __connman_ipconfig_lower_up(ipdevice);
400 __connman_ipconfig_lower_down(ipdevice);
403 void __connman_ipconfig_dellink(int index)
405 struct connman_ipdevice *ipdevice;
408 DBG("index %d", index);
410 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
411 if (ipdevice == NULL)
414 for (list = g_list_first(ipconfig_list); list;
415 list = g_list_next(list)) {
416 struct connman_ipconfig *ipconfig = list->data;
418 if (index != ipconfig->index)
421 ipconfig->index = -1;
423 if (ipconfig->ops->lower_down)
424 ipconfig->ops->lower_down(ipconfig);
425 if (ipconfig->ops->down)
426 ipconfig->ops->down(ipconfig);
429 __connman_ipconfig_lower_down(ipdevice);
431 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
434 void __connman_ipconfig_newaddr(int index, const char *label,
435 unsigned char prefixlen, const char *address)
437 struct connman_ipdevice *ipdevice;
438 struct connman_ipaddress *ipaddress;
441 DBG("index %d", index);
443 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
444 if (ipdevice == NULL)
447 ipaddress = connman_ipaddress_alloc();
448 if (ipaddress == NULL)
451 ipaddress->prefixlen = prefixlen;
452 ipaddress->local = g_strdup(address);
454 ipdevice->address_list = g_slist_append(ipdevice->address_list,
457 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
458 address, prefixlen, label);
460 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
463 if (g_slist_length(ipdevice->address_list) > 1)
466 for (list = g_list_first(ipconfig_list); list;
467 list = g_list_next(list)) {
468 struct connman_ipconfig *ipconfig = list->data;
470 if (index != ipconfig->index)
473 if (ipconfig->ops->ip_bound)
474 ipconfig->ops->ip_bound(ipconfig);
478 void __connman_ipconfig_deladdr(int index, const char *label,
479 unsigned char prefixlen, const char *address)
481 struct connman_ipdevice *ipdevice;
482 struct connman_ipaddress *ipaddress;
485 DBG("index %d", index);
487 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
488 if (ipdevice == NULL)
491 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
492 if (ipaddress == NULL)
495 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
498 connman_ipaddress_free(ipaddress);
500 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
501 address, prefixlen, label);
503 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
506 if (g_slist_length(ipdevice->address_list) > 0)
509 for (list = g_list_first(ipconfig_list); list;
510 list = g_list_next(list)) {
511 struct connman_ipconfig *ipconfig = list->data;
513 if (index != ipconfig->index)
516 if (ipconfig->ops->ip_release)
517 ipconfig->ops->ip_release(ipconfig);
521 void __connman_ipconfig_newroute(int index, unsigned char scope,
522 const char *dst, const char *gateway)
524 struct connman_ipdevice *ipdevice;
526 DBG("index %d", index);
528 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
529 if (ipdevice == NULL)
532 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
533 g_free(ipdevice->gateway);
534 ipdevice->gateway = g_strdup(gateway);
537 connman_info("%s {add} route %s gw %s scope %u <%s>",
538 ipdevice->ifname, dst, gateway,
539 scope, scope2str(scope));
542 void __connman_ipconfig_delroute(int index, unsigned char scope,
543 const char *dst, const char *gateway)
545 struct connman_ipdevice *ipdevice;
547 DBG("index %d", index);
549 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
550 if (ipdevice == NULL)
553 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
554 g_free(ipdevice->gateway);
555 ipdevice->gateway = NULL;
558 connman_info("%s {del} route %s gw %s scope %u <%s>",
559 ipdevice->ifname, dst, gateway,
560 scope, scope2str(scope));
563 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
568 keys = g_hash_table_get_keys(ipdevice_hash);
572 for (list = g_list_first(keys); list; list = g_list_next(list)) {
573 int index = GPOINTER_TO_INT(list->data);
575 function(index, user_data);
581 unsigned short __connman_ipconfig_get_type(int index)
583 struct connman_ipdevice *ipdevice;
585 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
586 if (ipdevice == NULL)
589 return ipdevice->type;
592 unsigned int __connman_ipconfig_get_flags(int index)
594 struct connman_ipdevice *ipdevice;
596 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
597 if (ipdevice == NULL)
600 return ipdevice->flags;
603 const char *__connman_ipconfig_get_gateway(int index)
605 struct connman_ipdevice *ipdevice;
607 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
608 if (ipdevice == NULL)
611 return ipdevice->gateway;
615 * connman_ipconfig_create:
617 * Allocate a new ipconfig structure.
619 * Returns: a newly-allocated #connman_ipconfig structure
621 struct connman_ipconfig *connman_ipconfig_create(int index)
623 struct connman_ipconfig *ipconfig;
625 DBG("index %d", index);
627 ipconfig = g_try_new0(struct connman_ipconfig, 1);
628 if (ipconfig == NULL)
631 ipconfig->refcount = 1;
633 ipconfig->index = index;
635 ipconfig->address = connman_ipaddress_alloc();
636 if (ipconfig->address == NULL) {
641 DBG("ipconfig %p", ipconfig);
647 * connman_ipconfig_clone:
649 * Clone an ipconfig structure and create new reference.
651 * Returns: a newly-allocated #connman_ipconfig structure
653 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
655 struct connman_ipconfig *ipconfig_clone;
657 DBG("ipconfig %p", ipconfig);
659 ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
660 if (ipconfig_clone == NULL)
663 ipconfig_clone->refcount = 1;
665 ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
667 ipconfig_clone->index = -1;
669 return ipconfig_clone;
673 * connman_ipconfig_ref:
674 * @ipconfig: ipconfig structure
676 * Increase reference counter of ipconfig
678 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
680 g_atomic_int_inc(&ipconfig->refcount);
686 * connman_ipconfig_unref:
687 * @ipconfig: ipconfig structure
689 * Decrease reference counter of ipconfig
691 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
693 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
694 connman_ipconfig_set_ops(ipconfig, NULL);
696 if (ipconfig->origin != NULL) {
697 connman_ipconfig_unref(ipconfig->origin);
698 ipconfig->origin = NULL;
701 connman_ipaddress_free(ipconfig->address);
702 g_free(ipconfig->eth);
708 * connman_ipconfig_get_data:
709 * @ipconfig: ipconfig structure
711 * Get private data pointer
713 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
715 return ipconfig->ops_data;
719 * connman_ipconfig_set_data:
720 * @ipconfig: ipconfig structure
721 * @data: data pointer
723 * Set private data pointer
725 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
727 ipconfig->ops_data = data;
731 * connman_ipconfig_get_index:
732 * @ipconfig: ipconfig structure
734 * Get interface index
736 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
738 if (ipconfig->origin != NULL)
739 return ipconfig->origin->index;
741 return ipconfig->index;
745 * connman_ipconfig_get_ifname:
746 * @ipconfig: ipconfig structure
750 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
752 struct connman_ipdevice *ipdevice;
754 if (ipconfig->index < 0)
757 ipdevice = g_hash_table_lookup(ipdevice_hash,
758 GINT_TO_POINTER(ipconfig->index));
759 if (ipdevice == NULL)
762 return ipdevice->ifname;
766 * connman_ipconfig_set_ops:
767 * @ipconfig: ipconfig structure
768 * @ops: operation callbacks
770 * Set the operation callbacks
772 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
773 const struct connman_ipconfig_ops *ops)
775 if (ipconfig->ops != NULL)
776 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
781 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
785 * connman_ipconfig_set_method:
786 * @ipconfig: ipconfig structure
787 * @method: configuration method
789 * Set the configuration method
791 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
792 enum connman_ipconfig_method method)
794 ipconfig->method = method;
800 * connman_ipconfig_bind:
801 * @ipconfig: ipconfig structure
802 * @ipaddress: ipaddress structure
804 * Bind IP address details to configuration
806 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
807 struct connman_ipaddress *ipaddress)
809 struct connman_ipconfig *origin;
811 origin = ipconfig->origin ? ipconfig->origin : ipconfig;
813 connman_ipaddress_copy(origin->address, ipaddress);
815 connman_inet_set_address(origin->index, origin->address);
818 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
820 struct connman_ipdevice *ipdevice;
822 DBG("ipconfig %p", ipconfig);
824 if (ipconfig == NULL || ipconfig->index < 0)
827 ipdevice = g_hash_table_lookup(ipdevice_hash,
828 GINT_TO_POINTER(ipconfig->index));
829 if (ipdevice == NULL)
832 if (ipdevice->config != NULL)
833 connman_ipconfig_unref(ipdevice->config);
835 ipdevice->config = connman_ipconfig_ref(ipconfig);
840 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
842 struct connman_ipdevice *ipdevice;
844 DBG("ipconfig %p", ipconfig);
846 if (ipconfig == NULL || ipconfig->index < 0)
849 ipdevice = g_hash_table_lookup(ipdevice_hash,
850 GINT_TO_POINTER(ipconfig->index));
851 if (ipdevice == NULL)
854 if (ipdevice->config == NULL || ipdevice->config != ipconfig)
857 connman_ipconfig_unref(ipdevice->config);
858 ipdevice->config = NULL;
863 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
866 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
868 case CONNMAN_IPCONFIG_METHOD_IGNORE:
870 case CONNMAN_IPCONFIG_METHOD_STATIC:
872 case CONNMAN_IPCONFIG_METHOD_DHCP:
879 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
881 if (g_strcmp0(method, "ignore") == 0)
882 return CONNMAN_IPCONFIG_METHOD_IGNORE;
883 else if (g_strcmp0(method, "static") == 0)
884 return CONNMAN_IPCONFIG_METHOD_STATIC;
885 else if (g_strcmp0(method, "dhcp") == 0)
886 return CONNMAN_IPCONFIG_METHOD_DHCP;
888 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
891 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
892 DBusMessageIter *iter)
896 str = __connman_ipconfig_method2string(ipconfig->method);
900 connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
902 if (ipconfig->address == NULL)
905 if (ipconfig->address->local != NULL) {
906 struct in_addr netmask;
909 connman_dbus_dict_append_basic(iter, "Address",
910 DBUS_TYPE_STRING, &ipconfig->address->local);
912 netmask.s_addr = ~0 << (32 - ipconfig->address->prefixlen);
913 mask = inet_ntoa(netmask);
914 connman_dbus_dict_append_basic(iter, "Netmask",
915 DBUS_TYPE_STRING, &mask);
919 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
920 const char *key, DBusMessageIter *value)
922 int type = dbus_message_iter_get_arg_type(value);
924 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
926 if (g_strcmp0(key, "Method") == 0) {
929 if (type != DBUS_TYPE_STRING)
932 dbus_message_iter_get_basic(value, &method);
934 ipconfig->method = __connman_ipconfig_string2method(method);
941 int __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
942 DBusMessageIter *iter)
944 const char *method = "auto";
946 connman_dbus_dict_append_basic(iter, "Method",
947 DBUS_TYPE_STRING, &method);
949 if (ipconfig->eth != NULL)
950 connman_dbus_dict_append_basic(iter, "Address",
951 DBUS_TYPE_STRING, &ipconfig->eth);
953 if (ipconfig->mtu > 0)
954 connman_dbus_dict_append_basic(iter, "MTU",
955 DBUS_TYPE_UINT16, &ipconfig->mtu);
960 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
961 GKeyFile *keyfile, const char *identifier, const char *prefix)
963 DBG("ipconfig %p identifier %s", ipconfig, identifier);
968 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
969 GKeyFile *keyfile, const char *identifier, const char *prefix)
971 DBG("ipconfig %p identifier %s", ipconfig, identifier);
976 int __connman_ipconfig_init(void)
980 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
981 NULL, free_ipdevice);
986 void __connman_ipconfig_cleanup(void)
990 g_hash_table_destroy(ipdevice_hash);
991 ipdevice_hash = NULL;