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
34 static DBusConnection *connection = NULL;
36 static GHashTable *provider_hash = NULL;
38 static GSList *driver_list = NULL;
40 struct connman_route {
47 struct connman_provider {
49 struct connman_service *vpn_service;
58 struct connman_provider_driver *driver;
60 GHashTable *setting_strings;
63 void __connman_provider_append_properties(struct connman_provider *provider,
64 DBusMessageIter *iter)
66 if (provider->host != NULL)
67 connman_dbus_dict_append_basic(iter, "Host",
68 DBUS_TYPE_STRING, &provider->host);
70 if (provider->domain != NULL)
71 connman_dbus_dict_append_basic(iter, "Domain",
72 DBUS_TYPE_STRING, &provider->domain);
74 if (provider->type != NULL)
75 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
79 static struct connman_provider *connman_provider_lookup(const char *identifier)
81 struct connman_provider *provider = NULL;
83 provider = g_hash_table_lookup(provider_hash, identifier);
88 static gboolean match_driver(struct connman_provider *provider,
89 struct connman_provider_driver *driver)
91 if (g_strcmp0(driver->name, provider->type) == 0)
97 static int provider_probe(struct connman_provider *provider)
101 DBG("provider %p name %s", provider, provider->name);
103 if (provider->driver != NULL)
106 for (list = driver_list; list; list = list->next) {
107 struct connman_provider_driver *driver = list->data;
109 if (match_driver(provider, driver) == FALSE)
112 DBG("driver %p name %s", driver, driver->name);
114 if (driver->probe != NULL && driver->probe(provider) == 0) {
115 provider->driver = driver;
120 if (provider->driver == NULL)
126 static void provider_remove(struct connman_provider *provider)
128 if (provider->driver != NULL) {
129 provider->driver->remove(provider);
130 provider->driver = NULL;
134 static int provider_register(struct connman_provider *provider)
136 return provider_probe(provider);
139 static void provider_unregister(struct connman_provider *provider)
141 provider_remove(provider);
144 struct connman_provider *connman_provider_ref(struct connman_provider *provider)
146 DBG("provider %p", provider);
148 g_atomic_int_inc(&provider->refcount);
153 static void provider_destruct(struct connman_provider *provider)
155 DBG("provider %p", provider);
157 g_free(provider->name);
158 g_free(provider->type);
159 g_free(provider->host);
160 g_free(provider->domain);
161 g_free(provider->identifier);
162 g_hash_table_destroy(provider->routes);
163 g_hash_table_destroy(provider->setting_strings);
166 void connman_provider_unref(struct connman_provider *provider)
168 DBG("provider %p", provider);
170 if (g_atomic_int_dec_and_test(&provider->refcount) == FALSE)
173 provider_remove(provider);
175 provider_destruct(provider);
178 static int provider_indicate_state(struct connman_provider *provider,
179 enum connman_service_state state)
181 DBG("state %d", state);
183 __connman_service_ipconfig_indicate_state(provider->vpn_service, state,
184 CONNMAN_IPCONFIG_TYPE_IPV4);
186 return __connman_service_ipconfig_indicate_state(provider->vpn_service,
187 state, CONNMAN_IPCONFIG_TYPE_IPV6);
190 int __connman_provider_disconnect(struct connman_provider *provider)
194 DBG("provider %p", provider);
196 if (provider->driver != NULL && provider->driver->disconnect != NULL)
197 err = provider->driver->disconnect(provider);
201 if (provider->vpn_service != NULL)
202 provider_indicate_state(provider,
203 CONNMAN_SERVICE_STATE_DISCONNECT);
206 if (err != -EINPROGRESS)
215 int __connman_provider_connect(struct connman_provider *provider)
219 DBG("provider %p", provider);
221 if (provider->driver != NULL && provider->driver->connect != NULL)
222 err = provider->driver->connect(provider);
227 if (err != -EINPROGRESS)
230 provider_indicate_state(provider,
231 CONNMAN_SERVICE_STATE_ASSOCIATION);
239 int __connman_provider_remove(const char *path)
241 struct connman_provider *provider;
245 DBG("path %s", path);
247 g_hash_table_iter_init(&iter, provider_hash);
248 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
249 const char *srv_path;
252 if (provider->vpn_service == NULL)
255 srv_path = __connman_service_get_path(provider->vpn_service);
257 if (g_strcmp0(srv_path, path) == 0) {
258 DBG("Removing VPN %s", provider->identifier);
260 provider_unregister(provider);
261 g_hash_table_remove(provider_hash,
262 provider->identifier);
270 static void provider_append_routes(gpointer key, gpointer value,
273 struct connman_route *route = value;
274 struct connman_provider *provider = user_data;
275 int index = provider->index;
277 if (route->family == AF_INET6) {
278 unsigned char prefix_len = atoi(route->netmask);
280 connman_inet_add_ipv6_network_route(index, route->host,
284 connman_inet_add_network_route(index, route->host,
290 static int set_connected(struct connman_provider *provider,
291 connman_bool_t connected)
293 struct connman_service *service = provider->vpn_service;
294 struct connman_ipconfig *ipconfig;
299 ipconfig = __connman_service_get_ipconfig(service, provider->family);
301 if (connected == TRUE) {
302 if (ipconfig == NULL) {
303 provider_indicate_state(provider,
304 CONNMAN_SERVICE_STATE_FAILURE);
308 __connman_ipconfig_address_add(ipconfig);
309 __connman_ipconfig_gateway_add(ipconfig);
311 provider_indicate_state(provider,
312 CONNMAN_SERVICE_STATE_READY);
314 g_hash_table_foreach(provider->routes, provider_append_routes,
318 if (ipconfig != NULL) {
319 provider_indicate_state(provider,
320 CONNMAN_SERVICE_STATE_DISCONNECT);
321 __connman_ipconfig_gateway_remove(ipconfig);
324 provider_indicate_state(provider,
325 CONNMAN_SERVICE_STATE_IDLE);
331 int connman_provider_set_state(struct connman_provider *provider,
332 enum connman_provider_state state)
334 if (provider == NULL || provider->vpn_service == NULL)
338 case CONNMAN_PROVIDER_STATE_UNKNOWN:
340 case CONNMAN_PROVIDER_STATE_IDLE:
341 return set_connected(provider, FALSE);
342 case CONNMAN_PROVIDER_STATE_CONNECT:
343 return provider_indicate_state(provider,
344 CONNMAN_SERVICE_STATE_ASSOCIATION);
345 case CONNMAN_PROVIDER_STATE_READY:
346 return set_connected(provider, TRUE);
347 case CONNMAN_PROVIDER_STATE_DISCONNECT:
348 return provider_indicate_state(provider,
349 CONNMAN_SERVICE_STATE_DISCONNECT);
350 case CONNMAN_PROVIDER_STATE_FAILURE:
351 return provider_indicate_state(provider,
352 CONNMAN_SERVICE_STATE_FAILURE);
358 int connman_provider_indicate_error(struct connman_provider *provider,
359 enum connman_provider_error error)
361 enum connman_service_error service_error;
364 case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
365 service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
367 case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
368 service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
370 case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
371 service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
374 service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
378 return __connman_service_indicate_error(provider->vpn_service,
382 static void unregister_provider(gpointer data)
384 struct connman_provider *provider = data;
385 struct connman_service *service = provider->vpn_service;
387 DBG("provider %p", provider);
389 provider->vpn_service = NULL;
390 __connman_service_put(service);
392 connman_provider_unref(provider);
395 static void destroy_route(gpointer user_data)
397 struct connman_route *route = user_data;
400 g_free(route->netmask);
401 g_free(route->gateway);
405 static void provider_initialize(struct connman_provider *provider)
407 DBG("provider %p", provider);
410 provider->name = NULL;
411 provider->type = NULL;
412 provider->domain = NULL;
413 provider->identifier = NULL;
414 provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
415 NULL, destroy_route);
416 provider->setting_strings = g_hash_table_new_full(g_str_hash, g_str_equal,
420 static struct connman_provider *connman_provider_new(void)
422 struct connman_provider *provider;
424 provider = g_try_new0(struct connman_provider, 1);
425 if (provider == NULL)
428 provider->refcount = 1;
430 DBG("provider %p", provider);
431 provider_initialize(provider);
436 static struct connman_provider *connman_provider_get(const char *identifier)
438 struct connman_provider *provider;
440 provider = g_hash_table_lookup(provider_hash, identifier);
441 if (provider != NULL)
444 provider = connman_provider_new();
445 if (provider == NULL)
448 DBG("provider %p", provider);
450 provider->identifier = g_strdup(identifier);
452 g_hash_table_insert(provider_hash, provider->identifier, provider);
454 provider->name = g_strdup(identifier);
459 static void provider_dbus_ident(char *ident)
461 int i, len = strlen(ident);
463 for (i = 0; i < len; i++) {
464 if (ident[i] >= '0' && ident[i] <= '9')
466 if (ident[i] >= 'a' && ident[i] <= 'z')
468 if (ident[i] >= 'A' && ident[i] <= 'Z')
474 int __connman_provider_create_and_connect(DBusMessage *msg)
476 struct connman_provider *provider;
477 DBusMessageIter iter, array;
478 const char *type = NULL, *name = NULL, *service_path;
479 const char *host = NULL, *domain = NULL;
483 dbus_message_iter_init(msg, &iter);
484 dbus_message_iter_recurse(&iter, &array);
486 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
487 DBusMessageIter entry, value;
490 dbus_message_iter_recurse(&array, &entry);
491 dbus_message_iter_get_basic(&entry, &key);
493 dbus_message_iter_next(&entry);
494 dbus_message_iter_recurse(&entry, &value);
496 switch (dbus_message_iter_get_arg_type(&value)) {
497 case DBUS_TYPE_STRING:
498 if (g_str_equal(key, "Type") == TRUE)
499 dbus_message_iter_get_basic(&value, &type);
500 else if (g_str_equal(key, "Name") == TRUE)
501 dbus_message_iter_get_basic(&value, &name);
502 else if (g_str_equal(key, "Host") == TRUE)
503 dbus_message_iter_get_basic(&value, &host);
504 else if (g_str_equal(key, "VPN.Domain") == TRUE)
505 dbus_message_iter_get_basic(&value, &domain);
509 dbus_message_iter_next(&array);
512 if (host == NULL || domain == NULL)
515 DBG("Type %s name %s", type, name);
517 if (type == NULL || name == NULL)
520 ident = g_strdup_printf("%s_%s", host, domain);
521 provider_dbus_ident(ident);
523 DBG("ident %s", ident);
525 provider = connman_provider_lookup(ident);
526 if (provider == NULL) {
527 provider = connman_provider_get(ident);
528 if (provider == NULL) {
529 DBG("can not create provider");
533 provider->host = g_strdup(host);
534 provider->domain = g_strdup(domain);
535 provider->name = g_strdup(name);
536 provider->type = g_strdup(type);
538 provider_register(provider);
541 dbus_message_iter_init(msg, &iter);
542 dbus_message_iter_recurse(&iter, &array);
544 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
545 DBusMessageIter entry, value;
546 const char *key, *str;
548 dbus_message_iter_recurse(&array, &entry);
549 dbus_message_iter_get_basic(&entry, &key);
551 dbus_message_iter_next(&entry);
552 dbus_message_iter_recurse(&entry, &value);
554 switch (dbus_message_iter_get_arg_type(&value)) {
555 case DBUS_TYPE_STRING:
556 dbus_message_iter_get_basic(&value, &str);
557 connman_provider_set_string(provider, key, str);
561 dbus_message_iter_next(&array);
566 if (provider->vpn_service == NULL) {
567 provider->vpn_service =
568 __connman_service_create_from_provider(provider);
569 if (provider->vpn_service == NULL) {
575 err = __connman_service_connect(provider->vpn_service);
576 if (err < 0 && err != -EINPROGRESS)
579 service_path = __connman_service_get_path(provider->vpn_service);
580 g_dbus_send_reply(connection, msg,
581 DBUS_TYPE_OBJECT_PATH, &service_path,
586 __connman_service_put(provider->vpn_service);
587 provider->vpn_service = NULL;
590 DBG("can not connect, delete provider");
592 connman_provider_unref(provider);
597 const char * __connman_provider_get_ident(struct connman_provider *provider)
599 if (provider == NULL)
602 return provider->identifier;
605 int connman_provider_set_string(struct connman_provider *provider,
606 const char *key, const char *value)
608 DBG("provider %p key %s value %s", provider, key, value);
610 if (g_str_equal(key, "Type") == TRUE) {
611 g_free(provider->type);
612 provider->type = g_strdup(value);
613 } else if (g_str_equal(key, "Name") == TRUE) {
614 g_free(provider->name);
615 provider->name = g_strdup(value);
616 } else if (g_str_equal(key, "Host") == TRUE) {
617 g_free(provider->host);
618 provider->host = g_strdup(value);
619 } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
620 g_free(provider->domain);
621 provider->domain = g_strdup(value);
623 g_hash_table_replace(provider->setting_strings,
624 g_strdup(key), g_strdup(value));
628 const char *connman_provider_get_string(struct connman_provider *provider,
631 DBG("provider %p key %s", provider, key);
633 if (g_str_equal(key, "Type") == TRUE)
634 return provider->type;
635 else if (g_str_equal(key, "Name") == TRUE)
636 return provider->name;
637 else if (g_str_equal(key, "Host") == TRUE)
638 return provider->host;
639 else if (g_str_equal(key, "VPN.Domain") == TRUE)
640 return provider->domain;
642 return g_hash_table_lookup(provider->setting_strings, key);
645 void *connman_provider_get_data(struct connman_provider *provider)
647 return provider->driver_data;
650 void connman_provider_set_data(struct connman_provider *provider, void *data)
652 provider->driver_data = data;
655 void connman_provider_set_index(struct connman_provider *provider, int index)
657 struct connman_service *service = provider->vpn_service;
658 struct connman_ipconfig *ipconfig;
665 ipconfig = __connman_service_get_ip4config(service);
667 if (ipconfig == NULL) {
668 __connman_service_create_ip4config(service, index);
670 ipconfig = __connman_service_get_ip4config(service);
671 if (ipconfig == NULL) {
672 DBG("Couldnt create ipconfig");
677 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
678 __connman_ipconfig_set_index(ipconfig, index);
681 ipconfig = __connman_service_get_ip6config(service);
683 if (ipconfig == NULL) {
684 __connman_service_create_ip6config(service, index);
686 ipconfig = __connman_service_get_ip6config(service);
687 if (ipconfig == NULL) {
688 DBG("Couldnt create ipconfig for IPv6");
693 connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
694 __connman_ipconfig_set_index(ipconfig, index);
697 provider->index = index;
700 int connman_provider_get_index(struct connman_provider *provider)
702 return provider->index;
705 int connman_provider_set_ipaddress(struct connman_provider *provider,
706 struct connman_ipaddress *ipaddress)
708 struct connman_ipconfig *ipconfig = NULL;
710 ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
712 if (ipconfig == NULL)
715 provider->family = ipaddress->family;
717 __connman_ipconfig_set_local(ipconfig, ipaddress->local);
718 __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
719 __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
720 __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
721 __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
726 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
728 DBG("provider %p pac %s", provider, pac);
730 __connman_service_set_pac(provider->vpn_service, pac);
736 int connman_provider_set_domain(struct connman_provider *provider,
739 DBG("provider %p domain %s", provider, domain);
741 g_free(provider->domain);
742 provider->domain = g_strdup(domain);
744 __connman_service_set_domainname(provider->vpn_service, domain);
749 int connman_provider_set_nameservers(struct connman_provider *provider,
750 const char *nameservers)
753 char **nameservers_array = NULL;
755 DBG("provider %p nameservers %s", provider, nameservers);
757 __connman_service_nameserver_clear(provider->vpn_service);
759 if (nameservers == NULL)
762 nameservers_array = g_strsplit(nameservers, " ", 0);
764 for (i = 0; nameservers_array[i] != NULL; i++) {
765 __connman_service_nameserver_append(provider->vpn_service,
766 nameservers_array[i]);
769 g_strfreev(nameservers_array);
774 enum provider_route_type {
775 PROVIDER_ROUTE_TYPE_NONE = 0,
776 PROVIDER_ROUTE_TYPE_MASK = 1,
777 PROVIDER_ROUTE_TYPE_ADDR = 2,
778 PROVIDER_ROUTE_TYPE_GW = 3,
781 static int route_env_parse(struct connman_provider *provider, const char *key,
782 int *family, unsigned long *idx,
783 enum provider_route_type *type)
788 DBG("name %s", provider->name);
790 if (!strcmp(provider->type, "openvpn")) {
791 if (g_str_has_prefix(key, "route_network_") == TRUE) {
792 start = key + strlen("route_network_");
793 *type = PROVIDER_ROUTE_TYPE_ADDR;
794 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
795 start = key + strlen("route_netmask_");
796 *type = PROVIDER_ROUTE_TYPE_MASK;
797 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
798 start = key + strlen("route_gateway_");
799 *type = PROVIDER_ROUTE_TYPE_GW;
804 *idx = g_ascii_strtoull(start, &end, 10);
806 } else if (!strcmp(provider->type, "openconnect")) {
807 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
809 start = key + strlen("CISCO_SPLIT_INC_");
810 } else if (g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC_") == TRUE) {
812 start = key + strlen("CISCO_IPV6_SPLIT_INC_");
816 *idx = g_ascii_strtoull(start, &end, 10);
818 if (strncmp(end, "_ADDR", 5) == 0)
819 *type = PROVIDER_ROUTE_TYPE_ADDR;
820 else if (strncmp(end, "_MASK", 5) == 0)
821 *type = PROVIDER_ROUTE_TYPE_MASK;
822 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
823 *family == AF_INET6) {
824 *type = PROVIDER_ROUTE_TYPE_MASK;
832 int connman_provider_append_route(struct connman_provider *provider,
833 const char *key, const char *value)
835 struct connman_route *route;
837 unsigned long idx = 0;
838 enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
840 DBG("key %s value %s", key, value);
842 ret = route_env_parse(provider, key, &family, &idx, &type);
846 DBG("idx %lu family %d type %d", idx, family, type);
848 route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
850 route = g_try_new0(struct connman_route, 1);
852 connman_error("out of memory");
856 route->family = family;
858 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
863 case PROVIDER_ROUTE_TYPE_NONE:
865 case PROVIDER_ROUTE_TYPE_MASK:
866 route->netmask = g_strdup(value);
868 case PROVIDER_ROUTE_TYPE_ADDR:
869 route->host = g_strdup(value);
871 case PROVIDER_ROUTE_TYPE_GW:
872 route->gateway = g_strdup(value);
879 const char *connman_provider_get_driver_name(struct connman_provider *provider)
881 return provider->driver->name;
884 static gint compare_priority(gconstpointer a, gconstpointer b)
889 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
891 struct connman_provider *provider = value;
893 if (provider->driver != NULL && provider->driver->remove)
894 provider->driver->remove(provider);
897 int connman_provider_driver_register(struct connman_provider_driver *driver)
899 DBG("driver %p name %s", driver, driver->name);
901 driver_list = g_slist_insert_sorted(driver_list, driver,
906 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
908 DBG("driver %p name %s", driver, driver->name);
910 driver_list = g_slist_remove(driver_list, driver);
913 static void provider_remove_all(gpointer key, gpointer value,
916 struct connman_provider *provider = value;
918 __connman_provider_remove(provider->identifier);
921 static void provider_offline_mode(connman_bool_t enabled)
923 DBG("enabled %d", enabled);
926 g_hash_table_foreach(provider_hash, provider_remove_all, NULL);
930 static struct connman_notifier provider_notifier = {
932 .offline_mode = provider_offline_mode,
935 int __connman_provider_init(void)
941 connection = connman_dbus_get_connection();
943 provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
944 NULL, unregister_provider);
946 err = connman_notifier_register(&provider_notifier);
948 g_hash_table_destroy(provider_hash);
949 dbus_connection_unref(connection);
955 void __connman_provider_cleanup(void)
959 connman_notifier_unregister(&provider_notifier);
961 g_hash_table_foreach(provider_hash, clean_provider, NULL);
963 g_hash_table_destroy(provider_hash);
964 provider_hash = NULL;
966 dbus_connection_unref(connection);