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)
354 if (up == TRUE && ipconfig->ops->up)
355 ipconfig->ops->up(ipconfig);
356 if (lower_up == TRUE && ipconfig->ops->lower_up)
357 ipconfig->ops->lower_up(ipconfig);
359 if (lower_down == TRUE && ipconfig->ops->lower_down)
360 ipconfig->ops->lower_down(ipconfig);
361 if (down == TRUE && ipconfig->ops->down)
362 ipconfig->ops->down(ipconfig);
367 __connman_ipconfig_lower_up(ipdevice);
369 __connman_ipconfig_lower_down(ipdevice);
372 void __connman_ipconfig_dellink(int index)
374 struct connman_ipdevice *ipdevice;
377 DBG("index %d", index);
379 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
380 if (ipdevice == NULL)
383 for (list = g_list_first(ipconfig_list); list;
384 list = g_list_next(list)) {
385 struct connman_ipconfig *ipconfig = list->data;
387 if (index != ipconfig->index)
390 ipconfig->index = -1;
392 if (ipconfig->ops && ipconfig->ops->lower_down)
393 ipconfig->ops->lower_down(ipconfig);
394 if (ipconfig->ops && ipconfig->ops->down)
395 ipconfig->ops->down(ipconfig);
398 __connman_ipconfig_lower_down(ipdevice);
400 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
403 void __connman_ipconfig_newaddr(int index, const char *label,
404 unsigned char prefixlen, const char *address)
406 struct connman_ipdevice *ipdevice;
407 struct connman_ipaddress *ipaddress;
410 DBG("index %d", index);
412 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
413 if (ipdevice == NULL)
416 ipaddress = connman_ipaddress_alloc();
417 if (ipaddress == NULL)
420 ipaddress->prefixlen = prefixlen;
421 ipaddress->local = g_strdup(address);
423 ipdevice->address_list = g_slist_append(ipdevice->address_list,
426 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
427 address, prefixlen, label);
429 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
432 if (g_slist_length(ipdevice->address_list) > 1)
435 for (list = g_list_first(ipconfig_list); list;
436 list = g_list_next(list)) {
437 struct connman_ipconfig *ipconfig = list->data;
439 if (index != ipconfig->index)
442 if (ipconfig->ops && ipconfig->ops->ip_bound)
443 ipconfig->ops->ip_bound(ipconfig);
447 void __connman_ipconfig_deladdr(int index, const char *label,
448 unsigned char prefixlen, const char *address)
450 struct connman_ipdevice *ipdevice;
451 struct connman_ipaddress *ipaddress;
454 DBG("index %d", index);
456 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
457 if (ipdevice == NULL)
460 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
461 if (ipaddress == NULL)
464 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
467 connman_ipaddress_free(ipaddress);
469 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
470 address, prefixlen, label);
472 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
475 if (g_slist_length(ipdevice->address_list) > 0)
478 for (list = g_list_first(ipconfig_list); list;
479 list = g_list_next(list)) {
480 struct connman_ipconfig *ipconfig = list->data;
482 if (index != ipconfig->index)
485 if (ipconfig->ops && ipconfig->ops->ip_release)
486 ipconfig->ops->ip_release(ipconfig);
490 void __connman_ipconfig_newroute(int index, unsigned char scope,
491 const char *dst, const char *gateway)
493 struct connman_ipdevice *ipdevice;
495 DBG("index %d", index);
497 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
498 if (ipdevice == NULL)
501 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
502 g_free(ipdevice->gateway);
503 ipdevice->gateway = g_strdup(gateway);
506 connman_info("%s {add} route %s gw %s scope %u <%s>",
507 ipdevice->ifname, dst, gateway,
508 scope, scope2str(scope));
511 void __connman_ipconfig_delroute(int index, unsigned char scope,
512 const char *dst, const char *gateway)
514 struct connman_ipdevice *ipdevice;
516 DBG("index %d", index);
518 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
519 if (ipdevice == NULL)
522 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
523 g_free(ipdevice->gateway);
524 ipdevice->gateway = NULL;
527 connman_info("%s {del} route %s gw %s scope %u <%s>",
528 ipdevice->ifname, dst, gateway,
529 scope, scope2str(scope));
532 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
537 keys = g_hash_table_get_keys(ipdevice_hash);
541 for (list = g_list_first(keys); list; list = g_list_next(list)) {
542 int index = GPOINTER_TO_INT(list->data);
544 function(index, user_data);
550 unsigned short __connman_ipconfig_get_type(int index)
552 struct connman_ipdevice *ipdevice;
554 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
555 if (ipdevice == NULL)
558 return ipdevice->type;
561 unsigned int __connman_ipconfig_get_flags(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->flags;
572 const char *__connman_ipconfig_get_gateway(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->gateway;
584 * connman_ipconfig_create:
586 * Allocate a new ipconfig structure.
588 * Returns: a newly-allocated #connman_ipconfig structure
590 struct connman_ipconfig *connman_ipconfig_create(int index)
592 struct connman_ipconfig *ipconfig;
594 DBG("index %d", index);
596 ipconfig = g_try_new0(struct connman_ipconfig, 1);
597 if (ipconfig == NULL)
600 ipconfig->refcount = 1;
602 ipconfig->index = index;
604 ipconfig->address = connman_ipaddress_alloc();
605 if (ipconfig->address == NULL) {
610 DBG("ipconfig %p", ipconfig);
612 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
618 * connman_ipconfig_ref:
619 * @ipconfig: ipconfig structure
621 * Increase reference counter of ipconfig
623 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
625 g_atomic_int_inc(&ipconfig->refcount);
631 * connman_ipconfig_unref:
632 * @ipconfig: ipconfig structure
634 * Decrease reference counter of ipconfig
636 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
638 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
639 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
641 connman_ipaddress_free(ipconfig->address);
647 * connman_ipconfig_get_data:
648 * @ipconfig: ipconfig structure
650 * Get private data pointer
652 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
654 return ipconfig->ops_data;
658 * connman_ipconfig_set_data:
659 * @ipconfig: ipconfig structure
660 * @data: data pointer
662 * Set private data pointer
664 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
666 ipconfig->ops_data = data;
670 * connman_ipconfig_get_index:
671 * @ipconfig: ipconfig structure
673 * Get interface index
675 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
677 return ipconfig->index;
681 * connman_ipconfig_get_ifname:
682 * @ipconfig: ipconfig structure
686 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
688 struct connman_ipdevice *ipdevice;
690 if (ipconfig->index < 0)
693 ipdevice = g_hash_table_lookup(ipdevice_hash,
694 GINT_TO_POINTER(ipconfig->index));
695 if (ipdevice == NULL)
698 return ipdevice->ifname;
702 * connman_ipconfig_set_ops:
703 * @ipconfig: ipconfig structure
704 * @ops: operation callbacks
706 * Set the operation callbacks
708 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
709 const struct connman_ipconfig_ops *ops)
715 * connman_ipconfig_set_method:
716 * @ipconfig: ipconfig structure
717 * @method: configuration method
719 * Set the configuration method
721 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
722 enum connman_ipconfig_method method)
724 ipconfig->method = method;
730 * connman_ipconfig_bind:
731 * @ipconfig: ipconfig structure
732 * @ipaddress: ipaddress structure
734 * Bind IP address details to configuration
736 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
737 struct connman_ipaddress *ipaddress)
739 connman_ipaddress_copy(ipconfig->address, ipaddress);
741 connman_inet_set_address(ipconfig->index, ipconfig->address);
744 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
747 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
749 case CONNMAN_IPCONFIG_METHOD_IGNORE:
751 case CONNMAN_IPCONFIG_METHOD_STATIC:
753 case CONNMAN_IPCONFIG_METHOD_DHCP:
760 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
762 if (g_strcmp0(method, "ignore") == 0)
763 return CONNMAN_IPCONFIG_METHOD_IGNORE;
764 else if (g_strcmp0(method, "static") == 0)
765 return CONNMAN_IPCONFIG_METHOD_STATIC;
766 else if (g_strcmp0(method, "dhcp") == 0)
767 return CONNMAN_IPCONFIG_METHOD_DHCP;
769 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
772 static void append_variant(DBusMessageIter *iter, const char *prefix,
773 const char *key, int type, void *val)
777 if (prefix == NULL) {
778 connman_dbus_dict_append_variant(iter, key, type, val);
782 str = g_strdup_printf("%s%s", prefix, key);
784 connman_dbus_dict_append_variant(iter, str, type, val);
789 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
790 DBusMessageIter *iter, const char *prefix)
794 str = __connman_ipconfig_method2string(ipconfig->method);
798 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
801 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
802 const char *key, DBusMessageIter *value)
804 int type = dbus_message_iter_get_arg_type(value);
806 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
808 if (g_strcmp0(key, "Method") == 0) {
811 if (type != DBUS_TYPE_STRING)
814 dbus_message_iter_get_basic(value, &method);
816 ipconfig->method = __connman_ipconfig_string2method(method);
823 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
824 GKeyFile *keyfile, const char *identifier, const char *prefix)
826 DBG("ipconfig %p identifier %s", ipconfig, identifier);
831 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
832 GKeyFile *keyfile, const char *identifier, const char *prefix)
834 DBG("ipconfig %p identifier %s", ipconfig, identifier);
839 int __connman_ipconfig_init(void)
843 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
844 NULL, free_ipdevice);
849 void __connman_ipconfig_cleanup(void)
853 g_hash_table_destroy(ipdevice_hash);
854 ipdevice_hash = NULL;