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_ipaddress {
38 unsigned char prefixlen;
42 struct connman_ipdevice {
52 struct connman_ipconfig {
56 const struct connman_ipconfig_ops *ops;
59 enum connman_ipconfig_method method;
62 static GHashTable *ipdevice_hash = NULL;
63 static GList *ipconfig_list = NULL;
65 static void free_address_list(struct connman_ipdevice *ipdevice)
69 for (list = ipdevice->address_list; list; list = list->next) {
70 struct connman_ipaddress *ipaddress = list->data;
72 g_free(ipaddress->address);
77 g_slist_free(ipdevice->address_list);
78 ipdevice->address_list = NULL;
81 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
82 unsigned char prefixlen, const char *address)
86 for (list = ipdevice->address_list; list; list = list->next) {
87 struct connman_ipaddress *ipaddress = list->data;
89 if (g_strcmp0(ipaddress->address, address) == 0 &&
90 ipaddress->prefixlen == prefixlen)
97 static const char *type2str(unsigned short type)
102 case ARPHRD_LOOPBACK:
115 static const char *scope2str(unsigned char scope)
127 static void free_ipdevice(gpointer data)
129 struct connman_ipdevice *ipdevice = data;
131 connman_info("%s {remove} index %d", ipdevice->ifname,
134 free_address_list(ipdevice);
135 g_free(ipdevice->gateway);
137 g_free(ipdevice->ifname);
141 void __connman_ipconfig_newlink(int index, unsigned short type,
144 struct connman_ipdevice *ipdevice;
147 gboolean up = FALSE, down = FALSE;
148 gboolean lower_up = FALSE, lower_down = FALSE;
150 DBG("index %d", index);
152 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
153 if (ipdevice != NULL)
156 ipdevice = g_try_new0(struct connman_ipdevice, 1);
157 if (ipdevice == NULL)
160 ipdevice->index = index;
161 ipdevice->ifname = connman_inet_ifname(index);
162 ipdevice->type = type;
164 g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
166 connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
167 index, type, type2str(type));
170 if (flags == ipdevice->flags)
173 if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
180 if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
181 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
182 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
183 (IFF_RUNNING | IFF_LOWER_UP))
185 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
189 ipdevice->flags = flags;
191 str = g_string_new(NULL);
196 g_string_append(str, "UP");
198 g_string_append(str, "DOWN");
200 if (flags & IFF_RUNNING)
201 g_string_append(str, ",RUNNING");
203 if (flags & IFF_LOWER_UP)
204 g_string_append(str, ",LOWER_UP");
206 connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
209 g_string_free(str, TRUE);
211 for (list = g_list_first(ipconfig_list); list;
212 list = g_list_next(list)) {
213 struct connman_ipconfig *ipconfig = list->data;
215 if (index != ipconfig->index)
219 if (up == TRUE && ipconfig->ops->up)
220 ipconfig->ops->up(ipconfig);
221 if (lower_up == TRUE && ipconfig->ops->lower_up)
222 ipconfig->ops->lower_up(ipconfig);
224 if (lower_down == TRUE && ipconfig->ops->lower_down)
225 ipconfig->ops->lower_down(ipconfig);
226 if (down == TRUE && ipconfig->ops->down)
227 ipconfig->ops->down(ipconfig);
232 void __connman_ipconfig_dellink(int index)
236 DBG("index %d", index);
238 for (list = g_list_first(ipconfig_list); list;
239 list = g_list_next(list)) {
240 struct connman_ipconfig *ipconfig = list->data;
242 if (index != ipconfig->index)
245 ipconfig->index = -1;
247 if (ipconfig->ops && ipconfig->ops->lower_down)
248 ipconfig->ops->lower_down(ipconfig);
249 if (ipconfig->ops && ipconfig->ops->down)
250 ipconfig->ops->down(ipconfig);
253 g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
256 void __connman_ipconfig_newaddr(int index, const char *label,
257 unsigned char prefixlen, const char *address)
259 struct connman_ipdevice *ipdevice;
260 struct connman_ipaddress *ipaddress;
262 DBG("index %d", index);
264 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
265 if (ipdevice == NULL)
268 ipaddress = g_try_new0(struct connman_ipaddress, 1);
269 if (ipaddress == NULL)
272 ipaddress->prefixlen = prefixlen;
273 ipaddress->address = g_strdup(address);
275 ipdevice->address_list = g_slist_append(ipdevice->address_list,
278 connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
279 address, prefixlen, label);
282 void __connman_ipconfig_deladdr(int index, const char *label,
283 unsigned char prefixlen, const char *address)
285 struct connman_ipdevice *ipdevice;
286 struct connman_ipaddress *ipaddress;
288 DBG("index %d", index);
290 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
291 if (ipdevice == NULL)
294 ipaddress = find_ipaddress(ipdevice, prefixlen, address);
295 if (ipaddress == NULL)
298 ipdevice->address_list = g_slist_remove(ipdevice->address_list,
301 g_free(ipaddress->address);
304 connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
305 address, prefixlen, label);
308 void __connman_ipconfig_newroute(int index, unsigned char scope,
309 const char *dst, const char *gateway)
311 struct connman_ipdevice *ipdevice;
313 DBG("index %d", index);
315 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
316 if (ipdevice == NULL)
319 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
320 g_free(ipdevice->gateway);
321 ipdevice->gateway = g_strdup(gateway);
324 connman_info("%s {add} route %s gw %s scope %u <%s>",
325 ipdevice->ifname, dst, gateway,
326 scope, scope2str(scope));
329 void __connman_ipconfig_delroute(int index, unsigned char scope,
330 const char *dst, const char *gateway)
332 struct connman_ipdevice *ipdevice;
334 DBG("index %d", index);
336 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
337 if (ipdevice == NULL)
340 if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
341 g_free(ipdevice->gateway);
342 ipdevice->gateway = NULL;
345 connman_info("%s {del} route %s gw %s scope %u <%s>",
346 ipdevice->ifname, dst, gateway,
347 scope, scope2str(scope));
350 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
355 keys = g_hash_table_get_keys(ipdevice_hash);
359 for (list = g_list_first(keys); list; list = g_list_next(list)) {
360 int index = GPOINTER_TO_INT(list->data);
362 function(index, user_data);
368 unsigned short __connman_ipconfig_get_type(int index)
370 struct connman_ipdevice *ipdevice;
372 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
373 if (ipdevice == NULL)
376 return ipdevice->type;
379 unsigned int __connman_ipconfig_get_flags(int index)
381 struct connman_ipdevice *ipdevice;
383 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
384 if (ipdevice == NULL)
387 return ipdevice->flags;
390 const char *__connman_ipconfig_get_gateway(int index)
392 struct connman_ipdevice *ipdevice;
394 ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
395 if (ipdevice == NULL)
398 return ipdevice->gateway;
402 * connman_ipconfig_create:
404 * Allocate a new ipconfig structure.
406 * Returns: a newly-allocated #connman_ipconfig structure
408 struct connman_ipconfig *connman_ipconfig_create(int index)
410 struct connman_ipconfig *ipconfig;
412 DBG("index %d", index);
414 ipconfig = g_try_new0(struct connman_ipconfig, 1);
415 if (ipconfig == NULL)
418 ipconfig->refcount = 1;
420 ipconfig->index = index;
422 DBG("ipconfig %p", ipconfig);
424 ipconfig_list = g_list_append(ipconfig_list, ipconfig);
430 * connman_ipconfig_ref:
431 * @ipconfig: ipconfig structure
433 * Increase reference counter of ipconfig
435 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
437 g_atomic_int_inc(&ipconfig->refcount);
443 * connman_ipconfig_unref:
444 * @ipconfig: ipconfig structure
446 * Decrease reference counter of ipconfig
448 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
450 if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
451 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
458 * connman_ipconfig_get_data:
459 * @ipconfig: ipconfig structure
461 * Get private data pointer
463 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
465 return ipconfig->ops_data;
469 * connman_ipconfig_set_data:
470 * @ipconfig: ipconfig structure
471 * @data: data pointer
473 * Set private data pointer
475 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
477 ipconfig->ops_data = data;
481 * connman_ipconfig_get_index:
482 * @ipconfig: ipconfig structure
484 * Get interface index
486 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
488 return ipconfig->index;
492 * connman_ipconfig_get_ifname:
493 * @ipconfig: ipconfig structure
497 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
499 struct connman_ipdevice *ipdevice;
501 if (ipconfig->index < 0)
504 ipdevice = g_hash_table_lookup(ipdevice_hash,
505 GINT_TO_POINTER(ipconfig->index));
506 if (ipdevice == NULL)
509 return ipdevice->ifname;
513 * connman_ipconfig_set_ops:
514 * @ipconfig: ipconfig structure
515 * @ops: operation callbacks
517 * Set the operation callbacks
519 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
520 const struct connman_ipconfig_ops *ops)
526 * connman_ipconfig_set_method:
527 * @ipconfig: ipconfig structure
528 * @method: configuration method
530 * Set the configuration method
532 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
533 enum connman_ipconfig_method method)
535 ipconfig->method = method;
540 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
543 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
545 case CONNMAN_IPCONFIG_METHOD_IGNORE:
547 case CONNMAN_IPCONFIG_METHOD_STATIC:
549 case CONNMAN_IPCONFIG_METHOD_DHCP:
556 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
558 if (g_strcmp0(method, "ignore") == 0)
559 return CONNMAN_IPCONFIG_METHOD_IGNORE;
560 else if (g_strcmp0(method, "static") == 0)
561 return CONNMAN_IPCONFIG_METHOD_STATIC;
562 else if (g_strcmp0(method, "dhcp") == 0)
563 return CONNMAN_IPCONFIG_METHOD_DHCP;
565 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
568 static void append_variant(DBusMessageIter *iter, const char *prefix,
569 const char *key, int type, void *val)
573 if (prefix == NULL) {
574 connman_dbus_dict_append_variant(iter, key, type, val);
578 str = g_strdup_printf("%s%s", prefix, key);
580 connman_dbus_dict_append_variant(iter, str, type, val);
585 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
586 DBusMessageIter *iter, const char *prefix)
590 str = __connman_ipconfig_method2string(ipconfig->method);
594 append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
597 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
598 const char *key, DBusMessageIter *value)
600 int type = dbus_message_iter_get_arg_type(value);
602 DBG("ipconfig %p key %s type %d", ipconfig, key, type);
604 if (g_strcmp0(key, "Method") == 0) {
607 if (type != DBUS_TYPE_STRING)
610 dbus_message_iter_get_basic(value, &method);
612 ipconfig->method = __connman_ipconfig_string2method(method);
619 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
620 GKeyFile *keyfile, const char *identifier, const char *prefix)
622 DBG("ipconfig %p identifier %s", ipconfig, identifier);
627 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
628 GKeyFile *keyfile, const char *identifier, const char *prefix)
630 DBG("ipconfig %p identifier %s", ipconfig, identifier);
635 static GSList *driver_list = NULL;
637 static gint compare_priority(gconstpointer a, gconstpointer b)
639 const struct connman_ipconfig_driver *driver1 = a;
640 const struct connman_ipconfig_driver *driver2 = b;
642 return driver2->priority - driver1->priority;
646 * connman_ipconfig_driver_register:
647 * @driver: IP configuration driver
649 * Register a new IP configuration driver
651 * Returns: %0 on success
653 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
655 DBG("driver %p name %s", driver, driver->name);
657 driver_list = g_slist_insert_sorted(driver_list, driver,
664 * connman_ipconfig_driver_unregister:
665 * @driver: IP configuration driver
667 * Remove a previously registered IP configuration driver.
669 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
671 DBG("driver %p name %s", driver, driver->name);
673 driver_list = g_slist_remove(driver_list, driver);
676 int __connman_ipconfig_init(void)
680 ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
681 NULL, free_ipdevice);
686 void __connman_ipconfig_cleanup(void)
690 g_hash_table_destroy(ipdevice_hash);
691 ipdevice_hash = NULL;