Imported Upstream version 1.37
[platform/upstream/connman.git] / src / provider.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <gdbus.h>
31 #include <gweb/gresolv.h>
32
33 #include "connman.h"
34
35 static DBusConnection *connection = NULL;
36
37 static GHashTable *provider_hash = NULL;
38
39 static GSList *driver_list = NULL;
40
41 struct connman_provider {
42         int refcount;
43         bool immutable;
44         struct connman_service *vpn_service;
45         int index;
46         char *identifier;
47         int family;
48         struct connman_provider_driver *driver;
49         void *driver_data;
50 };
51
52 void __connman_provider_append_properties(struct connman_provider *provider,
53                                                         DBusMessageIter *iter)
54 {
55         const char *host, *domain, *type;
56
57         if (!provider->driver || !provider->driver->get_property)
58                 return;
59
60         host = provider->driver->get_property(provider, "Host");
61         domain = provider->driver->get_property(provider, "Domain");
62         type = provider->driver->get_property(provider, "Type");
63
64         if (host)
65                 connman_dbus_dict_append_basic(iter, "Host",
66                                         DBUS_TYPE_STRING, &host);
67
68         if (domain)
69                 connman_dbus_dict_append_basic(iter, "Domain",
70                                         DBUS_TYPE_STRING, &domain);
71
72         if (type)
73                 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
74                                                  &type);
75 }
76
77 struct connman_provider *
78 connman_provider_ref_debug(struct connman_provider *provider,
79                         const char *file, int line, const char *caller)
80 {
81         DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
82                 file, line, caller);
83
84         __sync_fetch_and_add(&provider->refcount, 1);
85
86         return provider;
87 }
88
89 static void provider_remove(struct connman_provider *provider)
90 {
91         if (provider->driver) {
92                 provider->driver->remove(provider);
93                 provider->driver = NULL;
94         }
95 }
96
97 static void provider_destruct(struct connman_provider *provider)
98 {
99         DBG("provider %p", provider);
100
101         g_free(provider->identifier);
102         g_free(provider);
103 }
104
105 void connman_provider_unref_debug(struct connman_provider *provider,
106                                 const char *file, int line, const char *caller)
107 {
108         DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
109                 file, line, caller);
110
111         if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
112                 return;
113
114         provider_destruct(provider);
115 }
116
117 static int provider_indicate_state(struct connman_provider *provider,
118                                         enum connman_service_state state)
119 {
120         DBG("state %d", state);
121
122         __connman_service_ipconfig_indicate_state(provider->vpn_service, state,
123                                         CONNMAN_IPCONFIG_TYPE_IPV4);
124
125         return __connman_service_ipconfig_indicate_state(provider->vpn_service,
126                                         state, CONNMAN_IPCONFIG_TYPE_IPV6);
127 }
128
129 int connman_provider_disconnect(struct connman_provider *provider)
130 {
131         int err;
132
133         DBG("provider %p", provider);
134
135         if (provider->driver && provider->driver->disconnect)
136                 err = provider->driver->disconnect(provider);
137         else
138                 return -EOPNOTSUPP;
139
140         if (provider->vpn_service)
141                 provider_indicate_state(provider,
142                                         CONNMAN_SERVICE_STATE_DISCONNECT);
143
144         if (err < 0)
145                 return err;
146
147         if (provider->vpn_service)
148                 provider_indicate_state(provider,
149                                         CONNMAN_SERVICE_STATE_IDLE);
150
151         return 0;
152 }
153
154 int connman_provider_remove(struct connman_provider *provider)
155 {
156         DBG("Removing VPN %s", provider->identifier);
157
158         provider_remove(provider);
159
160         connman_provider_set_state(provider, CONNMAN_PROVIDER_STATE_IDLE);
161
162         g_hash_table_remove(provider_hash, provider->identifier);
163
164         return 0;
165 }
166
167 int __connman_provider_connect(struct connman_provider *provider,
168                                         const char *dbus_sender)
169 {
170         int err;
171
172         DBG("provider %p", provider);
173
174         if (provider->driver && provider->driver->connect)
175                 err = provider->driver->connect(provider, dbus_sender);
176         else
177                 return -EOPNOTSUPP;
178
179         if (err < 0) {
180                 if (err != -EINPROGRESS)
181                         return err;
182
183                 provider_indicate_state(provider,
184                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
185
186                 return -EINPROGRESS;
187         }
188
189         return 0;
190 }
191
192 int __connman_provider_remove_by_path(const char *path)
193 {
194         struct connman_provider *provider;
195         GHashTableIter iter;
196         gpointer value, key;
197
198         DBG("path %s", path);
199
200         g_hash_table_iter_init(&iter, provider_hash);
201         while (g_hash_table_iter_next(&iter, &key, &value)) {
202                 const char *srv_path;
203                 provider = value;
204
205                 if (!provider->vpn_service)
206                         continue;
207
208                 srv_path = __connman_service_get_path(provider->vpn_service);
209
210                 if (g_strcmp0(srv_path, path) == 0) {
211                         DBG("Removing VPN %s", provider->identifier);
212
213                         provider_remove(provider);
214
215                         connman_provider_set_state(provider,
216                                                 CONNMAN_PROVIDER_STATE_IDLE);
217
218                         g_hash_table_remove(provider_hash,
219                                                 provider->identifier);
220                         return 0;
221                 }
222         }
223
224         return -ENXIO;
225 }
226
227 static int set_connected(struct connman_provider *provider,
228                                         bool connected)
229 {
230         struct connman_service *service = provider->vpn_service;
231         struct connman_ipconfig *ipconfig;
232
233         if (!service)
234                 return -ENODEV;
235
236         ipconfig = __connman_service_get_ipconfig(service, provider->family);
237
238         if (connected) {
239                 if (!ipconfig) {
240                         provider_indicate_state(provider,
241                                                 CONNMAN_SERVICE_STATE_FAILURE);
242                         return -EIO;
243                 }
244
245                 __connman_ipconfig_address_add(ipconfig);
246                 __connman_ipconfig_gateway_add(ipconfig);
247
248                 provider_indicate_state(provider,
249                                         CONNMAN_SERVICE_STATE_READY);
250
251                 if (provider->driver && provider->driver->set_routes)
252                         provider->driver->set_routes(provider,
253                                                 CONNMAN_PROVIDER_ROUTE_ALL);
254
255         } else {
256                 if (ipconfig) {
257                         provider_indicate_state(provider,
258                                         CONNMAN_SERVICE_STATE_DISCONNECT);
259                         __connman_ipconfig_gateway_remove(ipconfig);
260                 }
261
262                 provider_indicate_state(provider,
263                                         CONNMAN_SERVICE_STATE_IDLE);
264         }
265
266         return 0;
267 }
268
269 int connman_provider_set_state(struct connman_provider *provider,
270                                         enum connman_provider_state state)
271 {
272         if (!provider || !provider->vpn_service)
273                 return -EINVAL;
274
275         switch (state) {
276         case CONNMAN_PROVIDER_STATE_UNKNOWN:
277                 return -EINVAL;
278         case CONNMAN_PROVIDER_STATE_IDLE:
279                 return set_connected(provider, false);
280         case CONNMAN_PROVIDER_STATE_CONNECT:
281                 return provider_indicate_state(provider,
282                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
283         case CONNMAN_PROVIDER_STATE_READY:
284                 return set_connected(provider, true);
285         case CONNMAN_PROVIDER_STATE_DISCONNECT:
286                 return provider_indicate_state(provider,
287                                         CONNMAN_SERVICE_STATE_DISCONNECT);
288         case CONNMAN_PROVIDER_STATE_FAILURE:
289                 return provider_indicate_state(provider,
290                                         CONNMAN_SERVICE_STATE_FAILURE);
291         }
292
293         return -EINVAL;
294 }
295
296 int connman_provider_indicate_error(struct connman_provider *provider,
297                                         enum connman_provider_error error)
298 {
299         enum connman_service_error service_error;
300
301         switch (error) {
302         case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
303                 service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
304                 break;
305         case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
306                 service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
307                 break;
308         case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
309                 service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
310                 break;
311         default:
312                 service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
313                 break;
314         }
315
316         return __connman_service_indicate_error(provider->vpn_service,
317                                                         service_error);
318 }
319
320 int connman_provider_create_service(struct connman_provider *provider)
321 {
322         if (provider->vpn_service) {
323                 bool connected;
324
325                 connected = __connman_service_is_connected_state(
326                         provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV4);
327                 if (connected)
328                         return -EALREADY;
329
330                 connected = __connman_service_is_connected_state(
331                         provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV6);
332                 if (connected)
333                         return -EALREADY;
334
335                 return 0;
336         }
337
338         provider->vpn_service =
339                 __connman_service_create_from_provider(provider);
340
341         if (!provider->vpn_service) {
342                 connman_warn("service creation failed for provider %s",
343                         provider->identifier);
344
345                 g_hash_table_remove(provider_hash, provider->identifier);
346                 return -EOPNOTSUPP;
347         }
348
349         return 0;
350 }
351
352 bool __connman_provider_is_immutable(struct connman_provider *provider)
353
354 {
355         if (provider)
356                 return provider->immutable;
357
358         return false;
359 }
360
361 int connman_provider_set_immutable(struct connman_provider *provider,
362                                                 bool immutable)
363 {
364         if (!provider)
365                 return -EINVAL;
366
367         provider->immutable = immutable;
368
369         return 0;
370 }
371
372 static struct connman_provider *provider_lookup(const char *identifier)
373 {
374         return g_hash_table_lookup(provider_hash, identifier);
375 }
376
377 static void connection_ready(DBusMessage *msg, int error_code, void *user_data)
378 {
379         DBusMessage *reply;
380         const char *identifier = user_data;
381
382         DBG("msg %p error %d", msg, error_code);
383
384         if (error_code != 0) {
385                 reply = __connman_error_failed(msg, -error_code);
386                 if (!g_dbus_send_message(connection, reply))
387                         DBG("reply %p send failed", reply);
388         } else {
389                 const char *path;
390                 struct connman_provider *provider;
391
392                 provider = provider_lookup(identifier);
393                 if (!provider) {
394                         reply = __connman_error_failed(msg, EINVAL);
395                         g_dbus_send_message(connection, reply);
396                         return;
397                 }
398
399                 path = __connman_service_get_path(provider->vpn_service);
400
401                 g_dbus_send_reply(connection, msg,
402                                 DBUS_TYPE_OBJECT_PATH, &path,
403                                 DBUS_TYPE_INVALID);
404         }
405 }
406
407 int __connman_provider_create_and_connect(DBusMessage *msg)
408 {
409         struct connman_provider_driver *driver;
410
411         if (!driver_list)
412                 return -EINVAL;
413
414         driver = driver_list->data;
415         if (!driver || !driver->create)
416                 return -EINVAL;
417
418         DBG("msg %p", msg);
419
420         return driver->create(msg, connection_ready);
421 }
422
423 const char *__connman_provider_get_ident(struct connman_provider *provider)
424 {
425         if (!provider)
426                 return NULL;
427
428         return provider->identifier;
429 }
430
431 int connman_provider_set_string(struct connman_provider *provider,
432                                         const char *key, const char *value)
433 {
434         if (provider->driver && provider->driver->set_property)
435                 return provider->driver->set_property(provider, key, value);
436
437         return 0;
438 }
439
440 const char *connman_provider_get_string(struct connman_provider *provider,
441                                                         const char *key)
442 {
443         if (provider->driver && provider->driver->get_property)
444                 return provider->driver->get_property(provider, key);
445
446         return NULL;
447 }
448
449 bool
450 __connman_provider_check_routes(struct connman_provider *provider)
451 {
452         if (!provider)
453                 return false;
454
455         if (provider->driver && provider->driver->check_routes)
456                 return provider->driver->check_routes(provider);
457
458         return false;
459 }
460
461 void *connman_provider_get_data(struct connman_provider *provider)
462 {
463         return provider->driver_data;
464 }
465
466 void connman_provider_set_data(struct connman_provider *provider, void *data)
467 {
468         provider->driver_data = data;
469 }
470
471 void connman_provider_set_index(struct connman_provider *provider, int index)
472 {
473         struct connman_service *service = provider->vpn_service;
474         struct connman_ipconfig *ipconfig;
475
476         DBG("");
477
478         if (!service)
479                 return;
480
481         ipconfig = __connman_service_get_ip4config(service);
482
483         if (!ipconfig) {
484                 connman_service_create_ip4config(service, index);
485
486                 ipconfig = __connman_service_get_ip4config(service);
487                 if (!ipconfig) {
488                         DBG("Couldnt create ipconfig");
489                         goto done;
490                 }
491         }
492
493         __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
494         __connman_ipconfig_set_index(ipconfig, index);
495
496         ipconfig = __connman_service_get_ip6config(service);
497
498         if (!ipconfig) {
499                 connman_service_create_ip6config(service, index);
500
501                 ipconfig = __connman_service_get_ip6config(service);
502                 if (!ipconfig) {
503                         DBG("Couldnt create ipconfig for IPv6");
504                         goto done;
505                 }
506         }
507
508         __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
509         __connman_ipconfig_set_index(ipconfig, index);
510
511 done:
512         provider->index = index;
513 }
514
515 int connman_provider_get_index(struct connman_provider *provider)
516 {
517         return provider->index;
518 }
519
520 int connman_provider_set_ipaddress(struct connman_provider *provider,
521                                         struct connman_ipaddress *ipaddress)
522 {
523         struct connman_ipconfig *ipconfig = NULL;
524
525         ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
526                                                         ipaddress->family);
527         if (!ipconfig)
528                 return -EINVAL;
529
530         provider->family = ipaddress->family;
531
532         __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
533
534         __connman_ipconfig_set_local(ipconfig, ipaddress->local);
535         __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
536         __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
537         __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
538         __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
539
540         return 0;
541 }
542
543 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
544 {
545         DBG("provider %p pac %s", provider, pac);
546
547         __connman_service_set_pac(provider->vpn_service, pac);
548
549         return 0;
550 }
551
552
553 int connman_provider_set_domain(struct connman_provider *provider,
554                                         const char *domain)
555 {
556         DBG("provider %p domain %s", provider, domain);
557
558         __connman_service_set_domainname(provider->vpn_service, domain);
559
560         return 0;
561 }
562
563 int connman_provider_set_nameservers(struct connman_provider *provider,
564                                         char * const *nameservers)
565 {
566         int i;
567
568         DBG("provider %p nameservers %p", provider, nameservers);
569
570         __connman_service_nameserver_clear(provider->vpn_service);
571
572         if (!nameservers)
573                 return 0;
574
575         for (i = 0; nameservers[i]; i++)
576                 __connman_service_nameserver_append(provider->vpn_service,
577                                                 nameservers[i], false);
578
579         return 0;
580 }
581
582 static void unregister_provider(gpointer data)
583 {
584         struct connman_provider *provider = data;
585
586         DBG("provider %p service %p", provider, provider->vpn_service);
587
588         if (provider->vpn_service) {
589                 connman_service_unref(provider->vpn_service);
590                 provider->vpn_service = NULL;
591         }
592
593         connman_provider_unref(provider);
594 }
595
596 static gint compare_priority(gconstpointer a, gconstpointer b)
597 {
598         return 0;
599 }
600
601 int connman_provider_driver_register(struct connman_provider_driver *driver)
602 {
603         DBG("driver %p name %s", driver, driver->name);
604
605         driver_list = g_slist_insert_sorted(driver_list, driver,
606                                                         compare_priority);
607         return 0;
608 }
609
610 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
611 {
612         DBG("driver %p name %s", driver, driver->name);
613
614         driver_list = g_slist_remove(driver_list, driver);
615 }
616
617 void connman_provider_set_driver(struct connman_provider *provider,
618                                 struct connman_provider_driver *driver)
619 {
620         provider->driver = driver;
621 }
622
623 static void provider_disconnect_all(gpointer key, gpointer value,
624                                                 gpointer user_data)
625 {
626         struct connman_provider *provider = value;
627
628         connman_provider_disconnect(provider);
629 }
630
631 static void provider_offline_mode(bool enabled)
632 {
633         DBG("enabled %d", enabled);
634
635         if (enabled)
636                 g_hash_table_foreach(provider_hash, provider_disconnect_all,
637                                                                         NULL);
638
639 }
640
641 static void provider_initialize(struct connman_provider *provider)
642 {
643         DBG("provider %p", provider);
644
645         provider->index = 0;
646         provider->identifier = NULL;
647 }
648
649 static struct connman_provider *provider_new(void)
650 {
651         struct connman_provider *provider;
652
653         provider = g_try_new0(struct connman_provider, 1);
654         if (!provider)
655                 return NULL;
656
657         provider->refcount = 1;
658
659         DBG("provider %p", provider);
660         provider_initialize(provider);
661
662         return provider;
663 }
664
665 struct connman_provider *connman_provider_get(const char *identifier)
666 {
667         struct connman_provider *provider;
668
669         provider = g_hash_table_lookup(provider_hash, identifier);
670         if (provider)
671                 return provider;
672
673         provider = provider_new();
674         if (!provider)
675                 return NULL;
676
677         DBG("provider %p", provider);
678
679         provider->identifier = g_strdup(identifier);
680
681         g_hash_table_insert(provider_hash, provider->identifier, provider);
682
683         return provider;
684 }
685
686 void connman_provider_put(struct connman_provider *provider)
687 {
688         g_hash_table_remove(provider_hash, provider->identifier);
689 }
690
691 static struct connman_provider *provider_get(int index)
692 {
693         GHashTableIter iter;
694         gpointer value, key;
695
696         g_hash_table_iter_init(&iter, provider_hash);
697
698         while (g_hash_table_iter_next(&iter, &key, &value)) {
699                 struct connman_provider *provider = value;
700
701                 if (provider->index == index)
702                         return provider;
703         }
704
705         return NULL;
706 }
707
708 static void provider_service_changed(struct connman_service *service,
709                                 enum connman_service_state state)
710 {
711         struct connman_provider *provider;
712         int vpn_index, service_index;
713
714         if (!service)
715                 return;
716
717         switch (state) {
718         case CONNMAN_SERVICE_STATE_UNKNOWN:
719         case CONNMAN_SERVICE_STATE_IDLE:
720         case CONNMAN_SERVICE_STATE_ASSOCIATION:
721         case CONNMAN_SERVICE_STATE_CONFIGURATION:
722         case CONNMAN_SERVICE_STATE_READY:
723         case CONNMAN_SERVICE_STATE_ONLINE:
724                 return;
725         case CONNMAN_SERVICE_STATE_DISCONNECT:
726         case CONNMAN_SERVICE_STATE_FAILURE:
727                 break;
728         }
729
730         service_index = __connman_service_get_index(service);
731
732         vpn_index = __connman_connection_get_vpn_index(service_index);
733
734         DBG("service %p %s state %d index %d/%d", service,
735                 connman_service_get_identifier(service),
736                 state, service_index, vpn_index);
737
738         if (vpn_index < 0)
739                 return;
740
741         provider = provider_get(vpn_index);
742         if (!provider)
743                 return;
744
745         DBG("disconnect %p index %d", provider, vpn_index);
746
747         connman_provider_disconnect(provider);
748 }
749
750 static const struct connman_notifier provider_notifier = {
751         .name                   = "provider",
752         .offline_mode           = provider_offline_mode,
753         .service_state_changed  = provider_service_changed,
754 };
755
756 int __connman_provider_init(void)
757 {
758         int err;
759
760         DBG("");
761
762         connection = connman_dbus_get_connection();
763
764         provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
765                                                 NULL, unregister_provider);
766
767         err = connman_notifier_register(&provider_notifier);
768         if (err < 0) {
769                 g_hash_table_destroy(provider_hash);
770                 dbus_connection_unref(connection);
771         }
772
773         return err;
774 }
775
776 void __connman_provider_cleanup(void)
777 {
778         DBG("");
779
780         connman_notifier_unregister(&provider_notifier);
781
782         g_hash_table_destroy(provider_hash);
783         provider_hash = NULL;
784
785         dbus_connection_unref(connection);
786 }