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 const struct connman_ipconfig_ops *ops;
44 enum connman_ipconfig_method method;
45 struct connman_ipaddress *address;
48 struct connman_ipdevice {
57 struct connman_ipconfig *config;
58 struct connman_ipconfig_driver *driver;
61 static GHashTable *ipdevice_hash = NULL;
62 static GList *ipconfig_list = NULL;
64 struct connman_ipaddress *connman_ipaddress_alloc(void)
66 struct connman_ipaddress *ipaddress;
68 ipaddress = g_try_new0(struct connman_ipaddress, 1);
69 if (ipaddress == NULL)
75 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
77 g_free(ipaddress->broadcast);
78 g_free(ipaddress->peer);
79 g_free(ipaddress->local);
83 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
84 struct connman_ipaddress *source)
86 ipaddress->prefixlen = source->prefixlen;
88 g_free(ipaddress->local);
89 ipaddress->local = g_strdup(source->local);
91 g_free(ipaddress->peer);
92 ipaddress->peer = g_strdup(source->peer);
94 g_free(ipaddress->broadcast);
95 ipaddress->broadcast = g_strdup(source->broadcast);
98 static void free_address_list(struct connman_ipdevice *ipdevice)
102 for (list = ipdevice->address_list; list; list = list->next) {
103 struct connman_ipaddress *ipaddress = list->data;
105 connman_ipaddress_free(ipaddress);
109 g_slist_free(ipdevice->address_list);
110 ipdevice->address_list = NULL;
113 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
114 unsigned char prefixlen, const char *local)
118 for (list = ipdevice->address_list; list; list = list->next) {
119 struct connman_ipaddress *ipaddress = list->data;
121 if (g_strcmp0(ipaddress->local, local) == 0 &&
122 ipaddress->prefixlen == prefixlen)
129 static const char *type2str(unsigned short type)
134 case ARPHRD_LOOPBACK:
147 static const char *scope2str(unsigned char scope)
159 static void free_ipdevice(gpointer data)
161 struct connman_ipdevice *ipdevice = data;
163 connman_info("%s {remove} index %d", ipdevice->ifname,
166 if (ipdevice->config != NULL)
167 connman_ipconfig_unref(ipdevice->config);
169 free_address_list(ipdevice);
170 g_free(ipdevice->gateway);
172 g_free(ipdevice->ifname);
176 static GSList *driver_list = NULL;
178 static gint compare_priority(gconstpointer a, gconstpointer b)
180 const struct connman_ipconfig_driver *driver1 = a;
181 const struct connman_ipconfig_driver *driver2 = b;
183 return driver2->priority - driver1->priority;
187 * connman_ipconfig_driver_register:
188 * @driver: IP configuration driver
190 * Register a new IP configuration driver
192 * Returns: %0 on success
194 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
196 DBG("driver %p name %s", driver, driver->name);
198 driver_list = g_slist_insert_sorted(driver_list, driver,
205 * connman_ipconfig_driver_unregister:
206 * @driver: IP configuration driver
208 * Remove a previously registered IP configuration driver.
210 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
212 DBG("driver %p name %s", driver, driver->name);
214 driver_list = g_slist_remove(driver_list, driver);
217 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
221 DBG("ipconfig %p", ipdevice->config);
223 if (ipdevice->config == NULL)
226 switch (ipdevice->config->method) {
227 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
228 case CONNMAN_IPCONFIG_METHOD_IGNORE:
229 case CONNMAN_IPCONFIG_METHOD_STATIC:
231 case CONNMAN_IPCONFIG_METHOD_DHCP:
235 if (ipdevice->driver != NULL)
238 for (list = driver_list; list; list = list->next) {
239 struct connman_ipconfig_driver *driver = list->data;
241 if (driver->request(ipdevice->config) == 0) {
242 ipdevice->driver = driver;
248 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
250 DBG("ipconfig %p", ipdevice->config);
252 if (ipdevice->config == NULL)
255 if (ipdevice->driver == NULL)
258 ipdevice->driver->release(ipdevice->config);
260 connman_inet_clear_address(ipdevice->index);
263 void __connman_ipconfig_newlink(int index, unsigned short type,
266 struct connman_ipdevice *ipdevice;
269 gboolean up = FALSE, down = FALSE;
270 gboolean lower_up = FALSE, lower_down = FALSE;
272 DBG("index %d", index);
274 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
275 if (ipdevice != NULL)
278 ipdevice = g_try_new0(struct connman_ipdevice, 1);
279 if (ipdevice == NULL)
282 ipdevice->index = index;
283 ipdevice->ifname = connman_inet_ifname(index);
284 ipdevice->type = type;
286 ipdevice->config = connman_ipconfig_create(index);
287 if (ipdevice->config == NULL) {
288 g_free(ipdevice->ifname);
293 if (type == ARPHRD_ETHER)
294 connman_ipconfig_set_method(ipdevice->config,
295 CONNMAN_IPCONFIG_METHOD_IGNORE);
297 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
299 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
300 index, type, type2str(type));
303 if (flags == ipdevice->flags)
306 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
313 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
314 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
315 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
316 (IFF_RUNNING | IFF_LOWER_UP))
318 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
322 connman_inet_clear_address(index);
324 ipdevice->flags = flags;
326 str = g_string_new(NULL);
331 g_string_append(str, "UP");
333 g_string_append(str, "DOWN");
335 if (flags & IFF_RUNNING)
336 g_string_append(str, ",RUNNING");
338 if (flags & IFF_LOWER_UP)
339 g_string_append(str, ",LOWER_UP");
341 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
344 g_string_free(str, TRUE);
346 for (list = g_list_first(ipconfig_list); list;
347 list = g_list_next(list)) {
348 struct connman_ipconfig *ipconfig = list->data;
350 if (index != ipconfig->index)
353 if (up == TRUE && ipconfig->ops->up)
354 ipconfig->ops->up(ipconfig);
355 if (lower_up == TRUE && ipconfig->ops->lower_up)
356 ipconfig->ops->lower_up(ipconfig);
358 if (lower_down == TRUE && ipconfig->ops->lower_down)
359 ipconfig->ops->lower_down(ipconfig);
360 if (down == TRUE && ipconfig->ops->down)
361 ipconfig->ops->down(ipconfig);
365 __connman_ipconfig_lower_up(ipdevice);
367 __connman_ipconfig_lower_down(ipdevice);
370 void __connman_ipconfig_dellink(int index)
372 struct connman_ipdevice *ipdevice;
375 DBG("index %d", index);
377 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
378 if (ipdevice == NULL)
381 for (list = g_list_first(ipconfig_list); list;
382 list = g_list_next(list)) {
383 struct connman_ipconfig *ipconfig = list->data;
385 if (index != ipconfig->index)
388 ipconfig->index = -1;
390 if (ipconfig->ops->lower_down)
391 ipconfig->ops->lower_down(ipconfig);
392 if (ipconfig->ops->down)
393 ipconfig->ops->down(ipconfig);
396 __connman_ipconfig_lower_down(ipdevice);
398 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
401 void __connman_ipconfig_newaddr(int index, const char *label,
402 unsigned char prefixlen, const char *address)
404 struct connman_ipdevice *ipdevice;
405 struct connman_ipaddress *ipaddress;
408 DBG("index %d", index);
410 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
411 if (ipdevice == NULL)
414 ipaddress = connman_ipaddress_alloc();
415 if (ipaddress == NULL)
418 ipaddress->prefixlen = prefixlen;
419 ipaddress->local = g_strdup(address);
421 ipdevice->address_list = g_slist_append(ipdevice->address_list,
424 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
425 address, prefixlen, label);
427 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
430 if (g_slist_length(ipdevice->address_list) > 1)
433 for (list = g_list_first(ipconfig_list); list;
434 list = g_list_next(list)) {
435 struct connman_ipconfig *ipconfig = list->data;
437 if (index != ipconfig->index)
440 if (ipconfig->ops->ip_bound)
441 ipconfig->ops->ip_bound(ipconfig);
445 void __connman_ipconfig_deladdr(int index, const char *label,
446 unsigned char prefixlen, const char *address)
448 struct connman_ipdevice *ipdevice;
449 struct connman_ipaddress *ipaddress;
452 DBG("index %d", index);
454 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
455 if (ipdevice == NULL)
458 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
459 if (ipaddress == NULL)
462 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
465 connman_ipaddress_free(ipaddress);
467 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
468 address, prefixlen, label);
470 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
473 if (g_slist_length(ipdevice->address_list) > 0)
476 for (list = g_list_first(ipconfig_list); list;
477 list = g_list_next(list)) {
478 struct connman_ipconfig *ipconfig = list->data;
480 if (index != ipconfig->index)
483 if (ipconfig->ops->ip_release)
484 ipconfig->ops->ip_release(ipconfig);
488 void __connman_ipconfig_newroute(int index, unsigned char scope,
489 const char *dst, const char *gateway)
491 struct connman_ipdevice *ipdevice;
493 DBG("index %d", index);
495 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
496 if (ipdevice == NULL)
499 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
500 g_free(ipdevice->gateway);
501 ipdevice->gateway = g_strdup(gateway);
504 connman_info("%s {add} route %s gw %s scope %u <%s>",
505 ipdevice->ifname, dst, gateway,
506 scope, scope2str(scope));
509 void __connman_ipconfig_delroute(int index, unsigned char scope,
510 const char *dst, const char *gateway)
512 struct connman_ipdevice *ipdevice;
514 DBG("index %d", index);
516 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
517 if (ipdevice == NULL)
520 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
521 g_free(ipdevice->gateway);
522 ipdevice->gateway = NULL;
525 connman_info("%s {del} route %s gw %s scope %u <%s>",
526 ipdevice->ifname, dst, gateway,
527 scope, scope2str(scope));
530 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
535 keys = g_hash_table_get_keys(ipdevice_hash);
539 for (list = g_list_first(keys); list; list = g_list_next(list)) {
540 int index = GPOINTER_TO_INT(list->data);
542 function(index, user_data);
548 unsigned short __connman_ipconfig_get_type(int index)
550 struct connman_ipdevice *ipdevice;
552 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
553 if (ipdevice == NULL)
556 return ipdevice->type;
559 unsigned int __connman_ipconfig_get_flags(int index)
561 struct connman_ipdevice *ipdevice;
563 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
564 if (ipdevice == NULL)
567 return ipdevice->flags;
570 const char *__connman_ipconfig_get_gateway(int index)
572 struct connman_ipdevice *ipdevice;
574 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
575 if (ipdevice == NULL)
578 return ipdevice->gateway;
582 * connman_ipconfig_create:
584 * Allocate a new ipconfig structure.
586 * Returns: a newly-allocated #connman_ipconfig structure
588 struct connman_ipconfig *connman_ipconfig_create(int index)
590 struct connman_ipconfig *ipconfig;
592 DBG("index %d", index);
594 ipconfig = g_try_new0(struct connman_ipconfig, 1);
595 if (ipconfig == NULL)
598 ipconfig->refcount = 1;
600 ipconfig->index = index;
602 ipconfig->address = connman_ipaddress_alloc();
603 if (ipconfig->address == NULL) {
608 DBG("ipconfig %p", ipconfig);
614 * connman_ipconfig_ref:
615 * @ipconfig: ipconfig structure
617 * Increase reference counter of ipconfig
619 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
621 g_atomic_int_inc(&ipconfig->refcount);
627 * connman_ipconfig_unref:
628 * @ipconfig: ipconfig structure
630 * Decrease reference counter of ipconfig
632 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
634 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
635 connman_ipconfig_set_ops(ipconfig, NULL);
637 connman_ipaddress_free(ipconfig->address);
643 * connman_ipconfig_get_data:
644 * @ipconfig: ipconfig structure
646 * Get private data pointer
648 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
650 return ipconfig->ops_data;
654 * connman_ipconfig_set_data:
655 * @ipconfig: ipconfig structure
656 * @data: data pointer
658 * Set private data pointer
660 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
662 ipconfig->ops_data = data;
666 * connman_ipconfig_get_index:
667 * @ipconfig: ipconfig structure
669 * Get interface index
671 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
673 return ipconfig->index;
677 * connman_ipconfig_get_ifname:
678 * @ipconfig: ipconfig structure
682 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
684 struct connman_ipdevice *ipdevice;
686 if (ipconfig->index < 0)
689 ipdevice = g_hash_table_lookup(ipdevice_hash,
690 GINT_TO_POINTER(ipconfig->index));
691 if (ipdevice == NULL)
694 return ipdevice->ifname;
698 * connman_ipconfig_set_ops:
699 * @ipconfig: ipconfig structure
700 * @ops: operation callbacks
702 * Set the operation callbacks
704 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
705 const struct connman_ipconfig_ops *ops)
707 if (ipconfig->ops != NULL)
708 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
713 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
717 * connman_ipconfig_set_method:
718 * @ipconfig: ipconfig structure
719 * @method: configuration method
721 * Set the configuration method
723 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
724 enum connman_ipconfig_method method)
726 ipconfig->method = method;
732 * connman_ipconfig_bind:
733 * @ipconfig: ipconfig structure
734 * @ipaddress: ipaddress structure
736 * Bind IP address details to configuration
738 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
739 struct connman_ipaddress *ipaddress)
741 connman_ipaddress_copy(ipconfig->address, ipaddress);
743 connman_inet_set_address(ipconfig->index, ipconfig->address);
746 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
749 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
751 case CONNMAN_IPCONFIG_METHOD_IGNORE:
753 case CONNMAN_IPCONFIG_METHOD_STATIC:
755 case CONNMAN_IPCONFIG_METHOD_DHCP:
762 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
764 if (g_strcmp0(method, "ignore") == 0)
765 return CONNMAN_IPCONFIG_METHOD_IGNORE;
766 else if (g_strcmp0(method, "static") == 0)
767 return CONNMAN_IPCONFIG_METHOD_STATIC;
768 else if (g_strcmp0(method, "dhcp") == 0)
769 return CONNMAN_IPCONFIG_METHOD_DHCP;
771 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
774 static void append_variant(DBusMessageIter *iter, const char *prefix,
775 const char *key, int type, void *val)
779 if (prefix == NULL) {
780 connman_dbus_dict_append_variant(iter, key, type, val);
784 str = g_strdup_printf("%s%s", prefix, key);
786 connman_dbus_dict_append_variant(iter, str, type, val);
791 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
792 DBusMessageIter *iter, const char *prefix)
796 str = __connman_ipconfig_method2string(ipconfig->method);
800 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
803 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
804 const char *key, DBusMessageIter *value)
806 int type = dbus_message_iter_get_arg_type(value);
808 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
810 if (g_strcmp0(key, "Method") == 0) {
813 if (type != DBUS_TYPE_STRING)
816 dbus_message_iter_get_basic(value, &method);
818 ipconfig->method = __connman_ipconfig_string2method(method);
825 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
826 GKeyFile *keyfile, const char *identifier, const char *prefix)
828 DBG("ipconfig %p identifier %s", ipconfig, identifier);
833 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
834 GKeyFile *keyfile, const char *identifier, const char *prefix)
836 DBG("ipconfig %p identifier %s", ipconfig, identifier);
841 int __connman_ipconfig_init(void)
845 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
846 NULL, free_ipdevice);
851 void __connman_ipconfig_cleanup(void)
855 g_hash_table_destroy(ipdevice_hash);
856 ipdevice_hash = NULL;