5 * Copyright (C) 2007-2010 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
33 static DBusConnection *connection = NULL;
35 static GHashTable *provider_hash = NULL;
37 static GSList *driver_list = NULL;
39 struct connman_route {
46 struct connman_provider {
47 struct connman_element element;
48 struct connman_service *vpn_service;
56 struct connman_provider_driver *driver;
60 void __connman_provider_append_properties(struct connman_provider *provider,
61 DBusMessageIter *iter)
63 if (provider->host != NULL)
64 connman_dbus_dict_append_basic(iter, "Host",
65 DBUS_TYPE_STRING, &provider->host);
67 if (provider->domain != NULL)
68 connman_dbus_dict_append_basic(iter, "Domain",
69 DBUS_TYPE_STRING, &provider->domain);
71 if (provider->type != NULL)
72 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
76 static struct connman_provider *connman_provider_lookup(const char *identifier)
78 struct connman_provider *provider = NULL;
80 provider = g_hash_table_lookup(provider_hash, identifier);
85 struct connman_provider *connman_provider_ref(struct connman_provider *provider)
87 DBG("provider %p", provider);
89 if (connman_element_ref(&provider->element) == NULL)
95 void connman_provider_unref(struct connman_provider *provider)
97 DBG("provider %p", provider);
99 connman_element_unref(&provider->element);
102 static gboolean match_driver(struct connman_provider *provider,
103 struct connman_provider_driver *driver)
105 if (g_strcmp0(driver->name, provider->type) == 0)
111 static int provider_probe(struct connman_provider *provider)
115 DBG("provider %p name %s", provider, provider->name);
117 if (provider->driver != NULL)
120 for (list = driver_list; list; list = list->next) {
121 struct connman_provider_driver *driver = list->data;
123 if (match_driver(provider, driver) == FALSE)
126 DBG("driver %p name %s", driver, driver->name);
128 if (driver->probe != NULL && driver->probe(provider) == 0) {
129 provider->driver = driver;
134 if (provider->driver == NULL)
140 static int provider_indicate_state(struct connman_provider *provider,
141 enum connman_service_state state)
143 DBG("state %d", state);
145 __connman_service_indicate_state(provider->vpn_service, state,
146 CONNMAN_IPCONFIG_TYPE_IPV4);
148 return __connman_service_indicate_state(provider->vpn_service, state,
149 CONNMAN_IPCONFIG_TYPE_IPV6);
152 int __connman_provider_disconnect(struct connman_provider *provider)
156 DBG("provider %p", provider);
158 if (provider->driver != NULL && provider->driver->disconnect != NULL)
159 err = provider->driver->disconnect(provider);
163 if (provider->vpn_service != NULL)
164 provider_indicate_state(provider,
165 CONNMAN_SERVICE_STATE_DISCONNECT);
168 if (err != -EINPROGRESS)
177 int __connman_provider_connect(struct connman_provider *provider)
181 DBG("provider %p", provider);
183 if (provider->driver != NULL && provider->driver->connect != NULL)
184 err = provider->driver->connect(provider);
189 if (err != -EINPROGRESS)
192 provider_indicate_state(provider,
193 CONNMAN_SERVICE_STATE_ASSOCIATION);
201 int __connman_provider_remove(const char *path)
203 struct connman_provider *provider;
207 DBG("path %s", path);
209 g_hash_table_iter_init(&iter, provider_hash);
210 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
211 const char *srv_path;
214 if (provider->vpn_service == NULL)
217 srv_path = __connman_service_get_path(provider->vpn_service);
219 if (g_strcmp0(srv_path, path) == 0) {
220 DBG("Removing VPN %s", provider->identifier);
221 g_hash_table_remove(provider_hash,
222 provider->identifier);
230 static void provider_append_routes(gpointer key, gpointer value,
233 struct connman_route *route = value;
234 struct connman_provider *provider = user_data;
235 int index = provider->element.index;
237 if (route->family == AF_INET6) {
238 unsigned char prefix_len = atoi(route->netmask);
240 connman_inet_add_ipv6_network_route(index, route->host,
244 connman_inet_add_network_route(index, route->host,
250 static int set_connected(struct connman_provider *provider,
251 connman_bool_t connected)
253 struct connman_service *service = provider->vpn_service;
254 struct connman_ipconfig *ipconfig;
259 ipconfig = __connman_service_get_ipconfig(service, provider->family);
261 if (connected == TRUE) {
262 if (ipconfig == NULL) {
263 provider_indicate_state(provider,
264 CONNMAN_SERVICE_STATE_FAILURE);
268 __connman_ipconfig_address_add(ipconfig);
269 __connman_ipconfig_gateway_add(ipconfig);
271 provider_indicate_state(provider,
272 CONNMAN_SERVICE_STATE_READY);
274 g_hash_table_foreach(provider->routes, provider_append_routes,
278 if (ipconfig != NULL) {
279 provider_indicate_state(provider,
280 CONNMAN_SERVICE_STATE_DISCONNECT);
281 __connman_ipconfig_gateway_remove(ipconfig);
284 provider_indicate_state(provider,
285 CONNMAN_SERVICE_STATE_IDLE);
291 int connman_provider_set_state(struct connman_provider *provider,
292 enum connman_provider_state state)
294 if (provider == NULL || provider->vpn_service == NULL)
298 case CONNMAN_PROVIDER_STATE_UNKNOWN:
300 case CONNMAN_PROVIDER_STATE_IDLE:
301 return set_connected(provider, FALSE);
302 case CONNMAN_PROVIDER_STATE_CONNECT:
303 return provider_indicate_state(provider,
304 CONNMAN_SERVICE_STATE_ASSOCIATION);
305 case CONNMAN_PROVIDER_STATE_READY:
306 return set_connected(provider, TRUE);
307 case CONNMAN_PROVIDER_STATE_DISCONNECT:
308 return provider_indicate_state(provider,
309 CONNMAN_SERVICE_STATE_DISCONNECT);
310 case CONNMAN_PROVIDER_STATE_FAILURE:
311 return provider_indicate_state(provider,
312 CONNMAN_SERVICE_STATE_FAILURE);
318 int connman_provider_indicate_error(struct connman_provider *provider,
319 enum connman_provider_error error)
321 enum connman_service_error service_error;
324 case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
325 service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
327 case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
328 service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
330 case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
331 service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
334 service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
338 return __connman_service_indicate_error(provider->vpn_service,
342 static void unregister_provider(gpointer data)
344 struct connman_provider *provider = data;
345 struct connman_service *service = provider->vpn_service;
347 DBG("provider %p", provider);
349 provider->vpn_service = NULL;
350 __connman_service_put(service);
352 connman_element_unregister(&provider->element);
353 connman_provider_unref(provider);
356 static void provider_destruct(struct connman_element *element)
358 struct connman_provider *provider = element->private;
360 DBG("provider %p", provider);
362 g_free(provider->name);
363 g_free(provider->type);
364 g_free(provider->domain);
365 g_free(provider->identifier);
366 g_hash_table_destroy(provider->routes);
369 static void destroy_route(gpointer user_data)
371 struct connman_route *route = user_data;
374 g_free(route->netmask);
375 g_free(route->gateway);
379 static void provider_initialize(struct connman_provider *provider)
381 DBG("provider %p", provider);
383 __connman_element_initialize(&provider->element);
385 provider->element.private = provider;
386 provider->element.destruct = provider_destruct;
388 provider->name = NULL;
389 provider->type = NULL;
390 provider->domain = NULL;
391 provider->identifier = NULL;
392 provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
393 NULL, destroy_route);
396 static struct connman_provider *connman_provider_new(void)
398 struct connman_provider *provider;
400 provider = g_try_new0(struct connman_provider, 1);
401 if (provider == NULL)
404 DBG("provider %p", provider);
405 provider_initialize(provider);
410 static struct connman_provider *connman_provider_get(const char *identifier)
412 struct connman_provider *provider;
414 provider = g_hash_table_lookup(provider_hash, identifier);
415 if (provider != NULL)
418 provider = connman_provider_new();
419 if (provider == NULL)
422 DBG("provider %p", provider);
424 provider->identifier = g_strdup(identifier);
426 g_hash_table_insert(provider_hash, provider->identifier, provider);
428 provider->element.name = g_strdup(identifier);
429 connman_element_register(&provider->element, NULL);
434 static void provider_dbus_ident(char *ident)
436 int i, len = strlen(ident);
438 for (i = 0; i < len; i++) {
439 if (ident[i] >= '0' && ident[i] <= '9')
441 if (ident[i] >= 'a' && ident[i] <= 'z')
443 if (ident[i] >= 'A' && ident[i] <= 'Z')
449 int __connman_provider_create_and_connect(DBusMessage *msg)
451 struct connman_provider *provider;
452 DBusMessageIter iter, array;
453 const char *type = NULL, *name = NULL, *service_path = NULL;
454 const char *host = NULL, *domain = NULL;
456 gboolean created = FALSE;
459 dbus_message_iter_init(msg, &iter);
460 dbus_message_iter_recurse(&iter, &array);
462 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
463 DBusMessageIter entry, value;
466 dbus_message_iter_recurse(&array, &entry);
467 dbus_message_iter_get_basic(&entry, &key);
469 dbus_message_iter_next(&entry);
470 dbus_message_iter_recurse(&entry, &value);
472 switch (dbus_message_iter_get_arg_type(&value)) {
473 case DBUS_TYPE_STRING:
474 if (g_str_equal(key, "Type") == TRUE)
475 dbus_message_iter_get_basic(&value, &type);
476 else if (g_str_equal(key, "Name") == TRUE)
477 dbus_message_iter_get_basic(&value, &name);
478 else if (g_str_equal(key, "Host") == TRUE)
479 dbus_message_iter_get_basic(&value, &host);
480 else if (g_str_equal(key, "VPN.Domain") == TRUE)
481 dbus_message_iter_get_basic(&value, &domain);
485 dbus_message_iter_next(&array);
488 if (host == NULL || domain == NULL) {
493 DBG("Type %s name %s", type, name);
495 if (type == NULL || name == NULL) {
500 ident = g_strdup_printf("%s_%s", host, domain);
501 provider_dbus_ident(ident);
503 DBG("ident %s", ident);
505 provider = connman_provider_lookup(ident);
507 if (provider == NULL) {
509 provider = connman_provider_get(ident);
511 provider->host = g_strdup(host);
512 provider->domain = g_strdup(domain);
513 provider->name = g_strdup(name);
514 provider->type = g_strdup(type);
518 if (provider == NULL) {
519 DBG("can not create provider");
523 dbus_message_iter_init(msg, &iter);
524 dbus_message_iter_recurse(&iter, &array);
526 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
527 DBusMessageIter entry, value;
528 const char *key, *str;
530 dbus_message_iter_recurse(&array, &entry);
531 dbus_message_iter_get_basic(&entry, &key);
533 dbus_message_iter_next(&entry);
534 dbus_message_iter_recurse(&entry, &value);
536 switch (dbus_message_iter_get_arg_type(&value)) {
537 case DBUS_TYPE_STRING:
538 dbus_message_iter_get_basic(&value, &str);
539 connman_provider_set_string(provider, key, str);
543 dbus_message_iter_next(&array);
548 if (provider == NULL) {
554 provider_probe(provider);
556 if (provider->vpn_service == NULL)
557 provider->vpn_service =
558 __connman_service_create_from_provider(provider);
559 if (provider->vpn_service == NULL) {
564 err = __connman_service_connect(provider->vpn_service);
565 if (err < 0 && err != -EINPROGRESS)
568 service_path = __connman_service_get_path(provider->vpn_service);
569 g_dbus_send_reply(connection, msg,
570 DBUS_TYPE_OBJECT_PATH, &service_path,
575 if (provider != NULL && created == TRUE) {
576 DBG("can not connect delete provider");
577 connman_provider_unref(provider);
579 if (provider->vpn_service != NULL) {
580 __connman_service_put(provider->vpn_service);
581 provider->vpn_service = NULL;
588 const char * __connman_provider_get_ident(struct connman_provider *provider)
590 if (provider == NULL)
593 return provider->identifier;
596 int connman_provider_set_string(struct connman_provider *provider,
597 const char *key, const char *value)
599 DBG("provider %p key %s value %s", provider, key, value);
601 if (g_str_equal(key, "Type") == TRUE) {
602 g_free(provider->type);
603 provider->type = g_strdup(value);
604 } else if (g_str_equal(key, "Name") == TRUE) {
605 g_free(provider->name);
606 provider->name = g_strdup(value);
609 return connman_element_set_string(&provider->element, key, value);
612 const char *connman_provider_get_string(struct connman_provider *provider,
615 DBG("provider %p key %s", provider, key);
617 if (g_str_equal(key, "Type") == TRUE)
618 return provider->type;
619 else if (g_str_equal(key, "Name") == TRUE)
620 return provider->name;
622 return connman_element_get_string(&provider->element, key);
625 void *connman_provider_get_data(struct connman_provider *provider)
627 return provider->driver_data;
630 void connman_provider_set_data(struct connman_provider *provider, void *data)
632 provider->driver_data = data;
635 void connman_provider_set_index(struct connman_provider *provider, int index)
637 struct connman_service *service = provider->vpn_service;
638 struct connman_ipconfig *ipconfig;
645 ipconfig = __connman_service_get_ip4config(service);
647 if (ipconfig == NULL) {
648 __connman_service_create_ip4config(service, index);
650 ipconfig = __connman_service_get_ip4config(service);
651 if (ipconfig == NULL) {
652 DBG("Couldnt create ipconfig");
657 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
658 __connman_ipconfig_set_index(ipconfig, index);
661 ipconfig = __connman_service_get_ip6config(service);
663 if (ipconfig == NULL) {
664 __connman_service_create_ip6config(service, index);
666 ipconfig = __connman_service_get_ip6config(service);
667 if (ipconfig == NULL) {
668 DBG("Couldnt create ipconfig for IPv6");
673 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
674 __connman_ipconfig_set_index(ipconfig, index);
677 provider->element.index = index;
680 int connman_provider_get_index(struct connman_provider *provider)
682 return provider->element.index;
685 int connman_provider_set_ipaddress(struct connman_provider *provider,
686 struct connman_ipaddress *ipaddress)
688 struct connman_ipconfig *ipconfig = NULL;
690 ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
692 if (ipconfig == NULL)
695 provider->family = ipaddress->family;
697 __connman_ipconfig_set_local(ipconfig, ipaddress->local);
698 __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
699 __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
700 __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
701 __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
706 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
708 DBG("provider %p pac %s", provider, pac);
710 __connman_service_set_pac(provider->vpn_service, pac);
716 int connman_provider_set_domain(struct connman_provider *provider,
719 DBG("provider %p domain %s", provider, domain);
721 g_free(provider->domain);
722 provider->domain = g_strdup(domain);
724 __connman_service_set_domainname(provider->vpn_service, domain);
729 int connman_provider_set_nameservers(struct connman_provider *provider,
730 const char *nameservers)
733 char **nameservers_array = NULL;
735 DBG("provider %p nameservers %s", provider, nameservers);
737 __connman_service_nameserver_clear(provider->vpn_service);
739 if (nameservers == NULL)
742 nameservers_array = g_strsplit(nameservers, " ", 0);
744 for (i = 0; nameservers_array[i] != NULL; i++) {
745 __connman_service_nameserver_append(provider->vpn_service,
746 nameservers_array[i]);
749 g_strfreev(nameservers_array);
754 enum provider_route_type {
755 PROVIDER_ROUTE_TYPE_NONE = 0,
756 PROVIDER_ROUTE_TYPE_MASK = 1,
757 PROVIDER_ROUTE_TYPE_ADDR = 2,
758 PROVIDER_ROUTE_TYPE_GW = 3,
761 static int route_env_parse(struct connman_provider *provider, const char *key,
762 int *family, unsigned long *idx,
763 enum provider_route_type *type)
768 DBG("name %s", provider->name);
770 if (!strcmp(provider->type, "openvpn")) {
771 if (g_str_has_prefix(key, "route_network_") == TRUE) {
772 start = key + strlen("route_network_");
773 *type = PROVIDER_ROUTE_TYPE_ADDR;
774 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
775 start = key + strlen("route_netmask_");
776 *type = PROVIDER_ROUTE_TYPE_MASK;
777 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
778 start = key + strlen("route_gateway_");
779 *type = PROVIDER_ROUTE_TYPE_GW;
784 *idx = g_ascii_strtoull(start, &end, 10);
786 } else if (!strcmp(provider->type, "openconnect")) {
787 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
789 start = key + strlen("CISCO_SPLIT_INC_");
790 } else if (g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC_") == TRUE) {
792 start = key + strlen("CISCO_IPV6_SPLIT_INC_");
796 *idx = g_ascii_strtoull(start, &end, 10);
798 if (strncmp(end, "_ADDR", 5) == 0)
799 *type = PROVIDER_ROUTE_TYPE_ADDR;
800 else if (strncmp(end, "_MASK", 5) == 0)
801 *type = PROVIDER_ROUTE_TYPE_MASK;
802 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
803 *family == AF_INET6) {
804 *type = PROVIDER_ROUTE_TYPE_MASK;
812 int connman_provider_append_route(struct connman_provider *provider,
813 const char *key, const char *value)
815 struct connman_route *route;
817 unsigned long idx = 0;
818 enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
820 DBG("key %s value %s", key, value);
822 ret = route_env_parse(provider, key, &family, &idx, &type);
826 DBG("idx %lu family %d type %d", idx, family, type);
828 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
830 route = g_try_new0(struct connman_route, 1);
832 connman_error("out of memory");
836 route->family = family;
838 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
843 case PROVIDER_ROUTE_TYPE_NONE:
845 case PROVIDER_ROUTE_TYPE_MASK:
846 route->netmask = g_strdup(value);
848 case PROVIDER_ROUTE_TYPE_ADDR:
849 route->host = g_strdup(value);
851 case PROVIDER_ROUTE_TYPE_GW:
852 route->gateway = g_strdup(value);
859 const char *connman_provider_get_driver_name(struct connman_provider *provider)
861 return provider->driver->name;
864 static gint compare_priority(gconstpointer a, gconstpointer b)
869 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
871 struct connman_provider *provider = value;
873 if (provider->driver != NULL && provider->driver->remove)
874 provider->driver->remove(provider);
877 int connman_provider_driver_register(struct connman_provider_driver *driver)
879 DBG("driver %p name %s", driver, driver->name);
881 driver_list = g_slist_insert_sorted(driver_list, driver,
886 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
888 DBG("driver %p name %s", driver, driver->name);
890 driver_list = g_slist_remove(driver_list, driver);
893 static void provider_remove(gpointer key, gpointer value,
896 struct connman_provider *provider = value;
898 g_hash_table_remove(provider_hash, provider->identifier);
901 static void provider_offline_mode(connman_bool_t enabled)
903 DBG("enabled %d", enabled);
906 g_hash_table_foreach(provider_hash, provider_remove, NULL);
910 static struct connman_notifier provider_notifier = {
912 .offline_mode = provider_offline_mode,
915 int __connman_provider_init(void)
921 connection = connman_dbus_get_connection();
923 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
924 NULL, unregister_provider);
926 err = connman_notifier_register(&provider_notifier);
928 g_hash_table_destroy(provider_hash);
929 dbus_connection_unref(connection);
935 void __connman_provider_cleanup(void)
939 connman_notifier_unregister(&provider_notifier);
941 g_hash_table_foreach(provider_hash, clean_provider, NULL);
943 g_hash_table_destroy(provider_hash);
944 provider_hash = NULL;
946 dbus_connection_unref(connection);