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);
261 void __connman_ipconfig_newlink(int index, unsigned short type,
264 struct connman_ipdevice *ipdevice;
267 gboolean up = FALSE, down = FALSE;
268 gboolean lower_up = FALSE, lower_down = FALSE;
270 DBG("index %d", index);
272 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
273 if (ipdevice != NULL)
276 ipdevice = g_try_new0(struct connman_ipdevice, 1);
277 if (ipdevice == NULL)
280 ipdevice->index = index;
281 ipdevice->ifname = connman_inet_ifname(index);
282 ipdevice->type = type;
284 ipdevice->config = connman_ipconfig_create(index);
285 if (ipdevice->config == NULL) {
286 g_free(ipdevice->ifname);
291 if (type == ARPHRD_ETHER)
292 connman_ipconfig_set_method(ipdevice->config,
293 CONNMAN_IPCONFIG_METHOD_IGNORE);
295 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
297 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
298 index, type, type2str(type));
301 if (flags == ipdevice->flags)
304 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
311 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
312 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
313 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
314 (IFF_RUNNING | IFF_LOWER_UP))
316 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
320 ipdevice->flags = flags;
322 str = g_string_new(NULL);
327 g_string_append(str, "UP");
329 g_string_append(str, "DOWN");
331 if (flags & IFF_RUNNING)
332 g_string_append(str, ",RUNNING");
334 if (flags & IFF_LOWER_UP)
335 g_string_append(str, ",LOWER_UP");
337 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
340 g_string_free(str, TRUE);
342 for (list = g_list_first(ipconfig_list); list;
343 list = g_list_next(list)) {
344 struct connman_ipconfig *ipconfig = list->data;
346 if (index != ipconfig->index)
350 if (up == TRUE && ipconfig->ops->up)
351 ipconfig->ops->up(ipconfig);
352 if (lower_up == TRUE && ipconfig->ops->lower_up)
353 ipconfig->ops->lower_up(ipconfig);
355 if (lower_down == TRUE && ipconfig->ops->lower_down)
356 ipconfig->ops->lower_down(ipconfig);
357 if (down == TRUE && ipconfig->ops->down)
358 ipconfig->ops->down(ipconfig);
363 __connman_ipconfig_lower_up(ipdevice);
365 __connman_ipconfig_lower_down(ipdevice);
368 void __connman_ipconfig_dellink(int index)
370 struct connman_ipdevice *ipdevice;
373 DBG("index %d", index);
375 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
376 if (ipdevice == NULL)
379 for (list = g_list_first(ipconfig_list); list;
380 list = g_list_next(list)) {
381 struct connman_ipconfig *ipconfig = list->data;
383 if (index != ipconfig->index)
386 ipconfig->index = -1;
388 if (ipconfig->ops && ipconfig->ops->lower_down)
389 ipconfig->ops->lower_down(ipconfig);
390 if (ipconfig->ops && ipconfig->ops->down)
391 ipconfig->ops->down(ipconfig);
394 __connman_ipconfig_lower_down(ipdevice);
396 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
399 void __connman_ipconfig_newaddr(int index, const char *label,
400 unsigned char prefixlen, const char *address)
402 struct connman_ipdevice *ipdevice;
403 struct connman_ipaddress *ipaddress;
405 DBG("index %d", index);
407 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
408 if (ipdevice == NULL)
411 ipaddress = connman_ipaddress_alloc();
412 if (ipaddress == NULL)
415 ipaddress->prefixlen = prefixlen;
416 ipaddress->local = g_strdup(address);
418 ipdevice->address_list = g_slist_append(ipdevice->address_list,
421 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
422 address, prefixlen, label);
425 void __connman_ipconfig_deladdr(int index, const char *label,
426 unsigned char prefixlen, const char *address)
428 struct connman_ipdevice *ipdevice;
429 struct connman_ipaddress *ipaddress;
431 DBG("index %d", index);
433 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
434 if (ipdevice == NULL)
437 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
438 if (ipaddress == NULL)
441 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
444 connman_ipaddress_free(ipaddress);
446 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
447 address, prefixlen, label);
450 void __connman_ipconfig_newroute(int index, unsigned char scope,
451 const char *dst, const char *gateway)
453 struct connman_ipdevice *ipdevice;
455 DBG("index %d", index);
457 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
458 if (ipdevice == NULL)
461 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
462 g_free(ipdevice->gateway);
463 ipdevice->gateway = g_strdup(gateway);
466 connman_info("%s {add} route %s gw %s scope %u <%s>",
467 ipdevice->ifname, dst, gateway,
468 scope, scope2str(scope));
471 void __connman_ipconfig_delroute(int index, unsigned char scope,
472 const char *dst, const char *gateway)
474 struct connman_ipdevice *ipdevice;
476 DBG("index %d", index);
478 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
479 if (ipdevice == NULL)
482 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
483 g_free(ipdevice->gateway);
484 ipdevice->gateway = NULL;
487 connman_info("%s {del} route %s gw %s scope %u <%s>",
488 ipdevice->ifname, dst, gateway,
489 scope, scope2str(scope));
492 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
497 keys = g_hash_table_get_keys(ipdevice_hash);
501 for (list = g_list_first(keys); list; list = g_list_next(list)) {
502 int index = GPOINTER_TO_INT(list->data);
504 function(index, user_data);
510 unsigned short __connman_ipconfig_get_type(int index)
512 struct connman_ipdevice *ipdevice;
514 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
515 if (ipdevice == NULL)
518 return ipdevice->type;
521 unsigned int __connman_ipconfig_get_flags(int index)
523 struct connman_ipdevice *ipdevice;
525 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
526 if (ipdevice == NULL)
529 return ipdevice->flags;
532 const char *__connman_ipconfig_get_gateway(int index)
534 struct connman_ipdevice *ipdevice;
536 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
537 if (ipdevice == NULL)
540 return ipdevice->gateway;
544 * connman_ipconfig_create:
546 * Allocate a new ipconfig structure.
548 * Returns: a newly-allocated #connman_ipconfig structure
550 struct connman_ipconfig *connman_ipconfig_create(int index)
552 struct connman_ipconfig *ipconfig;
554 DBG("index %d", index);
556 ipconfig = g_try_new0(struct connman_ipconfig, 1);
557 if (ipconfig == NULL)
560 ipconfig->refcount = 1;
562 ipconfig->index = index;
564 ipconfig->address = connman_ipaddress_alloc();
565 if (ipconfig->address == NULL) {
570 DBG("ipconfig %p", ipconfig);
572 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
578 * connman_ipconfig_ref:
579 * @ipconfig: ipconfig structure
581 * Increase reference counter of ipconfig
583 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
585 g_atomic_int_inc(&ipconfig->refcount);
591 * connman_ipconfig_unref:
592 * @ipconfig: ipconfig structure
594 * Decrease reference counter of ipconfig
596 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
598 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
599 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
601 connman_ipaddress_free(ipconfig->address);
607 * connman_ipconfig_get_data:
608 * @ipconfig: ipconfig structure
610 * Get private data pointer
612 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
614 return ipconfig->ops_data;
618 * connman_ipconfig_set_data:
619 * @ipconfig: ipconfig structure
620 * @data: data pointer
622 * Set private data pointer
624 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
626 ipconfig->ops_data = data;
630 * connman_ipconfig_get_index:
631 * @ipconfig: ipconfig structure
633 * Get interface index
635 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
637 return ipconfig->index;
641 * connman_ipconfig_get_ifname:
642 * @ipconfig: ipconfig structure
646 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
648 struct connman_ipdevice *ipdevice;
650 if (ipconfig->index < 0)
653 ipdevice = g_hash_table_lookup(ipdevice_hash,
654 GINT_TO_POINTER(ipconfig->index));
655 if (ipdevice == NULL)
658 return ipdevice->ifname;
662 * connman_ipconfig_set_ops:
663 * @ipconfig: ipconfig structure
664 * @ops: operation callbacks
666 * Set the operation callbacks
668 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
669 const struct connman_ipconfig_ops *ops)
675 * connman_ipconfig_set_method:
676 * @ipconfig: ipconfig structure
677 * @method: configuration method
679 * Set the configuration method
681 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
682 enum connman_ipconfig_method method)
684 ipconfig->method = method;
690 * connman_ipconfig_bind:
691 * @ipconfig: ipconfig structure
692 * @ipaddress: ipaddress structure
694 * Bind IP address details to configuration
696 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
697 struct connman_ipaddress *ipaddress)
699 connman_ipaddress_copy(ipconfig->address, ipaddress);
702 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
705 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
707 case CONNMAN_IPCONFIG_METHOD_IGNORE:
709 case CONNMAN_IPCONFIG_METHOD_STATIC:
711 case CONNMAN_IPCONFIG_METHOD_DHCP:
718 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
720 if (g_strcmp0(method, "ignore") == 0)
721 return CONNMAN_IPCONFIG_METHOD_IGNORE;
722 else if (g_strcmp0(method, "static") == 0)
723 return CONNMAN_IPCONFIG_METHOD_STATIC;
724 else if (g_strcmp0(method, "dhcp") == 0)
725 return CONNMAN_IPCONFIG_METHOD_DHCP;
727 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
730 static void append_variant(DBusMessageIter *iter, const char *prefix,
731 const char *key, int type, void *val)
735 if (prefix == NULL) {
736 connman_dbus_dict_append_variant(iter, key, type, val);
740 str = g_strdup_printf("%s%s", prefix, key);
742 connman_dbus_dict_append_variant(iter, str, type, val);
747 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
748 DBusMessageIter *iter, const char *prefix)
752 str = __connman_ipconfig_method2string(ipconfig->method);
756 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
759 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
760 const char *key, DBusMessageIter *value)
762 int type = dbus_message_iter_get_arg_type(value);
764 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
766 if (g_strcmp0(key, "Method") == 0) {
769 if (type != DBUS_TYPE_STRING)
772 dbus_message_iter_get_basic(value, &method);
774 ipconfig->method = __connman_ipconfig_string2method(method);
781 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
782 GKeyFile *keyfile, const char *identifier, const char *prefix)
784 DBG("ipconfig %p identifier %s", ipconfig, identifier);
789 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
790 GKeyFile *keyfile, const char *identifier, const char *prefix)
792 DBG("ipconfig %p identifier %s", ipconfig, identifier);
797 int __connman_ipconfig_init(void)
801 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
802 NULL, free_ipdevice);
807 void __connman_ipconfig_cleanup(void)
811 g_hash_table_destroy(ipdevice_hash);
812 ipdevice_hash = NULL;