service: Support automatically added nameservers
[framework/connectivity/connman.git] / src / service.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <netdb.h>
30 #include <gdbus.h>
31
32 #include <connman/storage.h>
33
34 #include "connman.h"
35
36 #define CONNECT_TIMEOUT         120
37
38 static DBusConnection *connection = NULL;
39
40 static GSequence *service_list = NULL;
41 static GHashTable *service_hash = NULL;
42 static GSList *counter_list = NULL;
43
44 struct connman_stats {
45         connman_bool_t valid;
46         connman_bool_t enabled;
47         struct connman_stats_data data_last;
48         struct connman_stats_data data;
49         GTimer *timer;
50 };
51
52 struct connman_stats_counter {
53         connman_bool_t append_all;
54         struct connman_stats stats;
55         struct connman_stats stats_roaming;
56 };
57
58 struct connman_service {
59         int refcount;
60         int session_usage_count;
61         char *identifier;
62         char *path;
63         enum connman_service_type type;
64         enum connman_service_security security;
65         enum connman_service_state state;
66         enum connman_service_state state_ipv4;
67         enum connman_service_state state_ipv6;
68         enum connman_service_error error;
69         connman_uint8_t strength;
70         connman_bool_t favorite;
71         connman_bool_t immutable;
72         connman_bool_t hidden;
73         connman_bool_t ignore;
74         connman_bool_t autoconnect;
75         connman_bool_t userconnect;
76         GTimeVal modified;
77         unsigned int order;
78         char *name;
79         char *passphrase;
80         char *agent_passphrase;
81         connman_bool_t roaming;
82         connman_bool_t login_required;
83         connman_bool_t network_created;
84         struct connman_ipconfig *ipconfig_ipv4;
85         struct connman_ipconfig *ipconfig_ipv6;
86         struct connman_network *network;
87         struct connman_provider *provider;
88         char **nameservers;
89         char **nameservers_config;
90         char **nameservers_auto;
91         char **domains;
92         char *domainname;
93         char **timeservers;
94         /* 802.1x settings from the config files */
95         char *eap;
96         char *identity;
97         char *agent_identity;
98         char *ca_cert_file;
99         char *client_cert_file;
100         char *private_key_file;
101         char *private_key_passphrase;
102         char *phase2;
103         DBusMessage *pending;
104         guint timeout;
105         struct connman_stats stats;
106         struct connman_stats stats_roaming;
107         GHashTable *counter_table;
108         enum connman_service_proxy_method proxy;
109         enum connman_service_proxy_method proxy_config;
110         char **proxies;
111         char **excludes;
112         char *pac;
113         connman_bool_t wps;
114 };
115
116 static void append_path(gpointer value, gpointer user_data)
117 {
118         struct connman_service *service = value;
119         DBusMessageIter *iter = user_data;
120
121         if (service->path == NULL || service->hidden == TRUE)
122                 return;
123
124         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
125                                                         &service->path);
126 }
127
128 void __connman_service_list(DBusMessageIter *iter, void *user_data)
129 {
130         if (service_list == NULL)
131                 return;
132
133         g_sequence_foreach(service_list, append_path, iter);
134 }
135
136 struct find_data {
137         const char *path;
138         struct connman_service *service;
139 };
140
141 static void compare_path(gpointer value, gpointer user_data)
142 {
143         struct connman_service *service = value;
144         struct find_data *data = user_data;
145
146         if (data->service != NULL)
147                 return;
148
149         if (g_strcmp0(service->path, data->path) == 0)
150                 data->service = service;
151 }
152
153 static struct connman_service *find_service(const char *path)
154 {
155         struct find_data data = { .path = path, .service = NULL };
156
157         DBG("path %s", path);
158
159         g_sequence_foreach(service_list, compare_path, &data);
160
161         return data.service;
162 }
163
164 const char *__connman_service_type2string(enum connman_service_type type)
165 {
166         switch (type) {
167         case CONNMAN_SERVICE_TYPE_UNKNOWN:
168                 break;
169         case CONNMAN_SERVICE_TYPE_SYSTEM:
170                 return "system";
171         case CONNMAN_SERVICE_TYPE_ETHERNET:
172                 return "ethernet";
173         case CONNMAN_SERVICE_TYPE_WIFI:
174                 return "wifi";
175         case CONNMAN_SERVICE_TYPE_WIMAX:
176                 return "wimax";
177         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
178                 return "bluetooth";
179         case CONNMAN_SERVICE_TYPE_CELLULAR:
180                 return "cellular";
181         case CONNMAN_SERVICE_TYPE_GPS:
182                 return "gps";
183         case CONNMAN_SERVICE_TYPE_VPN:
184                 return "vpn";
185         case CONNMAN_SERVICE_TYPE_GADGET:
186                 return "gadget";
187         }
188
189         return NULL;
190 }
191
192 static const char *security2string(enum connman_service_security security)
193 {
194         switch (security) {
195         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
196                 break;
197         case CONNMAN_SERVICE_SECURITY_NONE:
198                 return "none";
199         case CONNMAN_SERVICE_SECURITY_WEP:
200                 return "wep";
201         case CONNMAN_SERVICE_SECURITY_PSK:
202                 return "psk";
203         case CONNMAN_SERVICE_SECURITY_8021X:
204                 return "ieee8021x";
205         case CONNMAN_SERVICE_SECURITY_WPA:
206                 return "wpa";
207         case CONNMAN_SERVICE_SECURITY_RSN:
208                 return "rsn";
209         }
210
211         return NULL;
212 }
213
214 static const char *state2string(enum connman_service_state state)
215 {
216         switch (state) {
217         case CONNMAN_SERVICE_STATE_UNKNOWN:
218                 break;
219         case CONNMAN_SERVICE_STATE_IDLE:
220                 return "idle";
221         case CONNMAN_SERVICE_STATE_ASSOCIATION:
222                 return "association";
223         case CONNMAN_SERVICE_STATE_CONFIGURATION:
224                 return "configuration";
225         case CONNMAN_SERVICE_STATE_READY:
226                 return "ready";
227         case CONNMAN_SERVICE_STATE_ONLINE:
228                 return "online";
229         case CONNMAN_SERVICE_STATE_DISCONNECT:
230                 return "disconnect";
231         case CONNMAN_SERVICE_STATE_FAILURE:
232                 return "failure";
233         }
234
235         return NULL;
236 }
237
238 static const char *error2string(enum connman_service_error error)
239 {
240         switch (error) {
241         case CONNMAN_SERVICE_ERROR_UNKNOWN:
242                 break;
243         case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
244                 return "out-of-range";
245         case CONNMAN_SERVICE_ERROR_PIN_MISSING:
246                 return "pin-missing";
247         case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
248                 return "dhcp-failed";
249         case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
250                 return "connect-failed";
251         case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
252                 return "login-failed";
253         case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
254                 return "auth-failed";
255         case CONNMAN_SERVICE_ERROR_INVALID_KEY:
256                 return "invalid-key";
257         }
258
259         return NULL;
260 }
261
262 static enum connman_service_error string2error(const char *error)
263 {
264         if (g_strcmp0(error, "dhcp-failed") == 0)
265                 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
266         else if (g_strcmp0(error, "pin-missing") == 0)
267                 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
268         else if (g_strcmp0(error, "invalid-key") == 0)
269                 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
270
271         return CONNMAN_SERVICE_ERROR_UNKNOWN;
272 }
273
274 static const char *proxymethod2string(enum connman_service_proxy_method method)
275 {
276         switch (method) {
277         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
278                 return "direct";
279         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
280                 return "manual";
281         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
282                 return "auto";
283         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
284                 break;
285         }
286
287         return NULL;
288 }
289
290 static enum connman_service_proxy_method string2proxymethod(const char *method)
291 {
292         if (g_strcmp0(method, "direct") == 0)
293                 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
294         else if (g_strcmp0(method, "auto") == 0)
295                 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
296         else if (g_strcmp0(method, "manual") == 0)
297                 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
298         else
299                 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
300 }
301
302 static int service_load(struct connman_service *service)
303 {
304         GKeyFile *keyfile;
305         GError *error = NULL;
306         gsize length;
307         gchar *str;
308         connman_bool_t autoconnect;
309         unsigned int ssid_len;
310         int err = 0;
311
312         DBG("service %p", service);
313
314         keyfile = connman_storage_load_service(service->identifier);
315         if (keyfile == NULL)
316                 return -EIO;
317
318         switch (service->type) {
319         case CONNMAN_SERVICE_TYPE_UNKNOWN:
320         case CONNMAN_SERVICE_TYPE_SYSTEM:
321         case CONNMAN_SERVICE_TYPE_ETHERNET:
322         case CONNMAN_SERVICE_TYPE_GPS:
323         case CONNMAN_SERVICE_TYPE_VPN:
324         case CONNMAN_SERVICE_TYPE_GADGET:
325                 break;
326         case CONNMAN_SERVICE_TYPE_WIFI:
327                 if (service->name == NULL) {
328                         gchar *name;
329
330                         name = g_key_file_get_string(keyfile,
331                                         service->identifier, "Name", NULL);
332                         if (name != NULL) {
333                                 g_free(service->name);
334                                 service->name = name;
335                         }
336
337                         if (service->network != NULL)
338                                 connman_network_set_name(service->network,
339                                                                         name);
340                 }
341
342                 if (service->network &&
343                                 connman_network_get_blob(service->network,
344                                         "WiFi.SSID", &ssid_len) == NULL) {
345                         gchar *hex_ssid;
346
347                         hex_ssid = g_key_file_get_string(keyfile,
348                                                         service->identifier,
349                                                                 "SSID", NULL);
350
351                         if (hex_ssid != NULL) {
352                                 gchar *ssid;
353                                 unsigned int i, j = 0, hex;
354                                 size_t hex_ssid_len = strlen(hex_ssid);
355
356                                 ssid = g_try_malloc0(hex_ssid_len / 2);
357                                 if (ssid == NULL) {
358                                         g_free(hex_ssid);
359                                         err = -ENOMEM;
360                                         goto done;
361                                 }
362
363                                 for (i = 0; i < hex_ssid_len; i += 2) {
364                                         sscanf(hex_ssid + i, "%02x", &hex);
365                                         ssid[j++] = hex;
366                                 }
367
368                                 connman_network_set_blob(service->network,
369                                         "WiFi.SSID", ssid, hex_ssid_len / 2);
370                         }
371
372                         g_free(hex_ssid);
373                 }
374                 /* fall through */
375
376         case CONNMAN_SERVICE_TYPE_WIMAX:
377         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
378         case CONNMAN_SERVICE_TYPE_CELLULAR:
379                 service->favorite = g_key_file_get_boolean(keyfile,
380                                 service->identifier, "Favorite", NULL);
381
382                 autoconnect = g_key_file_get_boolean(keyfile,
383                                 service->identifier, "AutoConnect", &error);
384                 if (error == NULL)
385                         service->autoconnect = autoconnect;
386                 g_clear_error(&error);
387
388                 str = g_key_file_get_string(keyfile,
389                                 service->identifier, "Failure", NULL);
390                 if (str != NULL) {
391                         if (service->favorite == FALSE)
392                                 service->state_ipv4 = service->state_ipv6 =
393                                         CONNMAN_SERVICE_STATE_FAILURE;
394                         service->error = string2error(str);
395                         g_free(str);
396                 }
397                 break;
398         }
399
400         str = g_key_file_get_string(keyfile,
401                                 service->identifier, "Modified", NULL);
402         if (str != NULL) {
403                 g_time_val_from_iso8601(str, &service->modified);
404                 g_free(str);
405         }
406
407         str = g_key_file_get_string(keyfile,
408                                 service->identifier, "Passphrase", NULL);
409         if (str != NULL) {
410                 g_free(service->passphrase);
411                 service->passphrase = str;
412         }
413
414         if (service->ipconfig_ipv4 != NULL)
415                 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
416                                         service->identifier, "IPv4.");
417
418         if (service->ipconfig_ipv6 != NULL)
419                 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
420                                         service->identifier, "IPv6.");
421
422         service->nameservers_config = g_key_file_get_string_list(keyfile,
423                         service->identifier, "Nameservers", &length, NULL);
424         if (service->nameservers_config != NULL && length == 0) {
425                 g_strfreev(service->nameservers_config);
426                 service->nameservers_config = NULL;
427         }
428
429         service->domains = g_key_file_get_string_list(keyfile,
430                         service->identifier, "Domains", &length, NULL);
431         if (service->domains != NULL && length == 0) {
432                 g_strfreev(service->domains);
433                 service->domains = NULL;
434         }
435
436         str = g_key_file_get_string(keyfile,
437                                 service->identifier, "Proxy.Method", NULL);
438         if (str != NULL)
439                 service->proxy_config = string2proxymethod(str);
440
441         g_free(str);
442
443         service->proxies = g_key_file_get_string_list(keyfile,
444                         service->identifier, "Proxy.Servers", &length, NULL);
445         if (service->proxies != NULL && length == 0) {
446                 g_strfreev(service->proxies);
447                 service->proxies = NULL;
448         }
449
450         service->excludes = g_key_file_get_string_list(keyfile,
451                         service->identifier, "Proxy.Excludes", &length, NULL);
452         if (service->excludes != NULL && length == 0) {
453                 g_strfreev(service->excludes);
454                 service->excludes = NULL;
455         }
456
457         str = g_key_file_get_string(keyfile,
458                                 service->identifier, "Proxy.URL", NULL);
459         if (str != NULL) {
460                 g_free(service->pac);
461                 service->pac = str;
462         }
463
464 done:
465         g_key_file_free(keyfile);
466
467         return err;
468 }
469
470 static int service_save(struct connman_service *service)
471 {
472         GKeyFile *keyfile;
473         gchar *str;
474         guint freq;
475         const char *cst_str = NULL;
476         int err = 0;
477
478         DBG("service %p", service);
479
480         keyfile = __connman_storage_open_service(service->identifier);
481         if (keyfile == NULL)
482                 return -EIO;
483
484         if (service->name != NULL)
485                 g_key_file_set_string(keyfile, service->identifier,
486                                                 "Name", service->name);
487
488         switch (service->type) {
489         case CONNMAN_SERVICE_TYPE_UNKNOWN:
490         case CONNMAN_SERVICE_TYPE_SYSTEM:
491         case CONNMAN_SERVICE_TYPE_ETHERNET:
492         case CONNMAN_SERVICE_TYPE_GPS:
493         case CONNMAN_SERVICE_TYPE_VPN:
494         case CONNMAN_SERVICE_TYPE_GADGET:
495                 break;
496         case CONNMAN_SERVICE_TYPE_WIFI:
497                 if (service->network) {
498                         const unsigned char *ssid;
499                         unsigned int ssid_len = 0;
500
501                         ssid = connman_network_get_blob(service->network,
502                                                         "WiFi.SSID", &ssid_len);
503
504                         if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
505                                 char *identifier = service->identifier;
506                                 GString *str;
507                                 unsigned int i;
508
509                                 str = g_string_sized_new(ssid_len * 2);
510                                 if (str == NULL) {
511                                         err = -ENOMEM;
512                                         goto done;
513                                 }
514
515                                 for (i = 0; i < ssid_len; i++)
516                                         g_string_append_printf(str,
517                                                         "%02x", ssid[i]);
518
519                                 g_key_file_set_string(keyfile, identifier,
520                                                         "SSID", str->str);
521
522                                 g_string_free(str, TRUE);
523                         }
524
525                         freq = connman_network_get_frequency(service->network);
526                         g_key_file_set_integer(keyfile, service->identifier,
527                                                 "Frequency", freq);
528                 }
529                 /* fall through */
530
531         case CONNMAN_SERVICE_TYPE_WIMAX:
532         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
533         case CONNMAN_SERVICE_TYPE_CELLULAR:
534                 g_key_file_set_boolean(keyfile, service->identifier,
535                                         "Favorite", service->favorite);
536
537                 if (service->favorite == TRUE)
538                         g_key_file_set_boolean(keyfile, service->identifier,
539                                         "AutoConnect", service->autoconnect);
540
541                 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
542                         service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
543                         const char *failure = error2string(service->error);
544                         if (failure != NULL)
545                                 g_key_file_set_string(keyfile,
546                                                         service->identifier,
547                                                         "Failure", failure);
548                 } else {
549                         g_key_file_remove_key(keyfile, service->identifier,
550                                                         "Failure", NULL);
551                 }
552                 break;
553         }
554
555         str = g_time_val_to_iso8601(&service->modified);
556         if (str != NULL) {
557                 g_key_file_set_string(keyfile, service->identifier,
558                                                         "Modified", str);
559                 g_free(str);
560         }
561
562         if (service->passphrase != NULL && strlen(service->passphrase) > 0)
563                 g_key_file_set_string(keyfile, service->identifier,
564                                         "Passphrase", service->passphrase);
565         else
566                 g_key_file_remove_key(keyfile, service->identifier,
567                                                         "Passphrase", NULL);
568
569         if (service->ipconfig_ipv4 != NULL)
570                 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
571                                         service->identifier, "IPv4.");
572
573         if (service->ipconfig_ipv6 != NULL)
574                 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
575                                                 service->identifier, "IPv6.");
576
577         if (service->nameservers_config != NULL) {
578                 guint len = g_strv_length(service->nameservers_config);
579
580                 g_key_file_set_string_list(keyfile, service->identifier,
581                                                                 "Nameservers",
582                                 (const gchar **) service->nameservers_config, len);
583         } else
584         g_key_file_remove_key(keyfile, service->identifier,
585                                                         "Nameservers", NULL);
586
587         if (service->domains != NULL) {
588                 guint len = g_strv_length(service->domains);
589
590                 g_key_file_set_string_list(keyfile, service->identifier,
591                                                                 "Domains",
592                                 (const gchar **) service->domains, len);
593         } else
594                 g_key_file_remove_key(keyfile, service->identifier,
595                                                         "Domains", NULL);
596
597         cst_str = proxymethod2string(service->proxy_config);
598         if (cst_str != NULL)
599                 g_key_file_set_string(keyfile, service->identifier,
600                                 "Proxy.Method", cst_str);
601
602         if (service->proxies != NULL) {
603                 guint len = g_strv_length(service->proxies);
604
605                 g_key_file_set_string_list(keyfile, service->identifier,
606                                 "Proxy.Servers",
607                                 (const gchar **) service->proxies, len);
608         } else
609                 g_key_file_remove_key(keyfile, service->identifier,
610                                                 "Proxy.Servers", NULL);
611
612         if (service->excludes != NULL) {
613                 guint len = g_strv_length(service->excludes);
614
615                 g_key_file_set_string_list(keyfile, service->identifier,
616                                 "Proxy.Excludes",
617                                 (const gchar **) service->excludes, len);
618         } else
619                 g_key_file_remove_key(keyfile, service->identifier,
620                                                 "Proxy.Excludes", NULL);
621
622         if (service->pac != NULL && strlen(service->pac) > 0)
623                 g_key_file_set_string(keyfile, service->identifier,
624                                         "Proxy.URL", service->pac);
625         else
626                 g_key_file_remove_key(keyfile, service->identifier,
627                                                         "Proxy.URL", NULL);
628
629 done:
630         __connman_storage_save_service(keyfile, service->identifier);
631
632         g_key_file_free(keyfile);
633
634         return err;
635 }
636
637 static guint changed_timeout = 0;
638
639 static gboolean notify_services_changed(gpointer user_data)
640 {
641         changed_timeout = 0;
642
643         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
644                                 CONNMAN_MANAGER_INTERFACE, "Services",
645                                 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
646                                 NULL);
647
648         return FALSE;
649 }
650
651 static void services_changed(gboolean delayed)
652 {
653         DBG("");
654
655         if (changed_timeout > 0) {
656                 g_source_remove(changed_timeout);
657                 changed_timeout = 0;
658         }
659
660         if (__connman_connection_update_gateway() == TRUE) {
661                 notify_services_changed(NULL);
662                 return;
663         }
664
665         if (delayed == FALSE) {
666                 notify_services_changed(NULL);
667                 return;
668         }
669
670         changed_timeout = g_timeout_add_seconds(1, notify_services_changed,
671                                                                  NULL);
672 }
673
674 static enum connman_service_state combine_state(
675                                         enum connman_service_state state_a,
676                                         enum connman_service_state state_b)
677 {
678         enum connman_service_state result;
679
680         if (state_a == state_b) {
681                 result = state_a;
682                 goto done;
683         }
684
685         if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
686                 result = state_b;
687                 goto done;
688         }
689
690         if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
691                 result = state_a;
692                 goto done;
693         }
694
695         if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
696                 result = state_b;
697                 goto done;
698         }
699
700         if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
701                 result = state_a;
702                 goto done;
703         }
704
705         if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
706                 result = state_a;
707                 goto done;
708         }
709
710         if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
711                 result = state_b;
712                 goto done;
713         }
714
715         if (state_a == CONNMAN_SERVICE_STATE_READY) {
716                 result = state_a;
717                 goto done;
718         }
719
720         if (state_b == CONNMAN_SERVICE_STATE_READY) {
721                 result = state_b;
722                 goto done;
723         }
724
725         if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
726                 result = state_a;
727                 goto done;
728         }
729
730         if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
731                 result = state_b;
732                 goto done;
733         }
734
735         if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
736                 result = state_a;
737                 goto done;
738         }
739
740         if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
741                 result = state_b;
742                 goto done;
743         }
744
745         if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
746                 result = state_a;
747                 goto done;
748         }
749
750         if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
751                 result = state_b;
752                 goto done;
753         }
754
755         result = CONNMAN_SERVICE_STATE_FAILURE;
756
757 done:
758         return result;
759 }
760
761 static connman_bool_t is_connecting_state(struct connman_service *service,
762                                         enum connman_service_state state)
763 {
764         switch (state) {
765         case CONNMAN_SERVICE_STATE_UNKNOWN:
766         case CONNMAN_SERVICE_STATE_IDLE:
767         case CONNMAN_SERVICE_STATE_FAILURE:
768                 if (service->network != NULL)
769                         return connman_network_get_connecting(service->network);
770         case CONNMAN_SERVICE_STATE_DISCONNECT:
771         case CONNMAN_SERVICE_STATE_READY:
772         case CONNMAN_SERVICE_STATE_ONLINE:
773                 break;
774         case CONNMAN_SERVICE_STATE_ASSOCIATION:
775         case CONNMAN_SERVICE_STATE_CONFIGURATION:
776                 return TRUE;
777         }
778
779         return FALSE;
780 }
781
782 static connman_bool_t is_connected_state(const struct connman_service *service,
783                                         enum connman_service_state state)
784 {
785         switch (state) {
786         case CONNMAN_SERVICE_STATE_UNKNOWN:
787         case CONNMAN_SERVICE_STATE_IDLE:
788         case CONNMAN_SERVICE_STATE_ASSOCIATION:
789         case CONNMAN_SERVICE_STATE_CONFIGURATION:
790         case CONNMAN_SERVICE_STATE_DISCONNECT:
791         case CONNMAN_SERVICE_STATE_FAILURE:
792                 break;
793         case CONNMAN_SERVICE_STATE_READY:
794         case CONNMAN_SERVICE_STATE_ONLINE:
795                 return TRUE;
796         }
797
798         return FALSE;
799 }
800
801 static connman_bool_t is_connecting(struct connman_service *service)
802 {
803         return is_connecting_state(service, service->state);
804 }
805
806 static connman_bool_t is_connected(struct connman_service *service)
807 {
808         return is_connected_state(service, service->state);
809 }
810
811 static void update_nameservers(struct connman_service *service)
812 {
813         const char *ifname;
814
815         if (service->ipconfig_ipv4)
816                 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
817         else if (service->ipconfig_ipv6)
818                 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
819         else
820                 ifname = NULL;
821
822         if (ifname == NULL)
823                 return;
824
825         switch (combine_state(service->state_ipv4, service->state_ipv6)) {
826         case CONNMAN_SERVICE_STATE_UNKNOWN:
827         case CONNMAN_SERVICE_STATE_IDLE:
828         case CONNMAN_SERVICE_STATE_ASSOCIATION:
829         case CONNMAN_SERVICE_STATE_CONFIGURATION:
830                 return;
831         case CONNMAN_SERVICE_STATE_FAILURE:
832         case CONNMAN_SERVICE_STATE_DISCONNECT:
833                 connman_resolver_remove_all(ifname);
834                 return;
835         case CONNMAN_SERVICE_STATE_READY:
836         case CONNMAN_SERVICE_STATE_ONLINE:
837                 break;
838         }
839
840         if (service->nameservers_config != NULL) {
841                 int i;
842
843                 for (i = 0; service->nameservers_config[i] != NULL; i++) {
844                         connman_resolver_append(ifname, NULL,
845                                                 service->nameservers_config[i]);
846                 }
847         } else if (service->nameservers != NULL) {
848                 int i;
849
850                 for (i = 0; service->nameservers[i] != NULL; i++) {
851                         connman_resolver_append(ifname, NULL,
852                                                 service->nameservers[i]);
853                 }
854         }
855
856         if (service->domains != NULL) {
857                 int i;
858
859                 for (i = 0; service->domains[i]; i++)
860                         connman_resolver_append(ifname, service->domains[i],
861                                                 NULL);
862         } else if (service->domainname != NULL)
863                 connman_resolver_append(ifname, service->domainname, NULL);
864
865         connman_resolver_flush();
866 }
867
868 /*
869  * The is_auto variable is set to true when IPv6 autoconf nameservers are
870  * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
871  * for details) and not through service.c
872  */
873 int __connman_service_nameserver_append(struct connman_service *service,
874                                 const char *nameserver, gboolean is_auto)
875 {
876         char **nameservers;
877         int len, i;
878
879         DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
880
881         if (nameserver == NULL)
882                 return -EINVAL;
883
884         if (is_auto == TRUE)
885                 nameservers = service->nameservers_auto;
886         else
887                 nameservers = service->nameservers;
888
889         for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
890                 if (g_strcmp0(nameservers[i], nameserver) == 0)
891                         return -EEXIST;
892
893         if (nameservers != NULL) {
894                 len = g_strv_length(nameservers);
895                 nameservers = g_try_renew(char *, nameservers, len + 2);
896         } else {
897                 len = 0;
898                 nameservers = g_try_new0(char *, len + 2);
899         }
900
901         if (nameservers == NULL)
902                 return -ENOMEM;
903
904         nameservers[len] = g_strdup(nameserver);
905         if (nameservers[len] == NULL)
906                 return -ENOMEM;
907
908         nameservers[len + 1] = NULL;
909
910         if (is_auto == TRUE) {
911                 service->nameservers_auto = nameservers;
912         } else {
913                 service->nameservers = nameservers;
914                 update_nameservers(service);
915         }
916
917         return 0;
918 }
919
920 int __connman_service_nameserver_remove(struct connman_service *service,
921                                 const char *nameserver, gboolean is_auto)
922 {
923         char **servers, **nameservers;
924         gboolean found = FALSE;
925         int len, i, j;
926
927         DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
928
929         if (nameserver == NULL)
930                 return -EINVAL;
931
932         if (is_auto == TRUE)
933                 nameservers = service->nameservers_auto;
934         else
935                 nameservers = service->nameservers;
936
937         if (nameservers == NULL)
938                 return 0;
939
940         for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
941                 if (g_strcmp0(nameservers[i], nameserver) == 0) {
942                         found = TRUE;
943                         break;
944                 }
945
946         if (found == FALSE)
947                 return 0;
948
949         len = g_strv_length(nameservers);
950
951         if (len == 1) {
952                 g_strfreev(nameservers);
953                 if (is_auto == TRUE)
954                         service->nameservers_auto = NULL;
955                 else
956                         service->nameservers = NULL;
957
958                 return 0;
959         }
960
961         servers = g_try_new0(char *, len);
962         if (servers == NULL)
963                 return -ENOMEM;
964
965         for (i = 0, j = 0; i < len; i++) {
966                 if (g_strcmp0(nameservers[i], nameserver) != 0) {
967                         servers[j] = g_strdup(nameservers[i]);
968                         if (servers[j] == NULL)
969                                 return -ENOMEM;
970                         j++;
971                 }
972         }
973         servers[len - 1] = NULL;
974
975         g_strfreev(nameservers);
976         nameservers = servers;
977
978         if (is_auto == TRUE) {
979                 service->nameservers_auto = nameservers;
980         } else {
981                 service->nameservers = nameservers;
982                 update_nameservers(service);
983         }
984
985         return 0;
986 }
987
988 void __connman_service_nameserver_clear(struct connman_service *service)
989 {
990         g_strfreev(service->nameservers);
991         service->nameservers = NULL;
992
993         update_nameservers(service);
994 }
995
996 static void nameserver_add_routes(int index, char **nameservers,
997                                         const char *gw)
998 {
999         int i, ret, family;
1000         struct addrinfo hints;
1001         struct addrinfo *addr;
1002
1003         for (i = 0; nameservers[i] != NULL; i++) {
1004                 memset(&hints, 0, sizeof(struct addrinfo));
1005                 hints.ai_flags = AI_NUMERICHOST;
1006                 addr = NULL;
1007
1008                 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1009                 if (ret == EAI_NONAME)
1010                         family = AF_INET; /* use the IPv4 as a default */
1011                 else if (ret != 0)
1012                         continue;
1013                 else
1014                         family = addr->ai_family;
1015
1016                 if (family == AF_INET) {
1017                         if (connman_inet_compare_subnet(index,
1018                                                 nameservers[i]) != TRUE)
1019                                 connman_inet_add_host_route(index,
1020                                                         nameservers[i], gw);
1021                 } else if (family == AF_INET6)
1022                         connman_inet_add_ipv6_host_route(index,
1023                                                         nameservers[i], gw);
1024
1025                 freeaddrinfo(addr);
1026         }
1027 }
1028
1029 static void nameserver_del_routes(int index, char **nameservers)
1030 {
1031         int i, ret, family;
1032         struct addrinfo hints;
1033         struct addrinfo *addr;
1034
1035         for (i = 0; nameservers[i] != NULL; i++) {
1036                 memset(&hints, 0, sizeof(struct addrinfo));
1037                 hints.ai_flags = AI_NUMERICHOST;
1038                 addr = NULL;
1039
1040                 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1041                 if (ret == EAI_NONAME)
1042                         family = AF_INET; /* use the IPv4 as a default */
1043                 else if (ret != 0)
1044                         continue;
1045                 else
1046                         family = addr->ai_family;
1047
1048                 if (family == AF_INET)
1049                         connman_inet_del_host_route(index, nameservers[i]);
1050                 else if (family == AF_INET6)
1051                         connman_inet_del_ipv6_host_route(index,
1052                                                         nameservers[i]);
1053
1054                 freeaddrinfo(addr);
1055         }
1056 }
1057
1058 void __connman_service_nameserver_add_routes(struct connman_service *service,
1059                                                 const char *gw)
1060 {
1061         int index = -1;
1062
1063         if (service == NULL)
1064                 return;
1065
1066         if (service->network != NULL)
1067                 index = connman_network_get_index(service->network);
1068         else if (service->provider != NULL)
1069                 index = connman_provider_get_index(service->provider);
1070
1071         if (service->nameservers_config != NULL) {
1072                 /*
1073                  * Configured nameserver takes preference over the
1074                  * discoverd nameserver gathered from DHCP, VPN, etc.
1075                  */
1076                 nameserver_add_routes(index, service->nameservers_config, gw);
1077         } else if (service->nameservers != NULL) {
1078                 /*
1079                  * We add nameservers host routes for nameservers that
1080                  * are not on our subnet. For those who are, the subnet
1081                  * route will be installed by the time the dns proxy code
1082                  * tries to reach them. The subnet route is installed
1083                  * when setting the interface IP address.
1084                  */
1085                 nameserver_add_routes(index, service->nameservers, gw);
1086         }
1087 }
1088
1089 void __connman_service_nameserver_del_routes(struct connman_service *service)
1090 {
1091         int index = -1;
1092
1093         if (service == NULL)
1094                 return;
1095
1096         if (service->network != NULL)
1097                 index = connman_network_get_index(service->network);
1098         else if (service->provider != NULL)
1099                 index = connman_provider_get_index(service->provider);
1100
1101         if (service->nameservers_config != NULL)
1102                 nameserver_del_routes(index, service->nameservers_config);
1103         else if (service->nameservers != NULL)
1104                 nameserver_del_routes(index, service->nameservers);
1105 }
1106
1107 static struct connman_stats *stats_get(struct connman_service *service)
1108 {
1109         if (service->roaming == TRUE)
1110                 return &service->stats_roaming;
1111         else
1112                 return &service->stats;
1113 }
1114
1115 static connman_bool_t stats_enabled(struct connman_service *service)
1116 {
1117         struct connman_stats *stats = stats_get(service);
1118
1119         return stats->enabled;
1120 }
1121
1122 static void stats_start(struct connman_service *service)
1123 {
1124         struct connman_stats *stats = stats_get(service);
1125
1126         DBG("service %p", service);
1127
1128         if (stats->timer == NULL)
1129                 return;
1130
1131         stats->enabled = TRUE;
1132         stats->data_last.time = stats->data.time;
1133
1134         g_timer_start(stats->timer);
1135 }
1136
1137 static void stats_stop(struct connman_service *service)
1138 {
1139         struct connman_stats *stats = stats_get(service);
1140         unsigned int seconds;
1141
1142         DBG("service %p", service);
1143
1144         if (stats->timer == NULL)
1145                 return;
1146
1147         if (stats->enabled == FALSE)
1148                 return;
1149
1150         g_timer_stop(stats->timer);
1151
1152         seconds = g_timer_elapsed(stats->timer, NULL);
1153         stats->data.time = stats->data_last.time + seconds;
1154
1155         stats->enabled = FALSE;
1156 }
1157
1158 static void reset_stats(struct connman_service *service)
1159 {
1160         DBG("service %p", service);
1161
1162         /* home */
1163         service->stats.valid = FALSE;
1164
1165         service->stats.data.rx_packets = 0;
1166         service->stats.data.tx_packets = 0;
1167         service->stats.data.rx_bytes = 0;
1168         service->stats.data.tx_bytes = 0;
1169         service->stats.data.rx_errors = 0;
1170         service->stats.data.tx_errors = 0;
1171         service->stats.data.rx_dropped = 0;
1172         service->stats.data.tx_dropped = 0;
1173         service->stats.data.time = 0;
1174         service->stats.data_last.time = 0;
1175
1176         g_timer_reset(service->stats.timer);
1177
1178         /* roaming */
1179         service->stats_roaming.valid = FALSE;
1180
1181         service->stats_roaming.data.rx_packets = 0;
1182         service->stats_roaming.data.tx_packets = 0;
1183         service->stats_roaming.data.rx_bytes = 0;
1184         service->stats_roaming.data.tx_bytes = 0;
1185         service->stats_roaming.data.rx_errors = 0;
1186         service->stats_roaming.data.tx_errors = 0;
1187         service->stats_roaming.data.rx_dropped = 0;
1188         service->stats_roaming.data.tx_dropped = 0;
1189         service->stats_roaming.data.time = 0;
1190         service->stats_roaming.data_last.time = 0;
1191
1192         g_timer_reset(service->stats_roaming.timer);
1193 }
1194
1195 static struct connman_service *get_default(void)
1196 {
1197         struct connman_service *service;
1198         GSequenceIter *iter;
1199
1200         iter = g_sequence_get_begin_iter(service_list);
1201
1202         if (g_sequence_iter_is_end(iter) == TRUE)
1203                 return NULL;
1204
1205         service = g_sequence_get(iter);
1206
1207         if (is_connected(service) == FALSE)
1208                 return NULL;
1209
1210         return service;
1211 }
1212
1213 static void default_changed(void)
1214 {
1215         struct connman_service *service = get_default();
1216
1217         __connman_notifier_default_changed(service);
1218 }
1219
1220 const char *__connman_service_default(void)
1221 {
1222         struct connman_service *service;
1223
1224         service = get_default();
1225         if (service == NULL)
1226                 return "";
1227
1228         return __connman_service_type2string(service->type);
1229 }
1230
1231 static void state_changed(struct connman_service *service)
1232 {
1233         const char *str;
1234
1235         __connman_notifier_service_state_changed(service, service->state);
1236
1237         str = state2string(service->state);
1238         if (str == NULL)
1239                 return;
1240
1241         connman_dbus_property_changed_basic(service->path,
1242                                 CONNMAN_SERVICE_INTERFACE, "State",
1243                                                 DBUS_TYPE_STRING, &str);
1244 }
1245
1246 static void strength_changed(struct connman_service *service)
1247 {
1248         if (service->strength == 0)
1249                 return;
1250
1251         connman_dbus_property_changed_basic(service->path,
1252                                 CONNMAN_SERVICE_INTERFACE, "Strength",
1253                                         DBUS_TYPE_BYTE, &service->strength);
1254 }
1255
1256 static void favorite_changed(struct connman_service *service)
1257 {
1258         if (service->path == NULL)
1259                 return;
1260
1261         connman_dbus_property_changed_basic(service->path,
1262                                 CONNMAN_SERVICE_INTERFACE, "Favorite",
1263                                         DBUS_TYPE_BOOLEAN, &service->favorite);
1264 }
1265
1266 static void immutable_changed(struct connman_service *service)
1267 {
1268         if (service->path == NULL)
1269                 return;
1270
1271         connman_dbus_property_changed_basic(service->path,
1272                                 CONNMAN_SERVICE_INTERFACE, "Immutable",
1273                                         DBUS_TYPE_BOOLEAN, &service->immutable);
1274 }
1275
1276 static void roaming_changed(struct connman_service *service)
1277 {
1278         if (service->path == NULL)
1279                 return;
1280
1281         connman_dbus_property_changed_basic(service->path,
1282                                 CONNMAN_SERVICE_INTERFACE, "Roaming",
1283                                         DBUS_TYPE_BOOLEAN, &service->roaming);
1284 }
1285
1286 static void autoconnect_changed(struct connman_service *service)
1287 {
1288         if (service->path == NULL)
1289                 return;
1290
1291         connman_dbus_property_changed_basic(service->path,
1292                                 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1293                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1294 }
1295
1296 static void passphrase_changed(struct connman_service *service)
1297 {
1298         dbus_bool_t required;
1299
1300         switch (service->type) {
1301         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1302         case CONNMAN_SERVICE_TYPE_SYSTEM:
1303         case CONNMAN_SERVICE_TYPE_ETHERNET:
1304         case CONNMAN_SERVICE_TYPE_WIMAX:
1305         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1306         case CONNMAN_SERVICE_TYPE_CELLULAR:
1307         case CONNMAN_SERVICE_TYPE_GPS:
1308         case CONNMAN_SERVICE_TYPE_VPN:
1309         case CONNMAN_SERVICE_TYPE_GADGET:
1310                 return;
1311         case CONNMAN_SERVICE_TYPE_WIFI:
1312                 required = FALSE;
1313
1314                 switch (service->security) {
1315                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1316                 case CONNMAN_SERVICE_SECURITY_NONE:
1317                         break;
1318                 case CONNMAN_SERVICE_SECURITY_WEP:
1319                 case CONNMAN_SERVICE_SECURITY_PSK:
1320                 case CONNMAN_SERVICE_SECURITY_WPA:
1321                 case CONNMAN_SERVICE_SECURITY_RSN:
1322                         if (service->passphrase == NULL)
1323                                 required = TRUE;
1324                         break;
1325                 case CONNMAN_SERVICE_SECURITY_8021X:
1326                         break;
1327                 }
1328                 break;
1329         }
1330
1331         connman_dbus_property_changed_basic(service->path,
1332                                 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
1333                                                 DBUS_TYPE_BOOLEAN, &required);
1334 }
1335
1336 static void login_changed(struct connman_service *service)
1337 {
1338         dbus_bool_t required = service->login_required;
1339
1340         if (service->path == NULL)
1341                 return;
1342
1343         connman_dbus_property_changed_basic(service->path,
1344                                 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
1345                                                 DBUS_TYPE_BOOLEAN, &required);
1346 }
1347
1348 static void append_security(DBusMessageIter *iter, void *user_data)
1349 {
1350         struct connman_service *service = user_data;
1351         const char *str;
1352
1353         str = security2string(service->security);
1354         if (str != NULL)
1355                 dbus_message_iter_append_basic(iter,
1356                                 DBUS_TYPE_STRING, &str);
1357
1358         /*
1359          * Some access points incorrectly advertise WPS even when they
1360          * are configured as open or no security, so filter
1361          * appropriately.
1362          */
1363         if (service->wps == TRUE) {
1364                 switch (service->security) {
1365                 case CONNMAN_SERVICE_SECURITY_PSK:
1366                 case CONNMAN_SERVICE_SECURITY_WPA:
1367                 case CONNMAN_SERVICE_SECURITY_RSN:
1368                         str = "wps";
1369                         dbus_message_iter_append_basic(iter,
1370                                 DBUS_TYPE_STRING, &str);
1371                         break;
1372                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1373                 case CONNMAN_SERVICE_SECURITY_NONE:
1374                 case CONNMAN_SERVICE_SECURITY_WEP:
1375                 case CONNMAN_SERVICE_SECURITY_8021X:
1376                         break;
1377                 }
1378         }
1379 }
1380
1381 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1382 {
1383         struct connman_service *service = user_data;
1384
1385         if (service->ipconfig_ipv4 != NULL)
1386                 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1387                                                                         iter);
1388         else if (service->ipconfig_ipv6 != NULL)
1389                 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1390                                                                         iter);
1391 }
1392
1393 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1394 {
1395         struct connman_service *service = user_data;
1396
1397         DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1398                                 state2string(service->state_ipv4));
1399
1400         if (is_connected_state(service, service->state_ipv4) == FALSE)
1401                 return;
1402
1403         if (service->ipconfig_ipv4 != NULL)
1404                 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1405 }
1406
1407 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1408 {
1409         struct connman_service *service = user_data;
1410
1411         DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1412                                 state2string(service->state_ipv6));
1413
1414         if (is_connected_state(service, service->state_ipv6) == FALSE)
1415                 return;
1416
1417         if (service->ipconfig_ipv6 != NULL)
1418                 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1419                                                 service->ipconfig_ipv4);
1420 }
1421
1422 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1423 {
1424         struct connman_service *service = user_data;
1425
1426         if (service->ipconfig_ipv4 != NULL)
1427                 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1428                                                         iter);
1429 }
1430
1431 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1432 {
1433         struct connman_service *service = user_data;
1434
1435         if (service->ipconfig_ipv6 != NULL)
1436                 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1437                                                         iter);
1438 }
1439
1440 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1441 {
1442         char **servers;
1443         int i;
1444
1445         servers = *nameservers;
1446
1447         for (i = 0; servers[i] != NULL; i++) {
1448                 dbus_message_iter_append_basic(iter,
1449                                         DBUS_TYPE_STRING, &servers[i]);
1450         }
1451 }
1452
1453 static void append_dns(DBusMessageIter *iter, void *user_data)
1454 {
1455         struct connman_service *service = user_data;
1456
1457         if (is_connected(service) == FALSE)
1458                 return;
1459
1460         if (service->nameservers_config != NULL) {
1461                 append_nameserver(iter, &service->nameservers_config);
1462                 return;
1463         } else {
1464                 if (service->nameservers != NULL)
1465                         append_nameserver(iter, &service->nameservers);
1466
1467                 if (service->nameservers_auto != NULL)
1468                         append_nameserver(iter, &service->nameservers_auto);
1469         }
1470 }
1471
1472 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1473 {
1474         struct connman_service *service = user_data;
1475         int i;
1476
1477         if (service->nameservers_config == NULL)
1478                 return;
1479
1480         for (i = 0; service->nameservers_config[i]; i++) {
1481                 dbus_message_iter_append_basic(iter,
1482                                 DBUS_TYPE_STRING,
1483                                 &service->nameservers_config[i]);
1484         }
1485 }
1486
1487 static void append_domain(DBusMessageIter *iter, void *user_data)
1488 {
1489         struct connman_service *service = user_data;
1490
1491         if (is_connected(service) == FALSE &&
1492                                 is_connecting(service) == FALSE)
1493                 return;
1494
1495         if (service->domainname == NULL)
1496                 return;
1497
1498         dbus_message_iter_append_basic(iter,
1499                                 DBUS_TYPE_STRING, &service->domainname);
1500 }
1501
1502 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1503 {
1504         struct connman_service *service = user_data;
1505         int i;
1506
1507         if (service->domains == NULL)
1508                 return;
1509
1510         for (i = 0; service->domains[i]; i++)
1511                 dbus_message_iter_append_basic(iter,
1512                                 DBUS_TYPE_STRING, &service->domains[i]);
1513 }
1514
1515 static void append_proxies(DBusMessageIter *iter, void *user_data)
1516 {
1517         struct connman_service *service = user_data;
1518         int i;
1519
1520         if (service->proxies == NULL)
1521                 return;
1522
1523         for (i = 0; service->proxies[i]; i++)
1524                 dbus_message_iter_append_basic(iter,
1525                                 DBUS_TYPE_STRING, &service->proxies[i]);
1526 }
1527
1528 static void append_excludes(DBusMessageIter *iter, void *user_data)
1529 {
1530         struct connman_service *service = user_data;
1531         int i;
1532
1533         if (service->excludes == NULL)
1534                 return;
1535
1536         for (i = 0; service->excludes[i]; i++)
1537                 dbus_message_iter_append_basic(iter,
1538                                 DBUS_TYPE_STRING, &service->excludes[i]);
1539 }
1540
1541 static void append_proxy(DBusMessageIter *iter, void *user_data)
1542 {
1543         struct connman_service *service = user_data;
1544         enum connman_service_proxy_method proxy;
1545         const char *pac = NULL;
1546         const char *method = proxymethod2string(
1547                 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1548
1549         DBG("");
1550
1551         if (is_connected(service) == FALSE)
1552                 return;
1553
1554         proxy = connman_service_get_proxy_method(service);
1555
1556         switch (proxy) {
1557         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1558                 return;
1559         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1560                 goto done;
1561         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1562                 connman_dbus_dict_append_array(iter, "Servers",
1563                                         DBUS_TYPE_STRING, append_proxies,
1564                                         service);
1565
1566                 connman_dbus_dict_append_array(iter, "Excludes",
1567                                         DBUS_TYPE_STRING, append_excludes,
1568                                         service);
1569                 break;
1570         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1571                 /* Maybe DHCP, or WPAD,  has provided an url for a pac file */
1572                 if (service->ipconfig_ipv4 != NULL)
1573                         pac = __connman_ipconfig_get_proxy_autoconfig(
1574                                 service->ipconfig_ipv4);
1575                 else if (service->ipconfig_ipv6 != NULL)
1576                         pac = __connman_ipconfig_get_proxy_autoconfig(
1577                                 service->ipconfig_ipv6);
1578
1579                 if (service->pac == NULL && pac == NULL)
1580                         goto done;
1581
1582                 if (service->pac != NULL)
1583                         pac = service->pac;
1584
1585                 connman_dbus_dict_append_basic(iter, "URL",
1586                                         DBUS_TYPE_STRING, &pac);
1587                 break;
1588         }
1589
1590         method = proxymethod2string(proxy);
1591
1592 done:
1593         connman_dbus_dict_append_basic(iter, "Method",
1594                                         DBUS_TYPE_STRING, &method);
1595 }
1596
1597 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1598 {
1599         struct connman_service *service = user_data;
1600         const char *method;
1601
1602         if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1603                 return;
1604
1605         switch (service->proxy_config) {
1606         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1607                 return;
1608         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1609                 break;
1610         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1611                 if (service->proxies != NULL)
1612                         connman_dbus_dict_append_array(iter, "Servers",
1613                                                 DBUS_TYPE_STRING,
1614                                                 append_proxies, service);
1615
1616                 if (service->excludes != NULL)
1617                         connman_dbus_dict_append_array(iter, "Excludes",
1618                                                 DBUS_TYPE_STRING,
1619                                                 append_excludes, service);
1620                 break;
1621         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1622                 if (service->pac != NULL)
1623                         connman_dbus_dict_append_basic(iter, "URL",
1624                                         DBUS_TYPE_STRING, &service->pac);
1625                 break;
1626         }
1627
1628         method = proxymethod2string(service->proxy_config);
1629
1630         connman_dbus_dict_append_basic(iter, "Method",
1631                                 DBUS_TYPE_STRING, &method);
1632 }
1633
1634 static void append_provider(DBusMessageIter *iter, void *user_data)
1635 {
1636         struct connman_service *service = user_data;
1637
1638         DBG("%p %p", service, service->provider);
1639
1640         if (is_connected(service) == FALSE)
1641                 return;
1642
1643         if (service->provider != NULL)
1644                 __connman_provider_append_properties(service->provider, iter);
1645 }
1646
1647
1648 static void settings_changed(struct connman_service *service,
1649                                 struct connman_ipconfig *ipconfig)
1650 {
1651         connman_dbus_property_changed_dict(service->path,
1652                                         CONNMAN_SERVICE_INTERFACE, "IPv4",
1653                                                         append_ipv4, service);
1654
1655         connman_dbus_property_changed_dict(service->path,
1656                                         CONNMAN_SERVICE_INTERFACE, "IPv6",
1657                                                         append_ipv6, service);
1658
1659         __connman_notifier_ipconfig_changed(service, ipconfig);
1660 }
1661
1662 static void ipv4_configuration_changed(struct connman_service *service)
1663 {
1664         connman_dbus_property_changed_dict(service->path,
1665                                         CONNMAN_SERVICE_INTERFACE,
1666                                                         "IPv4.Configuration",
1667                                                         append_ipv4config,
1668                                                         service);
1669 }
1670
1671 static void ipv6_configuration_changed(struct connman_service *service)
1672 {
1673         connman_dbus_property_changed_dict(service->path,
1674                                         CONNMAN_SERVICE_INTERFACE,
1675                                                         "IPv6.Configuration",
1676                                                         append_ipv6config,
1677                                                         service);
1678 }
1679
1680 static void dns_changed(struct connman_service *service)
1681 {
1682         connman_dbus_property_changed_array(service->path,
1683                                 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1684                                         DBUS_TYPE_STRING, append_dns, service);
1685 }
1686
1687 static void dns_configuration_changed(struct connman_service *service)
1688 {
1689         connman_dbus_property_changed_array(service->path,
1690                                 CONNMAN_SERVICE_INTERFACE,
1691                                 "Nameservers.Configuration",
1692                                 DBUS_TYPE_STRING, append_dnsconfig, service);
1693
1694         dns_changed(service);
1695 }
1696
1697 static void domain_changed(struct connman_service *service)
1698 {
1699         connman_dbus_property_changed_array(service->path,
1700                                 CONNMAN_SERVICE_INTERFACE, "Domains",
1701                                 DBUS_TYPE_STRING, append_domain, service);
1702 }
1703
1704 static void domain_configuration_changed(struct connman_service *service)
1705 {
1706         connman_dbus_property_changed_array(service->path,
1707                                 CONNMAN_SERVICE_INTERFACE,
1708                                 "Domains.Configuration",
1709                                 DBUS_TYPE_STRING, append_domainconfig, service);
1710 }
1711
1712 static void proxy_changed(struct connman_service *service)
1713 {
1714         connman_dbus_property_changed_dict(service->path,
1715                                         CONNMAN_SERVICE_INTERFACE, "Proxy",
1716                                                         append_proxy, service);
1717 }
1718
1719 static void proxy_configuration_changed(struct connman_service *service)
1720 {
1721         connman_dbus_property_changed_dict(service->path,
1722                         CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1723                                                 append_proxyconfig, service);
1724
1725         proxy_changed(service);
1726 }
1727
1728 static void link_changed(struct connman_service *service)
1729 {
1730         connman_dbus_property_changed_dict(service->path,
1731                                         CONNMAN_SERVICE_INTERFACE, "Ethernet",
1732                                                 append_ethernet, service);
1733 }
1734
1735 static void stats_append_counters(DBusMessageIter *dict,
1736                         struct connman_stats_data *stats,
1737                         struct connman_stats_data *counters,
1738                         connman_bool_t append_all)
1739 {
1740         if (counters->rx_packets != stats->rx_packets || append_all) {
1741                 counters->rx_packets = stats->rx_packets;
1742                 connman_dbus_dict_append_basic(dict, "RX.Packets",
1743                                         DBUS_TYPE_UINT32, &stats->rx_packets);
1744         }
1745
1746         if (counters->tx_packets != stats->tx_packets || append_all) {
1747                 counters->tx_packets = stats->tx_packets;
1748                 connman_dbus_dict_append_basic(dict, "TX.Packets",
1749                                         DBUS_TYPE_UINT32, &stats->tx_packets);
1750         }
1751
1752         if (counters->rx_bytes != stats->rx_bytes || append_all) {
1753                 counters->rx_bytes = stats->rx_bytes;
1754                 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1755                                         DBUS_TYPE_UINT32, &stats->rx_bytes);
1756         }
1757
1758         if (counters->tx_bytes != stats->tx_bytes || append_all) {
1759                 counters->tx_bytes = stats->tx_bytes;
1760                 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1761                                         DBUS_TYPE_UINT32, &stats->tx_bytes);
1762         }
1763
1764         if (counters->rx_errors != stats->rx_errors || append_all) {
1765                 counters->rx_errors = stats->rx_errors;
1766                 connman_dbus_dict_append_basic(dict, "RX.Errors",
1767                                         DBUS_TYPE_UINT32, &stats->rx_errors);
1768         }
1769
1770         if (counters->tx_errors != stats->tx_errors || append_all) {
1771                 counters->tx_errors = stats->tx_errors;
1772                 connman_dbus_dict_append_basic(dict, "TX.Errors",
1773                                         DBUS_TYPE_UINT32, &stats->tx_errors);
1774         }
1775
1776         if (counters->rx_dropped != stats->rx_dropped || append_all) {
1777                 counters->rx_dropped = stats->rx_dropped;
1778                 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1779                                         DBUS_TYPE_UINT32, &stats->rx_dropped);
1780         }
1781
1782         if (counters->tx_dropped != stats->tx_dropped || append_all) {
1783                 counters->tx_dropped = stats->tx_dropped;
1784                 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1785                                         DBUS_TYPE_UINT32, &stats->tx_dropped);
1786         }
1787
1788         if (counters->time != stats->time || append_all) {
1789                 counters->time = stats->time;
1790                 connman_dbus_dict_append_basic(dict, "Time",
1791                                         DBUS_TYPE_UINT32, &stats->time);
1792         }
1793 }
1794
1795 static void stats_append(struct connman_service *service,
1796                                 const char *counter,
1797                                 struct connman_stats_counter *counters,
1798                                 connman_bool_t append_all)
1799 {
1800         DBusMessageIter array, dict;
1801         DBusMessage *msg;
1802
1803         DBG("service %p counter %s", service, counter);
1804
1805         msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1806         if (msg == NULL)
1807                 return;
1808
1809         dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1810                                 &service->path, DBUS_TYPE_INVALID);
1811
1812         dbus_message_iter_init_append(msg, &array);
1813
1814         /* home counter */
1815         connman_dbus_dict_open(&array, &dict);
1816
1817         stats_append_counters(&dict, &service->stats.data,
1818                                 &counters->stats.data, append_all);
1819
1820         connman_dbus_dict_close(&array, &dict);
1821
1822         /* roaming counter */
1823         connman_dbus_dict_open(&array, &dict);
1824
1825         stats_append_counters(&dict, &service->stats_roaming.data,
1826                                 &counters->stats_roaming.data, append_all);
1827
1828         connman_dbus_dict_close(&array, &dict);
1829
1830         __connman_counter_send_usage(counter, msg);
1831 }
1832
1833 static void stats_update(struct connman_service *service,
1834                                 unsigned int rx_packets, unsigned int tx_packets,
1835                                 unsigned int rx_bytes, unsigned int tx_bytes,
1836                                 unsigned int rx_errors, unsigned int tx_errors,
1837                                 unsigned int rx_dropped, unsigned int tx_dropped)
1838 {
1839         struct connman_stats *stats = stats_get(service);
1840         struct connman_stats_data *data_last = &stats->data_last;
1841         struct connman_stats_data *data = &stats->data;
1842         unsigned int seconds;
1843
1844         DBG("service %p", service);
1845
1846         if (stats->valid == TRUE) {
1847                 data->rx_packets +=
1848                         rx_packets - data_last->rx_packets;
1849                 data->tx_packets +=
1850                         tx_packets - data_last->tx_packets;
1851                 data->rx_bytes +=
1852                         rx_bytes - data_last->rx_bytes;
1853                 data->tx_bytes +=
1854                         tx_bytes - data_last->tx_bytes;
1855                 data->rx_errors +=
1856                         rx_errors - data_last->rx_errors;
1857                 data->tx_errors +=
1858                         tx_errors - data_last->tx_errors;
1859                 data->rx_dropped +=
1860                         rx_dropped - data_last->rx_dropped;
1861                 data->tx_dropped +=
1862                         tx_dropped - data_last->tx_dropped;
1863         } else {
1864                 stats->valid = TRUE;
1865         }
1866
1867         data_last->rx_packets = rx_packets;
1868         data_last->tx_packets = tx_packets;
1869         data_last->rx_bytes = rx_bytes;
1870         data_last->tx_bytes = tx_bytes;
1871         data_last->rx_errors = rx_errors;
1872         data_last->tx_errors = tx_errors;
1873         data_last->rx_dropped = rx_dropped;
1874         data_last->tx_dropped = tx_dropped;
1875
1876         seconds = g_timer_elapsed(stats->timer, NULL);
1877         stats->data.time = stats->data_last.time + seconds;
1878 }
1879
1880 static char *wifi_build_group_name(const unsigned char *ssid,
1881                                                 unsigned int ssid_len,
1882                                                         const char *mode,
1883                                                         const char *security)
1884 {
1885         GString *str;
1886         unsigned int i;
1887
1888         /* the last 3 is for the 2 '_' and '\0' */
1889         str = g_string_sized_new((ssid_len * 2) + strlen(mode)
1890                                         + strlen(security) + 3);
1891         if (str == NULL)
1892                 return NULL;
1893
1894         for (i = 0; i < ssid_len; i++)
1895                 g_string_append_printf(str, "%02x", ssid[i]);
1896
1897         g_string_append_printf(str, "_%s_%s", mode, security);
1898
1899         return g_string_free(str, FALSE);
1900 }
1901
1902 void __connman_service_notify(struct connman_service *service,
1903                         unsigned int rx_packets, unsigned int tx_packets,
1904                         unsigned int rx_bytes, unsigned int tx_bytes,
1905                         unsigned int rx_errors, unsigned int tx_errors,
1906                         unsigned int rx_dropped, unsigned int tx_dropped)
1907 {
1908         GHashTableIter iter;
1909         gpointer key, value;
1910         const char *counter;
1911         struct connman_stats_counter *counters;
1912         struct connman_stats_data *data;
1913         int err;
1914
1915         if (service == NULL)
1916                 return;
1917
1918         if (is_connected(service) == FALSE)
1919                 return;
1920
1921         stats_update(service,
1922                 rx_packets, tx_packets,
1923                 rx_bytes, tx_bytes,
1924                 rx_errors, tx_errors,
1925                 rx_dropped, tx_dropped);
1926
1927         data = &stats_get(service)->data;
1928         err = __connman_stats_update(service, service->roaming, data);
1929         if (err < 0)
1930                 connman_error("Failed to store statistics for %s",
1931                                 service->identifier);
1932
1933         g_hash_table_iter_init(&iter, service->counter_table);
1934         while (g_hash_table_iter_next(&iter, &key, &value)) {
1935                 counter = key;
1936                 counters = value;
1937
1938                 stats_append(service, counter, counters, counters->append_all);
1939                 counters->append_all = FALSE;
1940         }
1941 }
1942
1943 int __connman_service_counter_register(const char *counter)
1944 {
1945         struct connman_service *service;
1946         GSequenceIter *iter;
1947         struct connman_stats_counter *counters;
1948
1949         DBG("counter %s", counter);
1950
1951         counter_list = g_slist_append(counter_list, (gpointer)counter);
1952
1953         iter = g_sequence_get_begin_iter(service_list);
1954
1955         while (g_sequence_iter_is_end(iter) == FALSE) {
1956                 service = g_sequence_get(iter);
1957
1958                 counters = g_try_new0(struct connman_stats_counter, 1);
1959                 if (counters == NULL)
1960                         return -ENOMEM;
1961
1962                 counters->append_all = TRUE;
1963
1964                 g_hash_table_replace(service->counter_table, (gpointer)counter,
1965                                         counters);
1966
1967                 iter = g_sequence_iter_next(iter);
1968         }
1969
1970         return 0;
1971 }
1972
1973 void __connman_service_counter_unregister(const char *counter)
1974 {
1975         struct connman_service *service;
1976         GSequenceIter *iter;
1977
1978         DBG("counter %s", counter);
1979
1980         iter = g_sequence_get_begin_iter(service_list);
1981
1982         while (g_sequence_iter_is_end(iter) == FALSE) {
1983                 service = g_sequence_get(iter);
1984
1985                 g_hash_table_remove(service->counter_table, counter);
1986
1987                 iter = g_sequence_iter_next(iter);
1988         }
1989
1990         counter_list = g_slist_remove(counter_list, counter);
1991 }
1992
1993 GSequence *__connman_service_get_list(struct connman_session *session,
1994                                 service_match_cb service_match,
1995                                 create_service_entry_cb create_service_entry,
1996                                 GDestroyNotify destroy_service_entry)
1997 {
1998         GSequence *list;
1999         GSequenceIter *iter;
2000         struct connman_service *service;
2001         struct service_entry *entry;
2002
2003         list = g_sequence_new(destroy_service_entry);
2004         if (list == NULL)
2005                 return NULL;
2006
2007         iter = g_sequence_get_begin_iter(service_list);
2008
2009         while (g_sequence_iter_is_end(iter) == FALSE) {
2010                 service = g_sequence_get(iter);
2011
2012                 if (service_match(session, service) == TRUE) {
2013                         entry = create_service_entry(service, service->name,
2014                                                         service->state);
2015                         if (entry == NULL)
2016                                 return list;
2017
2018                         g_sequence_append(list, entry);
2019                 }
2020
2021                 iter = g_sequence_iter_next(iter);
2022         }
2023
2024         return list;
2025 }
2026
2027 void __connman_service_session_inc(struct connman_service *service)
2028 {
2029         DBG("service %p ref count %d", service,
2030                 service->session_usage_count + 1);
2031
2032         __sync_fetch_and_add(&service->session_usage_count, 1);
2033 }
2034
2035 connman_bool_t __connman_service_session_dec(struct connman_service *service)
2036 {
2037         DBG("service %p ref count %d", service,
2038                 service->session_usage_count - 1);
2039
2040         if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
2041                 return FALSE;
2042
2043         return TRUE;
2044 }
2045
2046 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2047                                         struct connman_service *service)
2048 {
2049         dbus_bool_t required;
2050         const char *str;
2051
2052         str = __connman_service_type2string(service->type);
2053         if (str != NULL)
2054                 connman_dbus_dict_append_basic(dict, "Type",
2055                                                 DBUS_TYPE_STRING, &str);
2056
2057         connman_dbus_dict_append_array(dict, "Security",
2058                                 DBUS_TYPE_STRING, append_security, service);
2059
2060         str = state2string(service->state);
2061         if (str != NULL)
2062                 connman_dbus_dict_append_basic(dict, "State",
2063                                                 DBUS_TYPE_STRING, &str);
2064
2065         str = error2string(service->error);
2066         if (str != NULL)
2067                 connman_dbus_dict_append_basic(dict, "Error",
2068                                                 DBUS_TYPE_STRING, &str);
2069
2070         if (service->strength > 0)
2071                 connman_dbus_dict_append_basic(dict, "Strength",
2072                                         DBUS_TYPE_BYTE, &service->strength);
2073
2074         connman_dbus_dict_append_basic(dict, "Favorite",
2075                                         DBUS_TYPE_BOOLEAN, &service->favorite);
2076
2077         connman_dbus_dict_append_basic(dict, "Immutable",
2078                                         DBUS_TYPE_BOOLEAN, &service->immutable);
2079
2080         if (service->favorite == TRUE)
2081                 connman_dbus_dict_append_basic(dict, "AutoConnect",
2082                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2083         else
2084                 connman_dbus_dict_append_basic(dict, "AutoConnect",
2085                                         DBUS_TYPE_BOOLEAN, &service->favorite);
2086
2087         if (service->name != NULL)
2088                 connman_dbus_dict_append_basic(dict, "Name",
2089                                         DBUS_TYPE_STRING, &service->name);
2090
2091         connman_dbus_dict_append_basic(dict, "LoginRequired",
2092                                 DBUS_TYPE_BOOLEAN, &service->login_required);
2093
2094         switch (service->type) {
2095         case CONNMAN_SERVICE_TYPE_UNKNOWN:
2096         case CONNMAN_SERVICE_TYPE_SYSTEM:
2097         case CONNMAN_SERVICE_TYPE_GPS:
2098         case CONNMAN_SERVICE_TYPE_VPN:
2099         case CONNMAN_SERVICE_TYPE_GADGET:
2100                 break;
2101         case CONNMAN_SERVICE_TYPE_CELLULAR:
2102                 connman_dbus_dict_append_basic(dict, "Roaming",
2103                                         DBUS_TYPE_BOOLEAN, &service->roaming);
2104
2105                 connman_dbus_dict_append_dict(dict, "Ethernet",
2106                                                 append_ethernet, service);
2107                 break;
2108         case CONNMAN_SERVICE_TYPE_WIFI:
2109                 if (service->passphrase != NULL && limited == FALSE)
2110                         connman_dbus_dict_append_basic(dict, "Passphrase",
2111                                 DBUS_TYPE_STRING, &service->passphrase);
2112
2113                 required = FALSE;
2114
2115                 switch (service->security) {
2116                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2117                 case CONNMAN_SERVICE_SECURITY_NONE:
2118                         break;
2119                 case CONNMAN_SERVICE_SECURITY_WEP:
2120                 case CONNMAN_SERVICE_SECURITY_PSK:
2121                 case CONNMAN_SERVICE_SECURITY_WPA:
2122                 case CONNMAN_SERVICE_SECURITY_RSN:
2123                         if (service->passphrase == NULL)
2124                                 required = TRUE;
2125                         break;
2126                 case CONNMAN_SERVICE_SECURITY_8021X:
2127                         break;
2128                 }
2129
2130                 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
2131                                                 DBUS_TYPE_BOOLEAN, &required);
2132
2133                 /* fall through */
2134         case CONNMAN_SERVICE_TYPE_ETHERNET:
2135         case CONNMAN_SERVICE_TYPE_WIMAX:
2136         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2137                 connman_dbus_dict_append_dict(dict, "Ethernet",
2138                                                 append_ethernet, service);
2139                 break;
2140         }
2141
2142         connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2143
2144         connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2145                                                 append_ipv4config, service);
2146
2147         connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2148
2149         connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2150                                                 append_ipv6config, service);
2151
2152         connman_dbus_dict_append_array(dict, "Nameservers",
2153                                 DBUS_TYPE_STRING, append_dns, service);
2154
2155         connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2156                                 DBUS_TYPE_STRING, append_dnsconfig, service);
2157
2158         connman_dbus_dict_append_array(dict, "Domains",
2159                                 DBUS_TYPE_STRING, append_domain, service);
2160
2161         connman_dbus_dict_append_array(dict, "Domains.Configuration",
2162                                 DBUS_TYPE_STRING, append_domainconfig, service);
2163
2164         connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2165
2166         connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2167                                                 append_proxyconfig, service);
2168
2169         connman_dbus_dict_append_dict(dict, "Provider",
2170                                                 append_provider, service);
2171 }
2172
2173 static void append_struct(gpointer value, gpointer user_data)
2174 {
2175         struct connman_service *service = value;
2176         DBusMessageIter *iter = user_data;
2177         DBusMessageIter entry, dict;
2178
2179         if (service->path == NULL || service->hidden == TRUE)
2180                 return;
2181
2182         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2183
2184         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2185                                                         &service->path);
2186
2187         connman_dbus_dict_open(&entry, &dict);
2188         append_properties(&dict, TRUE, service);
2189         connman_dbus_dict_close(&entry, &dict);
2190
2191         dbus_message_iter_close_container(iter, &entry);
2192 }
2193
2194 void __connman_service_list_struct(DBusMessageIter *iter)
2195 {
2196         g_sequence_foreach(service_list, append_struct, iter);
2197 }
2198
2199 int __connman_service_get_index(struct connman_service *service)
2200 {
2201         if (service == NULL)
2202                 return -1;
2203
2204         if (service->network != NULL)
2205                 return connman_network_get_index(service->network);
2206         else if (service->provider != NULL)
2207                 return connman_provider_get_index(service->provider);
2208
2209         return -1;
2210 }
2211
2212 void __connman_service_set_domainname(struct connman_service *service,
2213                                                 const char *domainname)
2214 {
2215         if (service == NULL)
2216                 return;
2217
2218         g_free(service->domainname);
2219         service->domainname = g_strdup(domainname);
2220
2221         domain_changed(service);
2222 }
2223
2224 const char *connman_service_get_domainname(struct connman_service *service)
2225 {
2226         if (service == NULL)
2227                 return NULL;
2228
2229         if (service->domains != NULL)
2230                 return service->domains[0];
2231         else
2232                 return service->domainname;
2233 }
2234
2235 char **connman_service_get_nameservers(struct connman_service *service)
2236 {
2237         if (service == NULL)
2238                 return NULL;
2239
2240         if (service->nameservers_config != NULL)
2241                 return service->nameservers_config;
2242         else if (service->nameservers != NULL)
2243                 return service->nameservers;
2244
2245         return NULL;
2246 }
2247
2248 void connman_service_set_proxy_method(struct connman_service *service,
2249                                         enum connman_service_proxy_method method)
2250 {
2251         if (service == NULL)
2252                 return;
2253
2254         service->proxy = method;
2255
2256         proxy_changed(service);
2257
2258         if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2259                 __connman_notifier_proxy_changed(service);
2260 }
2261
2262 enum connman_service_proxy_method connman_service_get_proxy_method(
2263                                         struct connman_service *service)
2264 {
2265         if (service == NULL)
2266                 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2267
2268         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2269                 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2270                                 service->pac == NULL)
2271                         return service->proxy;
2272
2273                 return service->proxy_config;
2274         }
2275
2276         return service->proxy;
2277 }
2278
2279 char **connman_service_get_proxy_servers(struct connman_service *service)
2280 {
2281         return g_strdupv(service->proxies);
2282 }
2283
2284 char **connman_service_get_proxy_excludes(struct connman_service *service)
2285 {
2286         return g_strdupv(service->excludes);
2287 }
2288
2289 const char *connman_service_get_proxy_url(struct connman_service *service)
2290 {
2291         if (service == NULL)
2292                 return NULL;
2293
2294         return service->pac;
2295 }
2296
2297 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2298                                                         const char *url)
2299 {
2300         if (service == NULL)
2301                 return;
2302
2303         service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2304
2305         if (service->ipconfig_ipv4) {
2306                 if (__connman_ipconfig_set_proxy_autoconfig(
2307                             service->ipconfig_ipv4, url) < 0)
2308                         return;
2309         } else if (service->ipconfig_ipv6) {
2310                 if (__connman_ipconfig_set_proxy_autoconfig(
2311                             service->ipconfig_ipv6, url) < 0)
2312                         return;
2313         } else
2314                 return;
2315
2316         proxy_changed(service);
2317
2318         __connman_notifier_proxy_changed(service);
2319 }
2320
2321 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2322 {
2323         if (service == NULL)
2324                 return NULL;
2325
2326         if (service->ipconfig_ipv4)
2327                 return __connman_ipconfig_get_proxy_autoconfig(
2328                                                 service->ipconfig_ipv4);
2329         else if (service->ipconfig_ipv6)
2330                 return __connman_ipconfig_get_proxy_autoconfig(
2331                                                 service->ipconfig_ipv6);
2332         return NULL;
2333 }
2334
2335 static void update_timeservers(struct connman_service *service)
2336 {
2337         int i;
2338
2339         if (service->timeservers == NULL)
2340                 return;
2341
2342         switch (service->state) {
2343         case CONNMAN_SERVICE_STATE_UNKNOWN:
2344         case CONNMAN_SERVICE_STATE_IDLE:
2345         case CONNMAN_SERVICE_STATE_ASSOCIATION:
2346         case CONNMAN_SERVICE_STATE_CONFIGURATION:
2347                 return;
2348         case CONNMAN_SERVICE_STATE_FAILURE:
2349         case CONNMAN_SERVICE_STATE_DISCONNECT:
2350                 for (i = 0; service->timeservers[i] != NULL; i++)
2351                         connman_timeserver_remove(service->timeservers[i]);
2352                 return;
2353         case CONNMAN_SERVICE_STATE_READY:
2354         case CONNMAN_SERVICE_STATE_ONLINE:
2355                 break;
2356         }
2357
2358         for (i = 0; service->timeservers[i] != NULL; i++)
2359                 connman_timeserver_append(service->timeservers[i]);
2360 }
2361
2362 int __connman_service_timeserver_append(struct connman_service *service,
2363                                                 const char *timeserver)
2364 {
2365         int len;
2366
2367         DBG("service %p timeserver %s", service, timeserver);
2368
2369         if (timeserver == NULL)
2370                 return -EINVAL;
2371
2372         if (service->timeservers != NULL) {
2373                 int i;
2374
2375                 for (i = 0; service->timeservers[i] != NULL; i++)
2376                         if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2377                                 return -EEXIST;
2378
2379                 len = g_strv_length(service->timeservers);
2380                 service->timeservers = g_try_renew(char *, service->timeservers,
2381                                                         len + 2);
2382         } else {
2383                 len = 0;
2384                 service->timeservers = g_try_new0(char *, len + 2);
2385         }
2386
2387         if (service->timeservers == NULL)
2388                 return -ENOMEM;
2389
2390         service->timeservers[len] = g_strdup(timeserver);
2391         service->timeservers[len + 1] = NULL;
2392
2393         update_timeservers(service);
2394
2395         return 0;
2396 }
2397
2398 int __connman_service_timeserver_remove(struct connman_service *service,
2399                                                 const char *timeserver)
2400 {
2401         char **servers;
2402         int len, i, j;
2403
2404         DBG("service %p timeserver %s", service, timeserver);
2405
2406         if (timeserver == NULL)
2407                 return -EINVAL;
2408
2409         if (service->timeservers == NULL)
2410                 return 0;
2411
2412         len = g_strv_length(service->timeservers);
2413         if (len == 1) {
2414                 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
2415                         return 0;
2416
2417                 g_strfreev(service->timeservers);
2418                 service->timeservers = NULL;
2419
2420                 return 0;
2421         }
2422
2423         servers = g_try_new0(char *, len - 1);
2424         if (servers == NULL)
2425                 return -ENOMEM;
2426
2427         for (i = 0, j = 0; i < len; i++) {
2428                 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2429                         servers[j] = g_strdup(service->timeservers[i]);
2430                         j++;
2431                 }
2432         }
2433         servers[len - 2] = NULL;
2434
2435         g_strfreev(service->timeservers);
2436         service->timeservers = servers;
2437
2438         update_timeservers(service);
2439
2440         return 0;
2441 }
2442
2443 void __connman_service_set_pac(struct connman_service *service,
2444                                         const char *pac)
2445 {
2446         g_free(service->pac);
2447         service->pac = g_strdup(pac);
2448
2449         proxy_changed(service);
2450 }
2451
2452 void __connman_service_set_identity(struct connman_service *service,
2453                                         const char *identity)
2454 {
2455         if (service->immutable)
2456                 return;
2457
2458         g_free(service->identity);
2459         service->identity = g_strdup(identity);
2460
2461         if (service->network != NULL)
2462                 connman_network_set_string(service->network,
2463                                         "WiFi.Identity",
2464                                         service->identity);
2465 }
2466
2467 void __connman_service_set_agent_identity(struct connman_service *service,
2468                                                 const char *agent_identity)
2469 {
2470         g_free(service->agent_identity);
2471         service->agent_identity = g_strdup(agent_identity);
2472
2473         if (service->network != NULL)
2474                 connman_network_set_string(service->network,
2475                                         "WiFi.AgentIdentity",
2476                                         service->agent_identity);
2477 }
2478
2479 void __connman_service_set_passphrase(struct connman_service *service,
2480                                         const char* passphrase)
2481 {
2482         if (service->immutable == TRUE)
2483                 return;
2484
2485         g_free(service->passphrase);
2486         service->passphrase = g_strdup(passphrase);
2487
2488         passphrase_changed(service);
2489
2490         if (service->network != NULL)
2491                 connman_network_set_string(service->network,
2492                                         "WiFi.Passphrase",
2493                                         service->passphrase);
2494
2495         service_save(service);
2496 }
2497
2498 void __connman_service_set_agent_passphrase(struct connman_service *service,
2499                                                 const char *agent_passphrase)
2500 {
2501         g_free(service->agent_passphrase);
2502         service->agent_passphrase = g_strdup(agent_passphrase);
2503
2504         if (service->network != NULL)
2505                 connman_network_set_string(service->network,
2506                                         "WiFi.AgentPassphrase",
2507                                         service->agent_passphrase);
2508 }
2509
2510 static DBusMessage *get_properties(DBusConnection *conn,
2511                                         DBusMessage *msg, void *user_data)
2512 {
2513         struct connman_service *service = user_data;
2514         DBusMessage *reply;
2515         DBusMessageIter array, dict;
2516
2517         DBG("service %p", service);
2518
2519         reply = dbus_message_new_method_return(msg);
2520         if (reply == NULL)
2521                 return NULL;
2522
2523         dbus_message_iter_init_append(reply, &array);
2524
2525         connman_dbus_dict_open(&array, &dict);
2526         append_properties(&dict, FALSE, service);
2527         connman_dbus_dict_close(&array, &dict);
2528
2529         return reply;
2530 }
2531
2532 static int update_proxy_configuration(struct connman_service *service,
2533                                 DBusMessageIter *array)
2534 {
2535         DBusMessageIter dict;
2536         enum connman_service_proxy_method method;
2537         GString *servers_str = NULL;
2538         GString *excludes_str = NULL;
2539         const char *url = NULL;
2540
2541         method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2542
2543         dbus_message_iter_recurse(array, &dict);
2544
2545         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2546                 DBusMessageIter entry, variant;
2547                 const char *key;
2548                 int type;
2549
2550                 dbus_message_iter_recurse(&dict, &entry);
2551
2552                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2553                         goto error;
2554
2555                 dbus_message_iter_get_basic(&entry, &key);
2556                 dbus_message_iter_next(&entry);
2557
2558                 if (dbus_message_iter_get_arg_type(&entry) !=
2559                                                         DBUS_TYPE_VARIANT)
2560                         goto error;
2561
2562                 dbus_message_iter_recurse(&entry, &variant);
2563                 type = dbus_message_iter_get_arg_type(&variant);
2564
2565                 if (g_str_equal(key, "Method") == TRUE) {
2566                         const char *val;
2567
2568                         if (type != DBUS_TYPE_STRING)
2569                                 goto error;
2570
2571                         dbus_message_iter_get_basic(&variant, &val);
2572                         method = string2proxymethod(val);
2573                 } else if (g_str_equal(key, "URL") == TRUE) {
2574                         if (type != DBUS_TYPE_STRING)
2575                                 goto error;
2576
2577                         dbus_message_iter_get_basic(&variant, &url);
2578                 } else if (g_str_equal(key, "Servers") == TRUE) {
2579                         DBusMessageIter str_array;
2580
2581                         if (type != DBUS_TYPE_ARRAY)
2582                                 goto error;
2583
2584                         servers_str = g_string_new(NULL);
2585                         if (servers_str == NULL)
2586                                 goto error;
2587
2588                         dbus_message_iter_recurse(&variant, &str_array);
2589
2590                         while (dbus_message_iter_get_arg_type(&str_array) ==
2591                                                         DBUS_TYPE_STRING) {
2592                                 char *val = NULL;
2593
2594                                 dbus_message_iter_get_basic(&str_array, &val);
2595
2596                                 if (servers_str->len > 0)
2597                                         g_string_append_printf(servers_str,
2598                                                         " %s", val);
2599                                 else
2600                                         g_string_append(servers_str, val);
2601
2602                                 dbus_message_iter_next(&str_array);
2603                         }
2604                 } else if (g_str_equal(key, "Excludes") == TRUE) {
2605                         DBusMessageIter str_array;
2606
2607                         if (type != DBUS_TYPE_ARRAY)
2608                                 goto error;
2609
2610                         excludes_str = g_string_new(NULL);
2611                         if (excludes_str == NULL)
2612                                 goto error;
2613
2614                         dbus_message_iter_recurse(&variant, &str_array);
2615
2616                         while (dbus_message_iter_get_arg_type(&str_array) ==
2617                                                         DBUS_TYPE_STRING) {
2618                                 char *val = NULL;
2619
2620                                 dbus_message_iter_get_basic(&str_array, &val);
2621
2622                                 if (excludes_str->len > 0)
2623                                         g_string_append_printf(excludes_str,
2624                                                         " %s", val);
2625                                 else
2626                                         g_string_append(excludes_str, val);
2627
2628                                 dbus_message_iter_next(&str_array);
2629                         }
2630                 }
2631
2632                 dbus_message_iter_next(&dict);
2633         }
2634
2635         switch (method) {
2636         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2637                 break;
2638         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2639                 if (servers_str == NULL && service->proxies == NULL)
2640                         goto error;
2641
2642                 if (servers_str != NULL) {
2643                         g_strfreev(service->proxies);
2644
2645                         if (servers_str->len > 0)
2646                                 service->proxies = g_strsplit_set(
2647                                         servers_str->str, " ", 0);
2648                         else
2649                                 service->proxies = NULL;
2650                 }
2651
2652                 if (excludes_str != NULL) {
2653                         g_strfreev(service->excludes);
2654
2655                         if (excludes_str->len > 0)
2656                                 service->excludes = g_strsplit_set(
2657                                         excludes_str->str, " ", 0);
2658                         else
2659                                 service->excludes = NULL;
2660                 }
2661
2662                 if (service->proxies == NULL)
2663                         method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2664
2665                 break;
2666         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2667                 g_free(service->pac);
2668
2669                 if (url != NULL && strlen(url) > 0)
2670                         service->pac = g_strdup(url);
2671                 else
2672                         service->pac = NULL;
2673
2674                 /* if we are connected:
2675                    - if service->pac == NULL
2676                    - if __connman_ipconfig_get_proxy_autoconfig(
2677                    service->ipconfig) == NULL
2678                    --> We should start WPAD */
2679
2680                 break;
2681         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2682                 goto error;
2683         }
2684
2685         if (servers_str != NULL)
2686                 g_string_free(servers_str, TRUE);
2687
2688         if (excludes_str != NULL)
2689                 g_string_free(excludes_str, TRUE);
2690
2691         service->proxy_config = method;
2692
2693         return 0;
2694
2695 error:
2696         if (servers_str != NULL)
2697                 g_string_free(servers_str, TRUE);
2698
2699         if (excludes_str != NULL)
2700                 g_string_free(excludes_str, TRUE);
2701
2702         return -EINVAL;
2703 }
2704
2705 static int set_ipconfig(struct connman_service *service,
2706                         struct connman_ipconfig *ipconfig,
2707                         DBusMessageIter *array,
2708                         enum connman_service_state state,
2709                         enum connman_service_state *new_state)
2710 {
2711         enum connman_ipconfig_method old_method;
2712         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2713         enum connman_ipconfig_type type;
2714         int err;
2715
2716         if (ipconfig == NULL)
2717                 return -EINVAL;
2718
2719         old_method = __connman_ipconfig_get_method(ipconfig);
2720
2721         if (is_connecting_state(service, state) ||
2722                                         is_connected_state(service, state))
2723                 __connman_network_clear_ipconfig(service->network, ipconfig);
2724
2725         err = __connman_ipconfig_set_config(ipconfig, array);
2726         method = __connman_ipconfig_get_method(ipconfig);
2727         type = __connman_ipconfig_get_config_type(ipconfig);
2728
2729         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2730                 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2731                                 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2732                         *new_state = service->state_ipv4 =
2733                                 CONNMAN_SERVICE_STATE_CONFIGURATION;
2734                         __connman_ipconfig_enable(ipconfig);
2735                 }
2736
2737         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2738                 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2739                                 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2740                         *new_state = service->state_ipv6;
2741                         __connman_ipconfig_enable(ipconfig);
2742                 }
2743         }
2744
2745         DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2746                 type, method, state2string(*new_state));
2747
2748         return err;
2749 }
2750
2751 static DBusMessage *set_property(DBusConnection *conn,
2752                                         DBusMessage *msg, void *user_data)
2753 {
2754         struct connman_service *service = user_data;
2755         DBusMessageIter iter, value;
2756         const char *name;
2757         int type;
2758
2759         DBG("service %p", service);
2760
2761         if (dbus_message_iter_init(msg, &iter) == FALSE)
2762                 return __connman_error_invalid_arguments(msg);
2763
2764         dbus_message_iter_get_basic(&iter, &name);
2765         dbus_message_iter_next(&iter);
2766         dbus_message_iter_recurse(&iter, &value);
2767
2768         type = dbus_message_iter_get_arg_type(&value);
2769
2770         if (g_str_equal(name, "AutoConnect") == TRUE) {
2771                 connman_bool_t autoconnect;
2772
2773                 if (type != DBUS_TYPE_BOOLEAN)
2774                         return __connman_error_invalid_arguments(msg);
2775
2776                 if (service->favorite == FALSE)
2777                         return __connman_error_invalid_service(msg);
2778
2779                 dbus_message_iter_get_basic(&value, &autoconnect);
2780
2781                 if (service->autoconnect == autoconnect)
2782                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2783
2784                 service->autoconnect = autoconnect;
2785
2786                 autoconnect_changed(service);
2787
2788                 service_save(service);
2789         } else if (g_str_equal(name, "Passphrase") == TRUE) {
2790                 const char *passphrase;
2791
2792                 if (type != DBUS_TYPE_STRING)
2793                         return __connman_error_invalid_arguments(msg);
2794
2795                 if (service->immutable == TRUE)
2796                         return __connman_error_not_supported(msg);
2797
2798                 dbus_message_iter_get_basic(&value, &passphrase);
2799
2800                 __connman_service_set_passphrase(service, passphrase);
2801         } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2802                 DBusMessageIter entry;
2803                 GString *str;
2804                 int index;
2805                 const char *gw;
2806
2807                 if (type != DBUS_TYPE_ARRAY)
2808                         return __connman_error_invalid_arguments(msg);
2809
2810                 str = g_string_new(NULL);
2811                 if (str == NULL)
2812                         return __connman_error_invalid_arguments(msg);
2813
2814                 index = connman_network_get_index(service->network);
2815                 gw = __connman_ipconfig_get_gateway_from_index(index);
2816
2817                 if (gw && strlen(gw))
2818                         __connman_service_nameserver_del_routes(service);
2819
2820                 dbus_message_iter_recurse(&value, &entry);
2821
2822                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2823                         const char *val;
2824                         dbus_message_iter_get_basic(&entry, &val);
2825                         dbus_message_iter_next(&entry);
2826                         if (str->len > 0)
2827                                 g_string_append_printf(str, " %s", val);
2828                         else
2829                                 g_string_append(str, val);
2830                 }
2831
2832                 g_strfreev(service->nameservers_config);
2833
2834                 if (str->len > 0) {
2835                         service->nameservers_config =
2836                                 g_strsplit_set(str->str, " ", 0);
2837                 } else {
2838                         service->nameservers_config = NULL;
2839                 }
2840
2841                 g_string_free(str, TRUE);
2842
2843                 if (gw && strlen(gw))
2844                         __connman_service_nameserver_add_routes(service, gw);
2845
2846                 update_nameservers(service);
2847                 dns_configuration_changed(service);
2848
2849                 service_save(service);
2850         } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2851                 DBusMessageIter entry;
2852                 GString *str;
2853
2854                 if (type != DBUS_TYPE_ARRAY)
2855                         return __connman_error_invalid_arguments(msg);
2856
2857                 str = g_string_new(NULL);
2858                 if (str == NULL)
2859                         return __connman_error_invalid_arguments(msg);
2860
2861                 dbus_message_iter_recurse(&value, &entry);
2862
2863                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2864                         const char *val;
2865                         dbus_message_iter_get_basic(&entry, &val);
2866                         dbus_message_iter_next(&entry);
2867                         if (str->len > 0)
2868                                 g_string_append_printf(str, " %s", val);
2869                         else
2870                                 g_string_append(str, val);
2871                 }
2872
2873                 g_strfreev(service->domains);
2874
2875                 if (str->len > 0)
2876                         service->domains = g_strsplit_set(str->str, " ", 0);
2877                 else
2878                         service->domains = NULL;
2879
2880                 g_string_free(str, TRUE);
2881
2882                 update_nameservers(service);
2883                 domain_configuration_changed(service);
2884
2885                 service_save(service);
2886         } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2887                 int err;
2888
2889                 if (type != DBUS_TYPE_ARRAY)
2890                         return __connman_error_invalid_arguments(msg);
2891
2892                 err = update_proxy_configuration(service, &value);
2893
2894                 if (err < 0)
2895                         return __connman_error_failed(msg, -err);
2896
2897                 proxy_configuration_changed(service);
2898
2899                 __connman_notifier_proxy_changed(service);
2900
2901                 service_save(service);
2902         } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2903                         g_str_equal(name, "IPv6.Configuration")) {
2904
2905                 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2906                 enum connman_service_state state =
2907                                                 CONNMAN_SERVICE_STATE_UNKNOWN;
2908                 int err = 0;
2909
2910                 DBG("%s", name);
2911
2912                 if (service->ipconfig_ipv4 == NULL &&
2913                                         service->ipconfig_ipv6 == NULL)
2914                         return __connman_error_invalid_property(msg);
2915
2916                 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2917                         ipv4 = service->ipconfig_ipv4;
2918                         err = set_ipconfig(service, ipv4, &value,
2919                                         service->state_ipv4, &state);
2920
2921                 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2922                         ipv6 = service->ipconfig_ipv6;
2923                         err = set_ipconfig(service, ipv6, &value,
2924                                         service->state_ipv6, &state);
2925                 }
2926
2927                 if (err < 0) {
2928                         if (is_connected_state(service, state) ||
2929                                         is_connecting_state(service, state))
2930                                 __connman_network_set_ipconfig(service->network,
2931                                                                 ipv4, ipv6);
2932                         return __connman_error_failed(msg, -err);
2933                 }
2934
2935                 if (ipv4)
2936                         ipv4_configuration_changed(service);
2937                 else if (ipv6)
2938                         ipv6_configuration_changed(service);
2939
2940                 if (is_connecting(service) || is_connected(service))
2941                         __connman_network_set_ipconfig(service->network,
2942                                                         ipv4, ipv6);
2943
2944                 service_save(service);
2945         } else
2946                 return __connman_error_invalid_property(msg);
2947
2948         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2949 }
2950
2951 static void set_idle(struct connman_service *service)
2952 {
2953         service->state = service->state_ipv4 = service->state_ipv6 =
2954                                                 CONNMAN_SERVICE_STATE_IDLE;
2955         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2956         state_changed(service);
2957 }
2958
2959 static DBusMessage *clear_property(DBusConnection *conn,
2960                                         DBusMessage *msg, void *user_data)
2961 {
2962         struct connman_service *service = user_data;
2963         const char *name;
2964
2965         DBG("service %p", service);
2966
2967         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2968                                                         DBUS_TYPE_INVALID);
2969
2970         if (g_str_equal(name, "Error") == TRUE) {
2971                 set_idle(service);
2972
2973                 g_get_current_time(&service->modified);
2974                 service_save(service);
2975         } else if (g_str_equal(name, "Passphrase") == TRUE) {
2976                 if (service->immutable == TRUE)
2977                         return __connman_error_not_supported(msg);
2978
2979                 g_free(service->passphrase);
2980                 service->passphrase = NULL;
2981
2982                 passphrase_changed(service);
2983
2984                 service_save(service);
2985         } else
2986                 return __connman_error_invalid_property(msg);
2987
2988         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2989 }
2990
2991 static connman_bool_t is_ignore(struct connman_service *service)
2992 {
2993         if (service->autoconnect == FALSE)
2994                 return TRUE;
2995
2996         if (service->roaming == TRUE)
2997                 return TRUE;
2998
2999         if (service->ignore == TRUE)
3000                 return TRUE;
3001
3002         if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3003                 return TRUE;
3004
3005         return FALSE;
3006 }
3007
3008 void __connman_service_auto_connect(void)
3009 {
3010         struct connman_service *service = NULL;
3011         GSequenceIter *iter;
3012
3013         DBG("");
3014
3015         if (__connman_session_mode() == TRUE) {
3016                 DBG("Session mode enabled: auto connect disabled");
3017                 return;
3018         }
3019
3020         iter = g_sequence_get_begin_iter(service_list);
3021
3022         while (g_sequence_iter_is_end(iter) == FALSE) {
3023                 service = g_sequence_get(iter);
3024
3025                 if (service->pending != NULL)
3026                         return;
3027
3028                 if (is_connecting(service) == TRUE)
3029                         return;
3030
3031                 if (service->favorite == FALSE)
3032                         return;
3033
3034                 if (is_connected(service) == TRUE)
3035                         return;
3036
3037                 if (is_ignore(service) == FALSE && service->state ==
3038                                                 CONNMAN_SERVICE_STATE_IDLE)
3039                         break;
3040
3041                 service = NULL;
3042
3043                 iter = g_sequence_iter_next(iter);
3044         }
3045
3046         if (service != NULL) {
3047                 service->userconnect = FALSE;
3048                 __connman_service_connect(service);
3049         }
3050 }
3051
3052 static void remove_timeout(struct connman_service *service)
3053 {
3054         if (service->timeout > 0) {
3055                 g_source_remove(service->timeout);
3056                 service->timeout = 0;
3057         }
3058 }
3059
3060 static void reply_pending(struct connman_service *service, int error)
3061 {
3062         remove_timeout(service);
3063
3064         if (service->pending != NULL) {
3065                 if (error > 0) {
3066                         DBusMessage *reply;
3067
3068                         reply = __connman_error_failed(service->pending,
3069                                                                 error);
3070                         if (reply != NULL)
3071                                 g_dbus_send_message(connection, reply);
3072                 } else {
3073                         const char *sender;
3074
3075                         sender = dbus_message_get_interface(service->pending);
3076
3077                         DBG("sender %s", sender);
3078
3079                         if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
3080                                 g_dbus_send_reply(connection, service->pending,
3081                                         DBUS_TYPE_OBJECT_PATH, &service->path,
3082                                                         DBUS_TYPE_INVALID);
3083                         else
3084                                 g_dbus_send_reply(connection, service->pending,
3085                                                         DBUS_TYPE_INVALID);
3086                 }
3087
3088                 dbus_message_unref(service->pending);
3089                 service->pending = NULL;
3090         }
3091 }
3092
3093 static gboolean connect_timeout(gpointer user_data)
3094 {
3095         struct connman_service *service = user_data;
3096         connman_bool_t autoconnect = FALSE;
3097
3098         DBG("service %p", service);
3099
3100         service->timeout = 0;
3101
3102         if (service->network != NULL)
3103                 __connman_network_disconnect(service->network);
3104
3105         __connman_ipconfig_disable(service->ipconfig_ipv4);
3106         __connman_ipconfig_disable(service->ipconfig_ipv6);
3107
3108         __connman_stats_service_unregister(service);
3109
3110         if (service->pending != NULL) {
3111                 DBusMessage *reply;
3112
3113                 reply = __connman_error_operation_timeout(service->pending);
3114                 if (reply != NULL)
3115                         g_dbus_send_message(connection, reply);
3116
3117                 dbus_message_unref(service->pending);
3118                 service->pending = NULL;
3119         } else
3120                 autoconnect = TRUE;
3121
3122         __connman_service_ipconfig_indicate_state(service,
3123                                         CONNMAN_SERVICE_STATE_FAILURE,
3124                                         CONNMAN_IPCONFIG_TYPE_IPV4);
3125         __connman_service_ipconfig_indicate_state(service,
3126                                         CONNMAN_SERVICE_STATE_FAILURE,
3127                                         CONNMAN_IPCONFIG_TYPE_IPV6);
3128
3129         if (autoconnect == TRUE && service->userconnect == FALSE)
3130                 __connman_service_auto_connect();
3131
3132         return FALSE;
3133 }
3134
3135 static void set_reconnect_state(struct connman_service *service,
3136                                                 connman_bool_t reconnect)
3137 {
3138         struct connman_device *device;
3139
3140         if (service->network == NULL)
3141                 return;
3142
3143         device = connman_network_get_device(service->network);
3144         if (device == NULL)
3145                 return;
3146
3147         __connman_device_set_reconnect(device, reconnect);
3148 }
3149
3150 static connman_bool_t get_reconnect_state(struct connman_service *service)
3151 {
3152         struct connman_device *device;
3153
3154         if (service->network == NULL)
3155                 return FALSE;
3156
3157         device = connman_network_get_device(service->network);
3158         if (device == NULL)
3159                 return FALSE;
3160
3161         return __connman_device_get_reconnect(device);
3162 }
3163
3164 static DBusMessage *connect_service(DBusConnection *conn,
3165                                         DBusMessage *msg, void *user_data)
3166 {
3167         struct connman_service *service = user_data;
3168         GSequenceIter *iter;
3169         int err;
3170
3171         DBG("service %p", service);
3172
3173         if (service->pending != NULL)
3174                 return __connman_error_in_progress(msg);
3175
3176         iter = g_sequence_get_begin_iter(service_list);
3177
3178         while (g_sequence_iter_is_end(iter) == FALSE) {
3179                 struct connman_service *temp = g_sequence_get(iter);
3180
3181                 if (service->type == temp->type && is_connecting(temp) == TRUE)
3182                         return __connman_error_in_progress(msg);
3183
3184                 iter = g_sequence_iter_next(iter);
3185         }
3186
3187         service->ignore = FALSE;
3188
3189         service->userconnect = TRUE;
3190
3191         service->pending = dbus_message_ref(msg);
3192
3193         set_reconnect_state(service, FALSE);
3194
3195         err = __connman_service_connect(service);
3196         if (err < 0) {
3197                 if (service->pending == NULL)
3198                         return NULL;
3199
3200                 if (err != -EINPROGRESS) {
3201                         dbus_message_unref(service->pending);
3202                         service->pending = NULL;
3203
3204                         return __connman_error_failed(msg, -err);
3205                 }
3206
3207                 return NULL;
3208         }
3209
3210         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3211 }
3212
3213 static DBusMessage *disconnect_service(DBusConnection *conn,
3214                                         DBusMessage *msg, void *user_data)
3215 {
3216         struct connman_service *service = user_data;
3217         int err;
3218
3219         DBG("service %p", service);
3220
3221         reply_pending(service, ECONNABORTED);
3222
3223         service->ignore = TRUE;
3224
3225         set_reconnect_state(service, FALSE);
3226
3227         err = __connman_service_disconnect(service);
3228         if (err < 0) {
3229                 if (err != -EINPROGRESS)
3230                         return __connman_error_failed(msg, -err);
3231
3232                 return NULL;
3233         }
3234
3235         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3236 }
3237
3238 static DBusMessage *remove_service(DBusConnection *conn,
3239                                         DBusMessage *msg, void *user_data)
3240 {
3241         struct connman_service *service = user_data;
3242
3243         DBG("service %p", service);
3244
3245         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3246                 return __connman_error_not_supported(msg);
3247
3248         if (service->immutable == TRUE)
3249                 return __connman_error_not_supported(msg);
3250
3251         if (service->favorite == FALSE && service->state !=
3252                                                 CONNMAN_SERVICE_STATE_FAILURE)
3253                 return __connman_error_not_supported(msg);
3254
3255         set_reconnect_state(service, FALSE);
3256
3257         __connman_service_disconnect(service);
3258
3259         g_free(service->passphrase);
3260         service->passphrase = NULL;
3261
3262         passphrase_changed(service);
3263
3264         set_idle(service);
3265
3266         __connman_service_set_favorite(service, FALSE);
3267         service_save(service);
3268
3269         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3270 }
3271
3272 static gboolean check_suitable_state(enum connman_service_state a,
3273                                         enum connman_service_state b)
3274 {
3275         /*
3276          * Special check so that "ready" service can be moved before
3277          * "online" one.
3278          */
3279         if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3280                         b == CONNMAN_SERVICE_STATE_READY) ||
3281                 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3282                         a == CONNMAN_SERVICE_STATE_READY))
3283                 return TRUE;
3284
3285         return a == b;
3286 }
3287
3288 static DBusMessage *move_service(DBusConnection *conn,
3289                                         DBusMessage *msg, void *user_data,
3290                                                                 gboolean before)
3291 {
3292         struct connman_service *service = user_data;
3293         struct connman_service *target;
3294         const char *path;
3295         GSequenceIter *src, *dst;
3296         enum connman_ipconfig_method target4, target6;
3297         enum connman_ipconfig_method service4, service6;
3298
3299         DBG("service %p", service);
3300
3301         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3302                                                         DBUS_TYPE_INVALID);
3303
3304         if (service->favorite == FALSE)
3305                 return __connman_error_not_supported(msg);
3306
3307         target = find_service(path);
3308         if (target == NULL || target->favorite == FALSE || target == service ||
3309                                 target->type == CONNMAN_SERVICE_TYPE_VPN)
3310                 return __connman_error_invalid_service(msg);
3311
3312         target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3313         target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3314         service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3315         service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3316
3317         DBG("target %s method %d/%d state %d/%d", target->identifier,
3318                                 target4, target6,
3319                                 target->state_ipv4, target->state_ipv6);
3320
3321         DBG("service %s method %d/%d state %d/%d", service->identifier,
3322                                 service4, service6,
3323                                 service->state_ipv4, service->state_ipv6);
3324
3325         /*
3326          * If method is OFF, then we do not need to check the corresponding
3327          * ipconfig state.
3328          */
3329         if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3330                 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3331                         if (check_suitable_state(target->state_ipv6,
3332                                                 service->state_ipv6) == FALSE)
3333                                 return __connman_error_invalid_service(msg);
3334                 }
3335         }
3336
3337         if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3338                 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3339                         if (check_suitable_state(target->state_ipv4,
3340                                                 service->state_ipv4) == FALSE)
3341                                 return __connman_error_invalid_service(msg);
3342                 }
3343         }
3344
3345         if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3346                 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3347                         if (check_suitable_state(target->state_ipv6,
3348                                                 service->state_ipv6) == FALSE)
3349                                 return __connman_error_invalid_service(msg);
3350                 }
3351         }
3352
3353         if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3354                 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3355                         if (check_suitable_state(target->state_ipv4,
3356                                                 service->state_ipv4) == FALSE)
3357                                 return __connman_error_invalid_service(msg);
3358                 }
3359         }
3360
3361         g_get_current_time(&service->modified);
3362         service_save(service);
3363
3364         src = g_hash_table_lookup(service_hash, service->identifier);
3365         dst = g_hash_table_lookup(service_hash, target->identifier);
3366
3367         before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
3368
3369         services_changed(FALSE);
3370
3371         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3372 }
3373
3374 static DBusMessage *move_before(DBusConnection *conn,
3375                                         DBusMessage *msg, void *user_data)
3376 {
3377         return move_service(conn, msg, user_data, TRUE);
3378 }
3379
3380 static DBusMessage *move_after(DBusConnection *conn,
3381                                         DBusMessage *msg, void *user_data)
3382 {
3383         return move_service(conn, msg, user_data, FALSE);
3384 }
3385
3386 static DBusMessage *reset_counters(DBusConnection *conn,
3387                                         DBusMessage *msg, void *user_data)
3388 {
3389         struct connman_service *service = user_data;
3390
3391         reset_stats(service);
3392
3393         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3394 }
3395
3396 static GDBusMethodTable service_methods[] = {
3397         { "GetProperties", "",   "a{sv}", get_properties     },
3398         { "SetProperty",   "sv", "",      set_property       },
3399         { "ClearProperty", "s",  "",      clear_property     },
3400         { "Connect",       "",   "",      connect_service,
3401                                                 G_DBUS_METHOD_FLAG_ASYNC },
3402         { "Disconnect",    "",   "",      disconnect_service },
3403         { "Remove",        "",   "",      remove_service     },
3404         { "MoveBefore",    "o",  "",      move_before        },
3405         { "MoveAfter",     "o",  "",      move_after         },
3406         { "ResetCounters", "",   "",      reset_counters     },
3407         { },
3408 };
3409
3410 static GDBusSignalTable service_signals[] = {
3411         { "PropertyChanged", "sv" },
3412         { },
3413 };
3414
3415 static void service_free(gpointer user_data)
3416 {
3417         struct connman_service *service = user_data;
3418         char *path = service->path;
3419
3420         DBG("service %p", service);
3421
3422         reply_pending(service, ENOENT);
3423
3424         g_hash_table_remove(service_hash, service->identifier);
3425
3426         __connman_notifier_service_remove(service);
3427
3428         stats_stop(service);
3429
3430         service->path = NULL;
3431
3432         if (path != NULL) {
3433                 services_changed(FALSE);
3434
3435                 g_dbus_unregister_interface(connection, path,
3436                                                 CONNMAN_SERVICE_INTERFACE);
3437                 g_free(path);
3438         }
3439
3440         g_hash_table_destroy(service->counter_table);
3441
3442         if (service->network != NULL) {
3443                 if (service->network_created == TRUE)
3444                         connman_network_unref(service->network);
3445         }
3446
3447         if (service->provider != NULL)
3448                 connman_provider_unref(service->provider);
3449
3450         if (service->ipconfig_ipv4 != NULL) {
3451                 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3452                 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3453                 connman_ipconfig_unref(service->ipconfig_ipv4);
3454                 service->ipconfig_ipv4 = NULL;
3455         }
3456
3457         if (service->ipconfig_ipv6 != NULL) {
3458                 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3459                 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3460                 connman_ipconfig_unref(service->ipconfig_ipv6);
3461                 service->ipconfig_ipv6 = NULL;
3462         }
3463
3464         g_strfreev(service->nameservers);
3465         g_strfreev(service->nameservers_config);
3466         g_strfreev(service->nameservers_auto);
3467         g_strfreev(service->domains);
3468         g_strfreev(service->proxies);
3469         g_strfreev(service->excludes);
3470
3471         g_free(service->domainname);
3472         g_free(service->pac);
3473         g_free(service->name);
3474         g_free(service->passphrase);
3475         g_free(service->agent_passphrase);
3476         g_free(service->identifier);
3477         g_free(service->eap);
3478         g_free(service->identity);
3479         g_free(service->agent_identity);
3480         g_free(service->ca_cert_file);
3481         g_free(service->client_cert_file);
3482         g_free(service->private_key_file);
3483         g_free(service->private_key_passphrase);
3484         g_free(service->phase2);
3485
3486         if (service->stats.timer != NULL)
3487                 g_timer_destroy(service->stats.timer);
3488         if (service->stats_roaming.timer != NULL)
3489                 g_timer_destroy(service->stats_roaming.timer);
3490
3491         g_free(service);
3492 }
3493
3494 /**
3495  * __connman_service_put:
3496  * @service: service structure
3497  *
3498  * Release service if no longer needed
3499  */
3500 void __connman_service_put(struct connman_service *service)
3501 {
3502         GSequenceIter *iter;
3503
3504         DBG("service %p", service);
3505
3506         if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
3507                 return;
3508
3509         iter = g_hash_table_lookup(service_hash, service->identifier);
3510         if (iter != NULL) {
3511                 reply_pending(service, ECONNABORTED);
3512
3513                 __connman_service_disconnect(service);
3514
3515                 g_sequence_remove(iter);
3516         } else {
3517                 service_free(service);
3518         }
3519 }
3520
3521 static void stats_init(struct connman_service *service)
3522 {
3523         /* home */
3524         service->stats.valid = FALSE;
3525         service->stats.enabled = FALSE;
3526         service->stats.timer = g_timer_new();
3527
3528         /* roaming */
3529         service->stats_roaming.valid = FALSE;
3530         service->stats_roaming.enabled = FALSE;
3531         service->stats_roaming.timer = g_timer_new();
3532 }
3533
3534 static void service_initialize(struct connman_service *service)
3535 {
3536         DBG("service %p", service);
3537
3538         service->refcount = 1;
3539         service->session_usage_count = 0;
3540
3541         service->network_created = FALSE;
3542
3543         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
3544         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3545
3546         service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3547         service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3548         service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3549
3550         service->favorite  = FALSE;
3551         service->immutable = FALSE;
3552         service->hidden = FALSE;
3553
3554         service->ignore = FALSE;
3555
3556         service->userconnect = FALSE;
3557
3558         service->order = 0;
3559
3560         stats_init(service);
3561
3562         service->provider = NULL;
3563
3564         service->wps = FALSE;
3565 }
3566
3567 /**
3568  * connman_service_create:
3569  *
3570  * Allocate a new service.
3571  *
3572  * Returns: a newly-allocated #connman_service structure
3573  */
3574 struct connman_service *connman_service_create(void)
3575 {
3576         GSList *list;
3577         struct connman_stats_counter *counters;
3578         const char *counter;
3579
3580         struct connman_service *service;
3581
3582         service = g_try_new0(struct connman_service, 1);
3583         if (service == NULL)
3584                 return NULL;
3585
3586         DBG("service %p", service);
3587
3588         service->counter_table = g_hash_table_new_full(g_str_hash,
3589                                                 g_str_equal, NULL, g_free);
3590
3591         for (list = counter_list; list; list = list->next) {
3592                 counter = list->data;
3593
3594                 counters = g_try_new0(struct connman_stats_counter, 1);
3595                 if (counters == NULL) {
3596                         g_hash_table_destroy(service->counter_table);
3597                         g_free(service);
3598                         return NULL;
3599                 }
3600
3601                 counters->append_all = TRUE;
3602
3603                 g_hash_table_replace(service->counter_table, (gpointer)counter,
3604                                 counters);
3605         }
3606
3607         service_initialize(service);
3608
3609         return service;
3610 }
3611
3612 /**
3613  * connman_service_ref:
3614  * @service: service structure
3615  *
3616  * Increase reference counter of service
3617  */
3618 struct connman_service *connman_service_ref(struct connman_service *service)
3619 {
3620         DBG("%p", service);
3621
3622         __sync_fetch_and_add(&service->refcount, 1);
3623
3624         return service;
3625 }
3626
3627 /**
3628  * connman_service_unref:
3629  * @service: service structure
3630  *
3631  * Decrease reference counter of service
3632  */
3633 void connman_service_unref(struct connman_service *service)
3634 {
3635         __connman_service_put(service);
3636 }
3637
3638 static gint service_compare(gconstpointer a, gconstpointer b,
3639                                                         gpointer user_data)
3640 {
3641         struct connman_service *service_a = (void *) a;
3642         struct connman_service *service_b = (void *) b;
3643         enum connman_service_state state_a, state_b;
3644
3645         state_a = service_a->state;
3646         state_b = service_b->state;
3647
3648         if (state_a != state_b) {
3649                 gboolean a_connected = is_connected(service_a);
3650                 gboolean b_connected = is_connected(service_b);
3651
3652                 if (a_connected == TRUE && b_connected == TRUE) {
3653                         /* We prefer online over ready state */
3654                         if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3655                                 return -1;
3656
3657                         if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3658                                 return 1;
3659                 }
3660
3661                 if (a_connected == TRUE)
3662                         return -1;
3663                 if (b_connected == TRUE)
3664                         return 1;
3665
3666                 if (is_connecting(service_a) == TRUE)
3667                         return -1;
3668                 if (is_connecting(service_b) == TRUE)
3669                         return 1;
3670         }
3671
3672         if (service_a->order > service_b->order)
3673                 return -1;
3674
3675         if (service_a->order < service_b->order)
3676                 return 1;
3677
3678         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3679                 return -1;
3680
3681         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3682                 return 1;
3683
3684         if (service_a->type != service_b->type) {
3685                 switch (service_a->type) {
3686                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3687                 case CONNMAN_SERVICE_TYPE_SYSTEM:
3688                 case CONNMAN_SERVICE_TYPE_ETHERNET:
3689                 case CONNMAN_SERVICE_TYPE_GPS:
3690                 case CONNMAN_SERVICE_TYPE_VPN:
3691                 case CONNMAN_SERVICE_TYPE_GADGET:
3692                         break;
3693                 case CONNMAN_SERVICE_TYPE_WIFI:
3694                         return 1;
3695                 case CONNMAN_SERVICE_TYPE_WIMAX:
3696                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3697                 case CONNMAN_SERVICE_TYPE_CELLULAR:
3698                         return -1;
3699                 }
3700         }
3701
3702         return (gint) service_b->strength - (gint) service_a->strength;
3703 }
3704
3705 /**
3706  * connman_service_get_type:
3707  * @service: service structure
3708  *
3709  * Get the type of service
3710  */
3711 enum connman_service_type connman_service_get_type(struct connman_service *service)
3712 {
3713         if (service == NULL)
3714                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3715
3716         return service->type;
3717 }
3718
3719 /**
3720  * connman_service_get_interface:
3721  * @service: service structure
3722  *
3723  * Get network interface of service
3724  */
3725 char *connman_service_get_interface(struct connman_service *service)
3726 {
3727         int index;
3728
3729         if (service == NULL)
3730                 return NULL;
3731
3732         if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3733                 if (service->ipconfig_ipv4)
3734                         index = connman_ipconfig_get_index(
3735                                                 service->ipconfig_ipv4);
3736                 else if (service->ipconfig_ipv6)
3737                         index = connman_ipconfig_get_index(
3738                                                 service->ipconfig_ipv6);
3739                 else
3740                         return NULL;
3741
3742                 return connman_inet_ifname(index);
3743         }
3744
3745         if (service->network == NULL)
3746                 return NULL;
3747
3748         index = connman_network_get_index(service->network);
3749
3750         return connman_inet_ifname(index);
3751 }
3752
3753 /**
3754  * connman_service_get_network:
3755  * @service: service structure
3756  *
3757  * Get the service network
3758  */
3759 struct connman_network *
3760 __connman_service_get_network(struct connman_service *service)
3761 {
3762         if (service == NULL)
3763                 return NULL;
3764
3765         return service->network;
3766 }
3767
3768 struct connman_ipconfig *
3769 __connman_service_get_ip4config(struct connman_service *service)
3770 {
3771         if (service == NULL)
3772                 return NULL;
3773
3774         return service->ipconfig_ipv4;
3775 }
3776
3777 struct connman_ipconfig *
3778 __connman_service_get_ip6config(struct connman_service *service)
3779 {
3780         if (service == NULL)
3781                 return NULL;
3782
3783         return service->ipconfig_ipv6;
3784 }
3785
3786 struct connman_ipconfig *
3787 __connman_service_get_ipconfig(struct connman_service *service, int family)
3788 {
3789         if (family == AF_INET)
3790                 return __connman_service_get_ip4config(service);
3791         else if (family == AF_INET6)
3792                 return __connman_service_get_ip6config(service);
3793         else
3794                 return NULL;
3795
3796 }
3797
3798 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3799 {
3800         if (service == NULL)
3801                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3802
3803         return service->security;
3804 }
3805
3806 const char *__connman_service_get_phase2(struct connman_service *service)
3807 {
3808         if (service == NULL)
3809                 return NULL;
3810
3811         return service->phase2;
3812 }
3813
3814 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3815 {
3816         if (service == NULL)
3817                 return FALSE;
3818
3819         return service->wps;
3820 }
3821
3822 /**
3823  * __connman_service_set_favorite:
3824  * @service: service structure
3825  * @favorite: favorite value
3826  *
3827  * Change the favorite setting of service
3828  */
3829 int __connman_service_set_favorite(struct connman_service *service,
3830                                                 connman_bool_t favorite)
3831 {
3832         GSequenceIter *iter;
3833
3834         iter = g_hash_table_lookup(service_hash, service->identifier);
3835         if (iter == NULL)
3836                 return -ENOENT;
3837
3838         if (service->favorite == favorite)
3839                 return -EALREADY;
3840
3841         service->favorite = favorite;
3842         service->order = __connman_service_get_order(service);
3843
3844         favorite_changed(service);
3845
3846         g_sequence_sort_changed(iter, service_compare, NULL);
3847
3848         services_changed(FALSE);
3849
3850         return 0;
3851 }
3852
3853 int __connman_service_set_immutable(struct connman_service *service,
3854                                                 connman_bool_t immutable)
3855 {
3856         service->immutable = immutable;
3857
3858         immutable_changed(service);
3859
3860         return 0;
3861 }
3862
3863 void __connman_service_set_string(struct connman_service *service,
3864                                   const char *key, const char *value)
3865 {
3866         if (g_str_equal(key, "EAP") == TRUE) {
3867                 g_free(service->eap);
3868                 service->eap = g_strdup(value);
3869         } else if (g_str_equal(key, "Identity") == TRUE) {
3870                 g_free(service->identity);
3871                 service->identity = g_strdup(value);
3872         } else if (g_str_equal(key, "CACertFile") == TRUE) {
3873                 g_free(service->ca_cert_file);
3874                 service->ca_cert_file = g_strdup(value);
3875         } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3876                 g_free(service->client_cert_file);
3877                 service->client_cert_file = g_strdup(value);
3878         } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3879                 g_free(service->private_key_file);
3880                 service->private_key_file = g_strdup(value);
3881         } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3882                 g_free(service->private_key_passphrase);
3883                 service->private_key_passphrase = g_strdup(value);
3884         } else if (g_str_equal(key, "Phase2") == TRUE) {
3885                 g_free(service->phase2);
3886                 service->phase2 = g_strdup(value);
3887         } else if (g_str_equal(key, "Passphrase") == TRUE) {
3888                 g_free(service->passphrase);
3889                 service->passphrase = g_strdup(value);
3890         }
3891 }
3892
3893 static void service_complete(struct connman_service *service)
3894 {
3895         reply_pending(service, EIO);
3896
3897         if (service->userconnect == FALSE)
3898                 __connman_service_auto_connect();
3899
3900         g_get_current_time(&service->modified);
3901         service_save(service);
3902 }
3903
3904 static void report_error_cb(struct connman_service *service,
3905                         gboolean retry, void *user_data)
3906 {
3907         if (retry == TRUE)
3908                 __connman_service_connect(service);
3909         else {
3910                 service_complete(service);
3911                 services_changed(FALSE);
3912                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3913         }
3914 }
3915
3916 static void request_input_cb (struct connman_service *service,
3917                         const char *identity, const char *passphrase,
3918                         void *user_data)
3919 {
3920         DBG ("RequestInput return, %p", service);
3921
3922         if (identity == NULL && passphrase == NULL && service->wps == FALSE) {
3923                 service_complete(service);
3924                 services_changed(FALSE);
3925                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3926                 return;
3927         }
3928
3929         if (identity != NULL)
3930                 __connman_service_set_agent_identity(service, identity);
3931
3932         if (passphrase != NULL) {
3933                 switch (service->security) {
3934                 case CONNMAN_SERVICE_SECURITY_WEP:
3935                 case CONNMAN_SERVICE_SECURITY_PSK:
3936                         __connman_service_set_passphrase(service, passphrase);
3937                         break;
3938                 case CONNMAN_SERVICE_SECURITY_8021X:
3939                         __connman_service_set_agent_passphrase(service,
3940                                                         passphrase);
3941                         break;
3942                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3943                 case CONNMAN_SERVICE_SECURITY_NONE:
3944                 case CONNMAN_SERVICE_SECURITY_WPA:
3945                 case CONNMAN_SERVICE_SECURITY_RSN:
3946                         DBG("service security '%s' not handled",
3947                                 security2string(service->security));
3948                         break;
3949                 }
3950         }
3951
3952         __connman_service_connect(service);
3953
3954         /* Never cache agent provided credentials */
3955         __connman_service_set_agent_identity(service, NULL);
3956         __connman_service_set_agent_passphrase(service, NULL);
3957 }
3958
3959 static int service_indicate_state(struct connman_service *service)
3960 {
3961         enum connman_service_state old_state, new_state;
3962         GSequenceIter *iter;
3963
3964         if (service == NULL)
3965                 return -EINVAL;
3966
3967         old_state = service->state;
3968         new_state = combine_state(service->state_ipv4, service->state_ipv6);
3969
3970         DBG("service %p old %s - new %s/%s => %s",
3971                                         service,
3972                                         state2string(old_state),
3973                                         state2string(service->state_ipv4),
3974                                         state2string(service->state_ipv6),
3975                                         state2string(new_state));
3976
3977         if (old_state == new_state)
3978                 return -EALREADY;
3979
3980         service->state = new_state;
3981         state_changed(service);
3982
3983         if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3984                         old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3985                 reply_pending(service, ECONNABORTED);
3986
3987                 __connman_service_disconnect(service);
3988         }
3989
3990         if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3991                 if (__connman_stats_service_register(service) == 0) {
3992                         __connman_stats_get(service, FALSE,
3993                                                 &service->stats.data);
3994                         __connman_stats_get(service, TRUE,
3995                                                 &service->stats_roaming.data);
3996                 }
3997         }
3998
3999         if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4000                 if (service->login_required == TRUE) {
4001                         service->login_required = FALSE;
4002                         login_changed(service);
4003                 }
4004
4005                 connman_timeserver_sync();
4006         }
4007
4008         if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4009                 connman_bool_t reconnect;
4010
4011                 reconnect = get_reconnect_state(service);
4012                 if (reconnect == TRUE)
4013                         __connman_service_auto_connect();
4014
4015                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4016         }
4017
4018         if (new_state == CONNMAN_SERVICE_STATE_READY) {
4019                 enum connman_ipconfig_method method;
4020
4021                 set_reconnect_state(service, TRUE);
4022
4023                 __connman_service_set_favorite(service, TRUE);
4024
4025                 reply_pending(service, 0);
4026
4027                 service->userconnect = FALSE;
4028
4029                 g_get_current_time(&service->modified);
4030                 service_save(service);
4031
4032                 update_nameservers(service);
4033                 dns_changed(service);
4034                 domain_changed(service);
4035
4036                 __connman_notifier_connect(service->type);
4037
4038                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4039                         connman_network_get_bool(service->network,
4040                                                 "WiFi.UseWPS") == TRUE) {
4041                         const char *pass;
4042
4043                         pass = connman_network_get_string(service->network,
4044                                                         "WiFi.Passphrase");
4045
4046                         __connman_service_set_passphrase(service, pass);
4047
4048                         connman_network_set_bool(service->network,
4049                                                         "WiFi.UseWPS", FALSE);
4050                 }
4051
4052                 default_changed();
4053
4054                 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4055                 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4056                         __connman_ipconfig_disable_ipv6(
4057                                                 service->ipconfig_ipv6);
4058
4059         } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4060                 struct connman_service *def_service = get_default();
4061
4062                 if (__connman_notifier_count_connected() == 0 &&
4063                         def_service != NULL &&
4064                                 def_service->provider != NULL)
4065                         __connman_provider_disconnect(def_service->provider);
4066
4067                 default_changed();
4068
4069                 __connman_wispr_stop(service);
4070
4071                 __connman_wpad_stop(service);
4072
4073                 update_nameservers(service);
4074                 dns_changed(service);
4075                 domain_changed(service);
4076
4077                 __connman_notifier_disconnect(service->type);
4078         }
4079
4080         if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4081                 if (service->userconnect == TRUE &&
4082                         __connman_agent_report_error(service,
4083                                         error2string(service->error),
4084                                         report_error_cb, NULL) == -EIO)
4085                         return 0;
4086                 service_complete(service);
4087
4088                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4089         } else
4090                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4091
4092         iter = g_hash_table_lookup(service_hash, service->identifier);
4093         if (iter != NULL)
4094                 g_sequence_sort_changed(iter, service_compare, NULL);
4095
4096         services_changed(FALSE);
4097
4098         if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4099                 default_changed();
4100
4101         return 0;
4102 }
4103
4104 int __connman_service_indicate_error(struct connman_service *service,
4105                                         enum connman_service_error error)
4106 {
4107         DBG("service %p error %d", service, error);
4108
4109         if (service == NULL)
4110                 return -EINVAL;
4111
4112         service->error = error;
4113
4114         if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4115                 __connman_service_set_passphrase(service, NULL);
4116
4117         __connman_service_ipconfig_indicate_state(service,
4118                                                 CONNMAN_SERVICE_STATE_FAILURE,
4119                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4120         __connman_service_ipconfig_indicate_state(service,
4121                                                 CONNMAN_SERVICE_STATE_FAILURE,
4122                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4123         return 0;
4124 }
4125
4126 int __connman_service_clear_error(struct connman_service *service)
4127 {
4128         DBG("service %p", service);
4129
4130         if (service == NULL)
4131                 return -EINVAL;
4132
4133         if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4134                 return -EINVAL;
4135
4136         service->state_ipv4 = service->state_ipv6 =
4137                                                 CONNMAN_SERVICE_STATE_UNKNOWN;
4138         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4139
4140         if (service->favorite == TRUE)
4141                 set_reconnect_state(service, TRUE);
4142
4143         __connman_service_ipconfig_indicate_state(service,
4144                                         CONNMAN_SERVICE_STATE_IDLE,
4145                                         CONNMAN_IPCONFIG_TYPE_IPV6);
4146
4147         /*
4148          * Toggling the IPv6 state to IDLE could trigger the auto connect
4149          * machinery and consequently the IPv4 state.
4150          */
4151         if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4152                         service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4153                 return 0;
4154
4155         return __connman_service_ipconfig_indicate_state(service,
4156                                                 CONNMAN_SERVICE_STATE_IDLE,
4157                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4158 }
4159
4160 int __connman_service_indicate_default(struct connman_service *service)
4161 {
4162         DBG("service %p", service);
4163
4164         default_changed();
4165
4166         return 0;
4167 }
4168
4169 enum connman_service_state __connman_service_ipconfig_get_state(
4170                                         struct connman_service *service,
4171                                         enum connman_ipconfig_type type)
4172 {
4173         if (service == NULL)
4174                 return CONNMAN_SERVICE_STATE_UNKNOWN;
4175
4176         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4177                 return service->state_ipv4;
4178
4179         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4180                 return service->state_ipv6;
4181
4182         return CONNMAN_SERVICE_STATE_UNKNOWN;
4183 }
4184
4185 static void check_proxy_setup(struct connman_service *service)
4186 {
4187         /*
4188          * We start WPAD if we haven't got a PAC URL from DHCP and
4189          * if our proxy manual configuration is either empty or set
4190          * to AUTO with an empty URL.
4191          */
4192
4193         if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4194                 goto done;
4195
4196         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4197                 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4198                         service->pac != NULL))
4199                 goto done;
4200
4201         if (__connman_wpad_start(service) < 0) {
4202                 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4203                 __connman_notifier_proxy_changed(service);
4204                 goto done;
4205         }
4206
4207         return;
4208
4209 done:
4210         __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4211 }
4212
4213 /*
4214  * How many networks are connected at the same time. If more than 1,
4215  * then set the rp_filter setting properly (loose mode routing) so that network
4216  * connectivity works ok. This is only done for IPv4 networks as IPv6
4217  * does not have rp_filter knob.
4218  */
4219 static int connected_networks_count;
4220 static int original_rp_filter;
4221
4222 static void service_rp_filter(struct connman_service *service,
4223                                 gboolean connected)
4224 {
4225         enum connman_ipconfig_method method;
4226
4227         method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4228
4229         switch (method) {
4230         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4231         case CONNMAN_IPCONFIG_METHOD_OFF:
4232         case CONNMAN_IPCONFIG_METHOD_AUTO:
4233                 return;
4234         case CONNMAN_IPCONFIG_METHOD_FIXED:
4235         case CONNMAN_IPCONFIG_METHOD_MANUAL:
4236         case CONNMAN_IPCONFIG_METHOD_DHCP:
4237                 break;
4238         }
4239
4240         if (connected == TRUE) {
4241                 if (connected_networks_count == 1) {
4242                         int filter_value;
4243                         filter_value = __connman_ipconfig_set_rp_filter();
4244                         if (filter_value < 0)
4245                                 return;
4246
4247                         original_rp_filter = filter_value;
4248                 }
4249                 connected_networks_count++;
4250
4251         } else {
4252                 if (connected_networks_count == 2)
4253                         __connman_ipconfig_unset_rp_filter(original_rp_filter);
4254
4255                 connected_networks_count--;
4256                 if (connected_networks_count < 0)
4257                         connected_networks_count = 0;
4258         }
4259
4260         DBG("%s %s ipconfig %p method %d count %d filter %d",
4261                 connected ? "connected" : "disconnected", service->identifier,
4262                 service->ipconfig_ipv4, method,
4263                 connected_networks_count, original_rp_filter);
4264 }
4265
4266 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4267                                         enum connman_service_state new_state,
4268                                         enum connman_ipconfig_type type)
4269 {
4270         struct connman_ipconfig *ipconfig = NULL;
4271         enum connman_service_state old_state;
4272         int ret;
4273
4274         if (service == NULL)
4275                 return -EINVAL;
4276
4277         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4278                 old_state = service->state_ipv4;
4279                 ipconfig = service->ipconfig_ipv4;
4280         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4281                 old_state = service->state_ipv6;
4282                 ipconfig = service->ipconfig_ipv6;
4283         }
4284
4285         if (ipconfig == NULL)
4286                 return -EINVAL;
4287
4288         /* Any change? */
4289         if (old_state == new_state)
4290                 return -EALREADY;
4291
4292         DBG("service %p (%s) state %d (%s) type %d (%s)",
4293                 service, service ? service->identifier : NULL,
4294                 new_state, state2string(new_state),
4295                 type, __connman_ipconfig_type2string(type));
4296
4297         switch (new_state) {
4298         case CONNMAN_SERVICE_STATE_UNKNOWN:
4299         case CONNMAN_SERVICE_STATE_IDLE:
4300                 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4301                         return -EINVAL;
4302                 break;
4303         case CONNMAN_SERVICE_STATE_ASSOCIATION:
4304                 break;
4305         case CONNMAN_SERVICE_STATE_CONFIGURATION:
4306                 __connman_ipconfig_enable(ipconfig);
4307                 break;
4308         case CONNMAN_SERVICE_STATE_READY:
4309                 update_nameservers(service);
4310
4311                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4312                         check_proxy_setup(service);
4313                         service_rp_filter(service, TRUE);
4314                 } else
4315                         __connman_wispr_start(service, type);
4316                 break;
4317         case CONNMAN_SERVICE_STATE_ONLINE:
4318                 break;
4319         case CONNMAN_SERVICE_STATE_DISCONNECT:
4320                 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4321                         return -EINVAL;
4322
4323                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4324                         service_rp_filter(service, FALSE);
4325
4326                 break;
4327         case CONNMAN_SERVICE_STATE_FAILURE:
4328                 break;
4329         }
4330
4331         /* We keep that state */
4332         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4333                 service->state_ipv4 = new_state;
4334         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4335                 service->state_ipv6 = new_state;
4336
4337         ret = service_indicate_state(service);
4338
4339         /*
4340          * If the ipconfig method is OFF, then we set the state to IDLE
4341          * so that it will not affect the combined state in the future.
4342          */
4343         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4344                 enum connman_ipconfig_method method;
4345                 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4346                 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4347                                 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4348                         service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4349                         ret = service_indicate_state(service);
4350                 }
4351
4352         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4353                 enum connman_ipconfig_method method;
4354                 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4355                 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4356                                 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4357                         service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4358                         ret = service_indicate_state(service);
4359                 }
4360         }
4361
4362         return ret;
4363 }
4364
4365 int __connman_service_request_login(struct connman_service *service)
4366 {
4367         DBG("service %p", service);
4368
4369         if (service == NULL)
4370                 return -EINVAL;
4371
4372         service->login_required = TRUE;
4373         login_changed(service);
4374
4375         return 0;
4376 }
4377
4378 static connman_bool_t prepare_network(struct connman_service *service)
4379 {
4380         enum connman_network_type type;
4381         unsigned int ssid_len;
4382
4383         type = connman_network_get_type(service->network);
4384
4385         switch (type) {
4386         case CONNMAN_NETWORK_TYPE_UNKNOWN:
4387         case CONNMAN_NETWORK_TYPE_VENDOR:
4388                 return FALSE;
4389         case CONNMAN_NETWORK_TYPE_WIFI:
4390                 if (connman_network_get_blob(service->network, "WiFi.SSID",
4391                                                         &ssid_len) == NULL)
4392                         return FALSE;
4393
4394                 if (service->passphrase != NULL)
4395                         connman_network_set_string(service->network,
4396                                 "WiFi.Passphrase", service->passphrase);
4397                 break;
4398         case CONNMAN_NETWORK_TYPE_ETHERNET:
4399         case CONNMAN_NETWORK_TYPE_WIMAX:
4400         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4401         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4402         case CONNMAN_NETWORK_TYPE_CELLULAR:
4403                 break;
4404         }
4405
4406         return TRUE;
4407 }
4408
4409 static void prepare_8021x(struct connman_service *service)
4410 {
4411         if (service->eap != NULL)
4412                 connman_network_set_string(service->network, "WiFi.EAP",
4413                                                                 service->eap);
4414
4415         if (service->identity != NULL)
4416                 connman_network_set_string(service->network, "WiFi.Identity",
4417                                                         service->identity);
4418
4419         if (service->ca_cert_file != NULL)
4420                 connman_network_set_string(service->network, "WiFi.CACertFile",
4421                                                         service->ca_cert_file);
4422
4423         if (service->client_cert_file != NULL)
4424                 connman_network_set_string(service->network,
4425                                                 "WiFi.ClientCertFile",
4426                                                 service->client_cert_file);
4427
4428         if (service->private_key_file != NULL)
4429                 connman_network_set_string(service->network,
4430                                                 "WiFi.PrivateKeyFile",
4431                                                 service->private_key_file);
4432
4433         if (service->private_key_passphrase != NULL)
4434                 connman_network_set_string(service->network,
4435                                         "WiFi.PrivateKeyPassphrase",
4436                                         service->private_key_passphrase);
4437
4438         if (service->phase2 != NULL)
4439                 connman_network_set_string(service->network, "WiFi.Phase2",
4440                                                         service->phase2);
4441 }
4442
4443 static int service_connect(struct connman_service *service)
4444 {
4445         int err;
4446
4447         switch (service->type) {
4448         case CONNMAN_SERVICE_TYPE_UNKNOWN:
4449         case CONNMAN_SERVICE_TYPE_SYSTEM:
4450         case CONNMAN_SERVICE_TYPE_GPS:
4451         case CONNMAN_SERVICE_TYPE_GADGET:
4452                 return -EINVAL;
4453         case CONNMAN_SERVICE_TYPE_ETHERNET:
4454         case CONNMAN_SERVICE_TYPE_WIMAX:
4455         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4456         case CONNMAN_SERVICE_TYPE_CELLULAR:
4457         case CONNMAN_SERVICE_TYPE_VPN:
4458                 break;
4459         case CONNMAN_SERVICE_TYPE_WIFI:
4460                 switch (service->security) {
4461                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4462                 case CONNMAN_SERVICE_SECURITY_NONE:
4463                         break;
4464                 case CONNMAN_SERVICE_SECURITY_WEP:
4465                 case CONNMAN_SERVICE_SECURITY_PSK:
4466                 case CONNMAN_SERVICE_SECURITY_WPA:
4467                 case CONNMAN_SERVICE_SECURITY_RSN:
4468                         if (service->passphrase == NULL) {
4469                                 if (service->network == NULL)
4470                                         return -EOPNOTSUPP;
4471
4472                                 if (service->wps == FALSE ||
4473                                         connman_network_get_bool(
4474                                                         service->network,
4475                                                         "WiFi.UseWPS") == FALSE)
4476                                         return -ENOKEY;
4477                         }
4478                         break;
4479                 case CONNMAN_SERVICE_SECURITY_8021X:
4480                         if (service->eap == NULL)
4481                                 return -EINVAL;
4482
4483                         /*
4484                          * never request credentials if using EAP-TLS
4485                          * (EAP-TLS networks need to be fully provisioned)
4486                          */
4487                         if (g_str_equal(service->eap, "tls") == TRUE)
4488                                 break;
4489
4490                         /*
4491                          * Return -ENOKEY if either identity or passphrase is
4492                          * missing. Agent provided credentials can be used as
4493                          * fallback if needed.
4494                          */
4495                         if ((service->identity == NULL &&
4496                                         service->agent_identity == NULL) ||
4497                                         (service->passphrase == NULL &&
4498                                         service->agent_passphrase == NULL))
4499                                 return -ENOKEY;
4500
4501                         break;
4502                 }
4503                 break;
4504         }
4505
4506         if (service->network != NULL) {
4507                 if (prepare_network(service) == FALSE)
4508                         return -EINVAL;
4509
4510                 switch (service->security) {
4511                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4512                 case CONNMAN_SERVICE_SECURITY_NONE:
4513                 case CONNMAN_SERVICE_SECURITY_WEP:
4514                 case CONNMAN_SERVICE_SECURITY_PSK:
4515                 case CONNMAN_SERVICE_SECURITY_WPA:
4516                 case CONNMAN_SERVICE_SECURITY_RSN:
4517                         break;
4518                 case CONNMAN_SERVICE_SECURITY_8021X:
4519                         prepare_8021x(service);
4520                         break;
4521                 }
4522
4523                 if (__connman_stats_service_register(service) == 0) {
4524                         __connman_stats_get(service, FALSE,
4525                                                 &service->stats.data);
4526                         __connman_stats_get(service, TRUE,
4527                                                 &service->stats_roaming.data);
4528                 }
4529
4530                 if (service->ipconfig_ipv4)
4531                         __connman_ipconfig_enable(service->ipconfig_ipv4);
4532                 if (service->ipconfig_ipv6)
4533                         __connman_ipconfig_enable(service->ipconfig_ipv6);
4534
4535                 err = __connman_network_connect(service->network);
4536         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4537                                         service->provider != NULL)
4538                 err = __connman_provider_connect(service->provider);
4539         else
4540                 return -EOPNOTSUPP;
4541
4542         if (err < 0) {
4543                 if (err != -EINPROGRESS) {
4544                         __connman_ipconfig_disable(service->ipconfig_ipv4);
4545                         __connman_ipconfig_disable(service->ipconfig_ipv6);
4546                         __connman_stats_service_unregister(service);
4547                 }
4548         }
4549
4550         return err;
4551 }
4552
4553
4554 int __connman_service_connect(struct connman_service *service)
4555 {
4556         int err;
4557
4558         DBG("service %p state %s", service, state2string(service->state));
4559
4560         if (is_connected(service) == TRUE)
4561                 return -EISCONN;
4562
4563         if (is_connecting(service) == TRUE)
4564                 return -EALREADY;
4565
4566         switch (service->type) {
4567         case CONNMAN_SERVICE_TYPE_UNKNOWN:
4568         case CONNMAN_SERVICE_TYPE_SYSTEM:
4569         case CONNMAN_SERVICE_TYPE_GPS:
4570         case CONNMAN_SERVICE_TYPE_GADGET:
4571                 return -EINVAL;
4572         default:
4573                 err = service_connect(service);
4574         }
4575
4576         if (err >= 0)
4577                 return 0;
4578
4579         if (err == -EINPROGRESS) {
4580                 if (service->timeout == 0)
4581                         service->timeout = g_timeout_add_seconds(
4582                                 CONNECT_TIMEOUT, connect_timeout, service);
4583
4584                 return -EINPROGRESS;
4585         }
4586
4587         __connman_service_ipconfig_indicate_state(service,
4588                                         CONNMAN_SERVICE_STATE_FAILURE,
4589                                         CONNMAN_IPCONFIG_TYPE_IPV4);
4590         __connman_service_ipconfig_indicate_state(service,
4591                                         CONNMAN_SERVICE_STATE_FAILURE,
4592                                         CONNMAN_IPCONFIG_TYPE_IPV6);
4593
4594         if (service->network != NULL)
4595                 __connman_network_disconnect(service->network);
4596         else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4597                                 service->provider != NULL)
4598                         __connman_provider_disconnect(service->provider);
4599
4600         if (service->userconnect == TRUE) {
4601                 if (err == -ENOKEY) {
4602                         if (__connman_agent_request_passphrase_input(service,
4603                                                         request_input_cb,
4604                                                         NULL) == -EIO)
4605                                 return -EINPROGRESS;
4606                 }
4607                 reply_pending(service, err);
4608         }
4609
4610         return err;
4611 }
4612
4613 int __connman_service_disconnect(struct connman_service *service)
4614 {
4615         int err;
4616
4617         DBG("service %p", service);
4618
4619         if (service->network != NULL) {
4620                 err = __connman_network_disconnect(service->network);
4621         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4622                                         service->provider != NULL)
4623                 err = __connman_provider_disconnect(service->provider);
4624         else
4625                 return -EOPNOTSUPP;
4626
4627         if (err < 0 && err != -EINPROGRESS)
4628                 return err;
4629
4630         __connman_6to4_remove(service->ipconfig_ipv4);
4631
4632         if (service->ipconfig_ipv4)
4633                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4634                                                         NULL);
4635         else
4636                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4637                                                         NULL);
4638
4639         __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4640         __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4641
4642         __connman_ipconfig_disable(service->ipconfig_ipv4);
4643         __connman_ipconfig_disable(service->ipconfig_ipv6);
4644
4645         __connman_stats_service_unregister(service);
4646
4647         return err;
4648 }
4649
4650 int __connman_service_disconnect_all(void)
4651 {
4652         GSequenceIter *iter;
4653         GSList *services = NULL, *list;
4654
4655         DBG("");
4656
4657         iter = g_sequence_get_begin_iter(service_list);
4658
4659         while (g_sequence_iter_is_end(iter) == FALSE) {
4660                 struct connman_service *service = g_sequence_get(iter);
4661
4662                 services = g_slist_prepend(services, service);
4663
4664                 iter = g_sequence_iter_next(iter);
4665         }
4666
4667         for (list = services; list != NULL; list = list->next) {
4668                 struct connman_service *service = list->data;
4669
4670                 service->ignore = TRUE;
4671
4672                 set_reconnect_state(service, FALSE);
4673
4674                 __connman_service_disconnect(service);
4675         }
4676
4677         g_slist_free(list);
4678
4679         return 0;
4680
4681 }
4682
4683 /**
4684  * __connman_service_lookup:
4685  * @pattern: search pattern
4686  * @path: return object path
4687  *
4688  * Look up a service path from a search pattern
4689  */
4690 int __connman_service_lookup(const char *pattern, const char **path)
4691 {
4692         GHashTableIter iter;
4693         gpointer key, value;
4694         struct connman_device *device;
4695         const char *ifname;
4696
4697         g_hash_table_iter_init(&iter, service_hash);
4698
4699         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4700                 GSequenceIter *iter = value;
4701                 struct connman_service *service = g_sequence_get(iter);
4702
4703                 if (g_strcmp0(service->identifier, pattern) == 0 ||
4704                                 g_strcmp0(service->name, pattern) == 0) {
4705                         *path = (const char *) service->path;
4706                         return 0;
4707                 }
4708
4709                 if (service->network == NULL)
4710                         continue;
4711
4712                 device = connman_network_get_device(service->network);
4713                 if (device == NULL)
4714                         continue;
4715
4716                 ifname = connman_device_get_string(device, "Interface");
4717                 if (ifname != NULL && g_strcmp0(ifname, pattern) == 0) {
4718                         *path = (const char *) service->path;
4719                         return 0;
4720                 }
4721
4722         }
4723
4724         return -ENXIO;
4725 }
4726
4727 /**
4728  * lookup_by_identifier:
4729  * @identifier: service identifier
4730  *
4731  * Look up a service by identifier (reference count will not be increased)
4732  */
4733 static struct connman_service *lookup_by_identifier(const char *identifier)
4734 {
4735         GSequenceIter *iter;
4736
4737         iter = g_hash_table_lookup(service_hash, identifier);
4738         if (iter != NULL)
4739                 return g_sequence_get(iter);
4740
4741         return NULL;
4742 }
4743
4744 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4745                 const char *ssid, const char *mode, const char *security,
4746                 const char *group)
4747 {
4748         struct connman_network *network;
4749         char *name;
4750         int index;
4751         unsigned int i, ssid_len;
4752
4753         ssid_len = strlen(ssid);
4754         if (ssid_len < 1)
4755                 return NULL;
4756
4757         network = connman_network_create(group, CONNMAN_NETWORK_TYPE_WIFI);
4758         if (network == NULL)
4759                 return NULL;
4760
4761         connman_network_set_blob(network, "WiFi.SSID",
4762                                         (unsigned char *) ssid, ssid_len);
4763
4764         connman_network_set_string(network, "WiFi.Mode", mode);
4765         connman_network_set_string(network, "WiFi.Security", security);
4766
4767         name = g_try_malloc0(ssid_len + 1);
4768         if (name == NULL) {
4769                 connman_network_unref(network);
4770                 return NULL;
4771         }
4772
4773         for (i = 0; i < ssid_len; i++) {
4774                 if (g_ascii_isprint(ssid[i]))
4775                         name[i] = ssid[i];
4776                 else
4777                         name[i] = ' ';
4778         }
4779
4780         connman_network_set_name(network, name);
4781
4782         g_free(name);
4783
4784         index = connman_device_get_index(device);
4785         connman_network_set_index(network, index);
4786
4787         if (connman_device_add_network(device, network) < 0) {
4788                 connman_network_unref(network);
4789                 return NULL;
4790         }
4791
4792         connman_network_set_available(network, TRUE);
4793
4794         return network;
4795 }
4796
4797 int __connman_service_create_and_connect(DBusMessage *msg)
4798 {
4799         struct connman_service *service;
4800         struct connman_network *network;
4801         struct connman_device *device;
4802         DBusMessageIter iter, array;
4803         const char *mode = "managed", *security = "none", *group_security;
4804         const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4805         connman_bool_t network_created = FALSE;
4806         unsigned int ssid_len = 0;
4807         const char *ident;
4808         char *name, *group;
4809         int err;
4810
4811         dbus_message_iter_init(msg, &iter);
4812         dbus_message_iter_recurse(&iter, &array);
4813
4814         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4815                 DBusMessageIter entry, value;
4816                 const char *key;
4817
4818                 dbus_message_iter_recurse(&array, &entry);
4819                 dbus_message_iter_get_basic(&entry, &key);
4820
4821                 dbus_message_iter_next(&entry);
4822                 dbus_message_iter_recurse(&entry, &value);
4823
4824                 switch (dbus_message_iter_get_arg_type(&value)) {
4825                 case DBUS_TYPE_STRING:
4826                         if (g_str_equal(key, "Type") == TRUE)
4827                                 dbus_message_iter_get_basic(&value, &type);
4828                         else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4829                                         g_str_equal(key, "Mode") == TRUE)
4830                                 dbus_message_iter_get_basic(&value, &mode);
4831                         else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4832                                         g_str_equal(key, "Security") == TRUE)
4833                                 dbus_message_iter_get_basic(&value, &security);
4834                         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4835                                         g_str_equal(key, "Passphrase") == TRUE)
4836                                 dbus_message_iter_get_basic(&value, &passphrase);
4837                         else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4838                                         g_str_equal(key, "SSID") == TRUE)
4839                                 dbus_message_iter_get_basic(&value, &ssid);
4840                 }
4841
4842                 dbus_message_iter_next(&array);
4843         }
4844
4845         if (type == NULL)
4846                 return -EINVAL;
4847
4848         if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4849                 return -EOPNOTSUPP;
4850
4851         if (ssid == NULL)
4852                 return -EINVAL;
4853
4854         ssid_len = strlen(ssid);
4855         if (ssid_len < 1)
4856                 return -EINVAL;
4857
4858         if (g_strcmp0(security, "none") != 0 &&
4859                                 g_strcmp0(security, "wep") != 0 &&
4860                                 g_strcmp0(security, "psk") != 0 &&
4861                                 g_strcmp0(security, "wpa") != 0 &&
4862                                 g_strcmp0(security, "rsn") != 0 &&
4863                                 g_strcmp0(security, "ieee8021x") != 0)
4864                 return -EINVAL;
4865
4866         device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4867         if (device == NULL)
4868                 return -EOPNOTSUPP;
4869
4870         ident = connman_device_get_ident(device);
4871         if (ident == NULL)
4872                 return -EOPNOTSUPP;
4873
4874
4875         if (!g_strcmp0(security, "wpa") ||
4876                 !g_strcmp0(security, "rsn"))
4877                 group_security = "psk";
4878         else
4879                 group_security = security;
4880
4881         group = wifi_build_group_name((unsigned char *) ssid,
4882                                                 ssid_len, mode, group_security);
4883         if (group == NULL)
4884                 return -EINVAL;
4885
4886         name = g_strdup_printf("%s_%s_%s", type, ident, group);
4887
4888         service = lookup_by_identifier(name);
4889
4890         if (service == NULL) {
4891                 network = create_hidden_wifi(device, ssid,
4892                                                 mode, security, group);
4893                 if (network != NULL) {
4894                         connman_network_set_group(network, group);
4895                         network_created = TRUE;
4896                 }
4897
4898                 service = lookup_by_identifier(name);
4899         }
4900
4901         g_free(name);
4902         g_free(group);
4903
4904         if (service == NULL) {
4905                 err = -EOPNOTSUPP;
4906                 goto failed;
4907         }
4908
4909         service->network_created = network_created;
4910
4911         if (is_connected(service) == TRUE) {
4912                 err = -EISCONN;
4913                 goto failed;
4914         }
4915
4916         if (is_connecting(service) == TRUE) {
4917                 err = -EALREADY;
4918                 goto failed;
4919         }
4920
4921         set_reconnect_state(service, FALSE);
4922
4923         __connman_device_disconnect(device);
4924
4925         if (passphrase != NULL) {
4926                 g_free(service->passphrase);
4927                 service->passphrase = g_strdup(passphrase);
4928         }
4929
4930         service->userconnect = TRUE;
4931
4932         err = __connman_service_connect(service);
4933         if (err < 0 && err != -EINPROGRESS)
4934                 goto failed;
4935
4936         service->pending = dbus_message_ref(msg);
4937
4938         return 0;
4939
4940 failed:
4941         if (service != NULL && service->network_created == TRUE) {
4942                 struct connman_network *network = service->network;
4943
4944                 if (network != NULL) {
4945                         connman_network_set_available(network, FALSE);
4946                         __connman_device_cleanup_networks(device);
4947                 } else
4948                         __connman_service_put(service);
4949         }
4950
4951         return err;
4952 }
4953
4954 static void provision_changed(gpointer value, gpointer user_data)
4955 {
4956         struct connman_service *service = value;
4957         char *path = user_data;
4958
4959         __connman_config_provision_service_ident(service, path);
4960 }
4961
4962 void __connman_service_provision_changed(const char *ident)
4963 {
4964         g_sequence_foreach(service_list, provision_changed, (void *)ident);
4965 }
4966
4967 int __connman_service_provision(DBusMessage *msg)
4968 {
4969         GKeyFile *keyfile = NULL;
4970         const char *config_str = NULL;
4971         char *group = NULL, *ident = NULL;
4972         int err = 0;
4973         struct connman_service *service;
4974
4975         DBG("");
4976
4977         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4978                                                         DBUS_TYPE_INVALID);
4979
4980         if (config_str == NULL || strlen(config_str) == 0)
4981                 return -EINVAL;
4982
4983         keyfile = g_key_file_new();
4984
4985         /* populate GKeyFile with config_str */
4986         if (g_key_file_load_from_data(keyfile, config_str,
4987                                         strlen(config_str), 0, NULL) == FALSE) {
4988                 err = -EINVAL;
4989                 goto done;
4990         }
4991
4992         /*
4993          * read only one group of settings (only one service supported, no
4994          * global settings)
4995          */
4996         group = g_key_file_get_start_group(keyfile);
4997
4998         if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4999                 err = -EINVAL;
5000                 goto done;
5001         }
5002
5003         err = __connman_config_load_service(keyfile, group, TRUE);
5004         if (err < 0)
5005                 goto done;
5006
5007         ident = group + strlen("service_");
5008
5009         /* trigger service provisioning if service exists */
5010         service = lookup_by_identifier(ident);
5011         if (service != NULL)
5012                 __connman_config_provision_service(service);
5013
5014         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
5015
5016 done:
5017         if (group != NULL)
5018                 g_free(group);
5019
5020         if (keyfile != NULL)
5021                 g_key_file_free(keyfile);
5022
5023         return err;
5024 }
5025
5026 /**
5027  * __connman_service_get:
5028  * @identifier: service identifier
5029  *
5030  * Look up a service by identifier or create a new one if not found
5031  */
5032 static struct connman_service *service_get(const char *identifier)
5033 {
5034         struct connman_service *service;
5035         GSequenceIter *iter;
5036
5037         iter = g_hash_table_lookup(service_hash, identifier);
5038         if (iter != NULL) {
5039                 service = g_sequence_get(iter);
5040                 if (service != NULL)
5041                         connman_service_ref(service);
5042                 return service;
5043         }
5044
5045         service = connman_service_create();
5046         if (service == NULL)
5047                 return NULL;
5048
5049         DBG("service %p", service);
5050
5051         service->identifier = g_strdup(identifier);
5052
5053         iter = g_sequence_insert_sorted(service_list, service,
5054                                                 service_compare, NULL);
5055
5056         g_hash_table_insert(service_hash, service->identifier, iter);
5057
5058         return service;
5059 }
5060
5061 static int service_register(struct connman_service *service)
5062 {
5063         GSequenceIter *iter;
5064
5065         DBG("service %p", service);
5066
5067         if (service->path != NULL)
5068                 return -EALREADY;
5069
5070         service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
5071                                                 service->identifier);
5072
5073         DBG("path %s", service->path);
5074
5075         __connman_config_provision_service(service);
5076
5077         service_load(service);
5078
5079         g_dbus_register_interface(connection, service->path,
5080                                         CONNMAN_SERVICE_INTERFACE,
5081                                         service_methods, service_signals,
5082                                                         NULL, service, NULL);
5083
5084         iter = g_hash_table_lookup(service_hash, service->identifier);
5085         if (iter != NULL)
5086                 g_sequence_sort_changed(iter, service_compare, NULL);
5087
5088         services_changed(TRUE);
5089
5090         return 0;
5091 }
5092
5093 static void service_up(struct connman_ipconfig *ipconfig)
5094 {
5095         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5096
5097         DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
5098
5099         link_changed(service);
5100
5101         service->stats.valid = FALSE;
5102         service->stats_roaming.valid = FALSE;
5103 }
5104
5105 static void service_down(struct connman_ipconfig *ipconfig)
5106 {
5107         DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
5108 }
5109
5110 static void service_lower_up(struct connman_ipconfig *ipconfig)
5111 {
5112         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5113
5114         DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
5115
5116         stats_start(service);
5117 }
5118
5119 static void service_lower_down(struct connman_ipconfig *ipconfig)
5120 {
5121         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5122
5123         DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
5124
5125         stats_stop(service);
5126         service_save(service);
5127 }
5128
5129 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5130 {
5131         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5132         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5133         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5134
5135         DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
5136
5137         type = __connman_ipconfig_get_config_type(ipconfig);
5138         method = __connman_ipconfig_get_method(ipconfig);
5139
5140         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5141                                                         type, method);
5142
5143         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5144                         method == CONNMAN_IPCONFIG_METHOD_AUTO)
5145                 __connman_service_ipconfig_indicate_state(service,
5146                                                 CONNMAN_SERVICE_STATE_READY,
5147                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
5148
5149         settings_changed(service, ipconfig);
5150 }
5151
5152 static void service_ip_release(struct connman_ipconfig *ipconfig)
5153 {
5154         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5155         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5156         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5157
5158         DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
5159
5160         type = __connman_ipconfig_get_config_type(ipconfig);
5161         method = __connman_ipconfig_get_method(ipconfig);
5162
5163         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5164                                                         type, method);
5165
5166         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5167                         method == CONNMAN_IPCONFIG_METHOD_OFF)
5168                 __connman_service_ipconfig_indicate_state(service,
5169                                         CONNMAN_SERVICE_STATE_DISCONNECT,
5170                                         CONNMAN_IPCONFIG_TYPE_IPV6);
5171
5172         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5173                         method == CONNMAN_IPCONFIG_METHOD_OFF)
5174                 __connman_service_ipconfig_indicate_state(service,
5175                                         CONNMAN_SERVICE_STATE_DISCONNECT,
5176                                         CONNMAN_IPCONFIG_TYPE_IPV4);
5177
5178         settings_changed(service, ipconfig);
5179 }
5180
5181 static const struct connman_ipconfig_ops service_ops = {
5182         .up             = service_up,
5183         .down           = service_down,
5184         .lower_up       = service_lower_up,
5185         .lower_down     = service_lower_down,
5186         .ip_bound       = service_ip_bound,
5187         .ip_release     = service_ip_release,
5188 };
5189
5190 static void setup_ip4config(struct connman_service *service, int index,
5191                         enum connman_ipconfig_method method)
5192 {
5193         if (index < 0)
5194                 return;
5195
5196         service->ipconfig_ipv4 = connman_ipconfig_create(index,
5197                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
5198         if (service->ipconfig_ipv4 == NULL)
5199                 return;
5200
5201         connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5202
5203         connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5204
5205         connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5206 }
5207
5208 static void setup_ip6config(struct connman_service *service, int index)
5209 {
5210         if (index < 0)
5211                 return;
5212
5213         service->ipconfig_ipv6 = connman_ipconfig_create(index,
5214                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
5215         if (service->ipconfig_ipv6 == NULL)
5216                 return;
5217
5218         connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5219
5220         connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5221 }
5222
5223 void __connman_service_read_ip4config(struct connman_service *service)
5224 {
5225         GKeyFile *keyfile;
5226
5227         if (service->ipconfig_ipv4 == NULL)
5228                 return;
5229
5230         keyfile = connman_storage_load_service(service->identifier);
5231         if (keyfile == NULL)
5232                 return;
5233
5234         __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5235                                 service->identifier, "IPv4.");
5236
5237         g_key_file_free(keyfile);
5238 }
5239
5240 void __connman_service_create_ip4config(struct connman_service *service,
5241                                         int index)
5242 {
5243         DBG("ipv4 %p", service->ipconfig_ipv4);
5244
5245         if (service->ipconfig_ipv4 != NULL)
5246                 return;
5247
5248         setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5249         __connman_service_read_ip4config(service);
5250 }
5251
5252 void __connman_service_read_ip6config(struct connman_service *service)
5253 {
5254         GKeyFile *keyfile;
5255
5256         if (service->ipconfig_ipv6 == NULL)
5257                 return;
5258
5259         keyfile = connman_storage_load_service(service->identifier);
5260         if (keyfile == NULL)
5261                 return;
5262
5263         __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5264                                 service->identifier, "IPv6.");
5265
5266         g_key_file_free(keyfile);
5267 }
5268
5269 void __connman_service_create_ip6config(struct connman_service *service,
5270                                                                 int index)
5271 {
5272         DBG("ipv6 %p", service->ipconfig_ipv6);
5273
5274         if (service->ipconfig_ipv6 != NULL)
5275                 return;
5276
5277         setup_ip6config(service, index);
5278
5279         __connman_service_read_ip6config(service);
5280 }
5281
5282 /**
5283  * __connman_service_lookup_from_network:
5284  * @network: network structure
5285  *
5286  * Look up a service by network (reference count will not be increased)
5287  */
5288 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5289 {
5290         struct connman_service *service;
5291         const char *ident, *group;
5292         char *name;
5293
5294         DBG("network %p", network);
5295
5296         if (network == NULL)
5297                 return NULL;
5298
5299         ident = __connman_network_get_ident(network);
5300         if (ident == NULL)
5301                 return NULL;
5302
5303         group = connman_network_get_group(network);
5304         if (group == NULL)
5305                 return NULL;
5306
5307         name = g_strdup_printf("%s_%s_%s",
5308                         __connman_network_get_type(network), ident, group);
5309         service = lookup_by_identifier(name);
5310         g_free(name);
5311
5312         return service;
5313 }
5314
5315 struct connman_service *__connman_service_lookup_from_index(int index)
5316 {
5317         struct connman_service *service;
5318         GSequenceIter *iter;
5319
5320         iter = g_sequence_get_begin_iter(service_list);
5321
5322         while (g_sequence_iter_is_end(iter) == FALSE) {
5323                 service = g_sequence_get(iter);
5324
5325                 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
5326                                                         == index)
5327                         return service;
5328
5329                 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
5330                                                         == index)
5331                         return service;
5332
5333                 iter = g_sequence_iter_next(iter);
5334         }
5335
5336         return NULL;
5337 }
5338
5339 const char *__connman_service_get_ident(struct connman_service *service)
5340 {
5341         return service->identifier;
5342 }
5343
5344 const char *__connman_service_get_path(struct connman_service *service)
5345 {
5346         return service->path;
5347 }
5348
5349 unsigned int __connman_service_get_order(struct connman_service *service)
5350 {
5351         GSequenceIter *iter;
5352
5353         if (service == NULL)
5354                 return 0;
5355
5356         if (service->favorite == FALSE) {
5357                 service->order = 0;
5358                 goto done;
5359         }
5360
5361         iter = g_hash_table_lookup(service_hash, service->identifier);
5362         if (iter != NULL) {
5363                 if (g_sequence_iter_get_position(iter) == 0)
5364                         service->order = 1;
5365                 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5366                         service->order = 10;
5367                 else
5368                         service->order = 0;
5369         }
5370
5371 done:
5372         return service->order;
5373 }
5374
5375 static enum connman_service_type convert_network_type(struct connman_network *network)
5376 {
5377         enum connman_network_type type = connman_network_get_type(network);
5378
5379         switch (type) {
5380         case CONNMAN_NETWORK_TYPE_UNKNOWN:
5381         case CONNMAN_NETWORK_TYPE_VENDOR:
5382                 break;
5383         case CONNMAN_NETWORK_TYPE_ETHERNET:
5384                 return CONNMAN_SERVICE_TYPE_ETHERNET;
5385         case CONNMAN_NETWORK_TYPE_WIFI:
5386                 return CONNMAN_SERVICE_TYPE_WIFI;
5387         case CONNMAN_NETWORK_TYPE_WIMAX:
5388                 return CONNMAN_SERVICE_TYPE_WIMAX;
5389         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5390         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5391                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5392         case CONNMAN_NETWORK_TYPE_CELLULAR:
5393                 return CONNMAN_SERVICE_TYPE_CELLULAR;
5394         }
5395
5396         return CONNMAN_SERVICE_TYPE_UNKNOWN;
5397 }
5398
5399 static enum connman_service_security convert_wifi_security(const char *security)
5400 {
5401         if (security == NULL)
5402                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5403         else if (g_str_equal(security, "none") == TRUE)
5404                 return CONNMAN_SERVICE_SECURITY_NONE;
5405         else if (g_str_equal(security, "wep") == TRUE)
5406                 return CONNMAN_SERVICE_SECURITY_WEP;
5407         else if (g_str_equal(security, "psk") == TRUE)
5408                 return CONNMAN_SERVICE_SECURITY_PSK;
5409         else if (g_str_equal(security, "ieee8021x") == TRUE)
5410                 return CONNMAN_SERVICE_SECURITY_8021X;
5411         else if (g_str_equal(security, "wpa") == TRUE)
5412                 return CONNMAN_SERVICE_SECURITY_WPA;
5413         else if (g_str_equal(security, "rsn") == TRUE)
5414                 return CONNMAN_SERVICE_SECURITY_RSN;
5415         else
5416                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5417 }
5418
5419 static void update_from_network(struct connman_service *service,
5420                                         struct connman_network *network)
5421 {
5422         connman_uint8_t strength = service->strength;
5423         GSequenceIter *iter;
5424         const char *str;
5425
5426         DBG("service %p network %p", service, network);
5427
5428         if (is_connected(service) == TRUE)
5429                 return;
5430
5431         if (is_connecting(service) == TRUE)
5432                 return;
5433
5434         str = connman_network_get_string(network, "Name");
5435         if (str != NULL) {
5436                 g_free(service->name);
5437                 service->name = g_strdup(str);
5438                 service->hidden = FALSE;
5439         } else {
5440                 g_free(service->name);
5441                 service->name = NULL;
5442                 service->hidden = TRUE;
5443         }
5444
5445         service->strength = connman_network_get_strength(network);
5446         service->roaming = connman_network_get_bool(network, "Roaming");
5447
5448         if (service->strength == 0) {
5449                 /*
5450                  * Filter out 0-values; it's unclear what they mean
5451                  * and they cause anomalous sorting of the priority list.
5452                  */
5453                 service->strength = strength;
5454         }
5455
5456         str = connman_network_get_string(network, "WiFi.Security");
5457         service->security = convert_wifi_security(str);
5458
5459         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5460                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5461
5462         if (service->strength > strength && service->network != NULL) {
5463                 service->network = network;
5464
5465                 strength_changed(service);
5466         }
5467
5468         if (service->network == NULL)
5469                 service->network = network;
5470
5471         iter = g_hash_table_lookup(service_hash, service->identifier);
5472         if (iter != NULL)
5473                 g_sequence_sort_changed(iter, service_compare, NULL);
5474 }
5475
5476 /**
5477  * __connman_service_create_from_network:
5478  * @network: network structure
5479  *
5480  * Look up service by network and if not found, create one
5481  */
5482 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5483 {
5484         struct connman_service *service;
5485         struct connman_device *device;
5486         const char *ident, *group;
5487         char *name;
5488         int index;
5489
5490         DBG("network %p", network);
5491
5492         if (network == NULL)
5493                 return NULL;
5494
5495         ident = __connman_network_get_ident(network);
5496         if (ident == NULL)
5497                 return NULL;
5498
5499         group = connman_network_get_group(network);
5500         if (group == NULL)
5501                 return NULL;
5502
5503         name = g_strdup_printf("%s_%s_%s",
5504                         __connman_network_get_type(network), ident, group);
5505         service = service_get(name);
5506         g_free(name);
5507
5508         if (service == NULL)
5509                 return NULL;
5510
5511         if (__connman_network_get_weakness(network) == TRUE)
5512                 return service;
5513
5514         if (service->path != NULL) {
5515                 update_from_network(service, network);
5516                 services_changed(TRUE);
5517                 return service;
5518         }
5519
5520         service->type = convert_network_type(network);
5521
5522         switch (service->type) {
5523         case CONNMAN_SERVICE_TYPE_UNKNOWN:
5524         case CONNMAN_SERVICE_TYPE_SYSTEM:
5525         case CONNMAN_SERVICE_TYPE_ETHERNET:
5526         case CONNMAN_SERVICE_TYPE_WIMAX:
5527         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5528         case CONNMAN_SERVICE_TYPE_GPS:
5529         case CONNMAN_SERVICE_TYPE_VPN:
5530         case CONNMAN_SERVICE_TYPE_GADGET:
5531                 service->autoconnect = FALSE;
5532                 break;
5533         case CONNMAN_SERVICE_TYPE_WIFI:
5534         case CONNMAN_SERVICE_TYPE_CELLULAR:
5535                 service->autoconnect = TRUE;
5536                 break;
5537         }
5538
5539         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5540         service->state = combine_state(service->state_ipv4, service->state_ipv6);
5541
5542         update_from_network(service, network);
5543
5544         index = connman_network_get_index(network);
5545
5546         if (service->ipconfig_ipv4 == NULL)
5547                 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5548
5549         if (service->ipconfig_ipv6 == NULL)
5550                 setup_ip6config(service, index);
5551
5552         service_register(service);
5553
5554         if (service->favorite == TRUE) {
5555                 device = connman_network_get_device(service->network);
5556                 if (device && __connman_device_scanning(device) == FALSE)
5557                         __connman_service_auto_connect();
5558         }
5559
5560         __connman_notifier_service_add(service, service->name);
5561
5562         return service;
5563 }
5564
5565 void __connman_service_update_from_network(struct connman_network *network)
5566 {
5567         struct connman_service *service;
5568         connman_uint8_t strength;
5569         connman_bool_t roaming;
5570         GSequenceIter *iter;
5571         const char *name;
5572         connman_bool_t stats_enable;
5573
5574         DBG("network %p", network);
5575
5576         service = __connman_service_lookup_from_network(network);
5577         if (service == NULL)
5578                 return;
5579
5580         if (service->network == NULL)
5581                 return;
5582
5583         name = connman_network_get_string(service->network, "Name");
5584         if (g_strcmp0(service->name, name) != 0) {
5585                 g_free(service->name);
5586                 service->name = g_strdup(name);
5587                 connman_dbus_property_changed_basic(service->path,
5588                                 CONNMAN_SERVICE_INTERFACE, "Name",
5589                                 DBUS_TYPE_STRING, &service->name);
5590         }
5591
5592         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5593                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5594
5595         strength = connman_network_get_strength(service->network);
5596         if (strength == service->strength)
5597                 goto roaming;
5598
5599         service->strength = strength;
5600
5601         strength_changed(service);
5602
5603 roaming:
5604         roaming = connman_network_get_bool(service->network, "Roaming");
5605         if (roaming == service->roaming)
5606                 return;
5607
5608         stats_enable = stats_enabled(service);
5609         if (stats_enable == TRUE)
5610                 stats_stop(service);
5611
5612         service->roaming = roaming;
5613
5614         if (stats_enable == TRUE)
5615                 stats_start(service);
5616
5617         roaming_changed(service);
5618
5619         iter = g_hash_table_lookup(service_hash, service->identifier);
5620         if (iter != NULL)
5621                 g_sequence_sort_changed(iter, service_compare, NULL);
5622 }
5623
5624 void __connman_service_remove_from_network(struct connman_network *network)
5625 {
5626         struct connman_service *service;
5627
5628         DBG("network %p", network);
5629
5630         service = __connman_service_lookup_from_network(network);
5631         if (service == NULL)
5632                 return;
5633
5634         __connman_connection_gateway_remove(service,
5635                                         CONNMAN_IPCONFIG_TYPE_ALL);
5636
5637         __connman_service_put(service);
5638 }
5639
5640 /**
5641  * __connman_service_create_from_provider:
5642  * @provider: provider structure
5643  *
5644  * Look up service by provider and if not found, create one
5645  */
5646 struct connman_service *
5647 __connman_service_create_from_provider(struct connman_provider *provider)
5648 {
5649         struct connman_service *service;
5650         const char *ident, *str;
5651         char *name;
5652         int index = connman_provider_get_index(provider);
5653
5654         DBG("provider %p", provider);
5655
5656         ident = __connman_provider_get_ident(provider);
5657         if (ident == NULL)
5658                 return NULL;
5659
5660         name = g_strdup_printf("vpn_%s", ident);
5661         service = service_get(name);
5662         g_free(name);
5663
5664         if (service == NULL)
5665                 return NULL;
5666
5667         service->type = CONNMAN_SERVICE_TYPE_VPN;
5668         service->provider = connman_provider_ref(provider);
5669         service->autoconnect = FALSE;
5670         service->userconnect = TRUE;
5671
5672         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5673
5674         str = connman_provider_get_string(provider, "Name");
5675         if (str != NULL) {
5676                 g_free(service->name);
5677                 service->name = g_strdup(str);
5678                 service->hidden = FALSE;
5679         } else {
5680                 g_free(service->name);
5681                 service->name = NULL;
5682                 service->hidden = TRUE;
5683         }
5684
5685         service->strength = 0;
5686
5687         if (service->ipconfig_ipv4 == NULL)
5688                 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5689
5690         if (service->ipconfig_ipv6 == NULL)
5691                 setup_ip6config(service, index);
5692
5693         service_register(service);
5694
5695         __connman_notifier_service_add(service, service->name);
5696
5697         return service;
5698 }
5699
5700 void __connman_service_downgrade_state(struct connman_service *service)
5701 {
5702         if (service == NULL)
5703                 return;
5704
5705         DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
5706                                                 service->state_ipv6);
5707
5708         if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
5709                 __connman_service_ipconfig_indicate_state(service,
5710                                                 CONNMAN_SERVICE_STATE_READY,
5711                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
5712
5713         if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
5714                 __connman_service_ipconfig_indicate_state(service,
5715                                                 CONNMAN_SERVICE_STATE_READY,
5716                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
5717 }
5718
5719 int __connman_service_init(void)
5720 {
5721         DBG("");
5722
5723         connection = connman_dbus_get_connection();
5724
5725         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5726                                                                 NULL, NULL);
5727
5728         service_list = g_sequence_new(service_free);
5729
5730         return 0;
5731 }
5732
5733 void __connman_service_cleanup(void)
5734 {
5735         GSequence *list;
5736
5737         DBG("");
5738
5739         list = service_list;
5740         service_list = NULL;
5741         g_sequence_free(list);
5742
5743         g_hash_table_destroy(service_hash);
5744         service_hash = NULL;
5745
5746         g_slist_free(counter_list);
5747         counter_list = NULL;
5748
5749         dbus_connection_unref(connection);
5750 }