service: Make state downgrading routine static
[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 void downgrade_state(struct connman_service *service)
3289 {
3290         if (service == NULL)
3291                 return;
3292
3293         DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3294                                                 service->state_ipv6);
3295
3296         if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3297                 __connman_service_ipconfig_indicate_state(service,
3298                                                 CONNMAN_SERVICE_STATE_READY,
3299                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
3300
3301         if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3302                 __connman_service_ipconfig_indicate_state(service,
3303                                                 CONNMAN_SERVICE_STATE_READY,
3304                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
3305 }
3306
3307 static void apply_relevant_default_downgrade(struct connman_service *service)
3308 {
3309         struct connman_service *def_service;
3310
3311         def_service = get_default();
3312         if (def_service == NULL)
3313                 return;
3314
3315         if (def_service == service &&
3316                         def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3317                 def_service->state = CONNMAN_SERVICE_STATE_READY;
3318 }
3319
3320 static DBusMessage *move_service(DBusConnection *conn,
3321                                         DBusMessage *msg, void *user_data,
3322                                                                 gboolean before)
3323 {
3324         struct connman_service *service = user_data;
3325         struct connman_service *target;
3326         const char *path;
3327         GSequenceIter *src, *dst;
3328         enum connman_ipconfig_method target4, target6;
3329         enum connman_ipconfig_method service4, service6;
3330
3331         DBG("service %p", service);
3332
3333         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3334                                                         DBUS_TYPE_INVALID);
3335
3336         if (service->favorite == FALSE)
3337                 return __connman_error_not_supported(msg);
3338
3339         target = find_service(path);
3340         if (target == NULL || target->favorite == FALSE || target == service ||
3341                                 target->type == CONNMAN_SERVICE_TYPE_VPN)
3342                 return __connman_error_invalid_service(msg);
3343
3344         target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3345         target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3346         service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3347         service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3348
3349         DBG("target %s method %d/%d state %d/%d", target->identifier,
3350                                 target4, target6,
3351                                 target->state_ipv4, target->state_ipv6);
3352
3353         DBG("service %s method %d/%d state %d/%d", service->identifier,
3354                                 service4, service6,
3355                                 service->state_ipv4, service->state_ipv6);
3356
3357         /*
3358          * If method is OFF, then we do not need to check the corresponding
3359          * ipconfig state.
3360          */
3361         if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3362                 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3363                         if (check_suitable_state(target->state_ipv6,
3364                                                 service->state_ipv6) == FALSE)
3365                                 return __connman_error_invalid_service(msg);
3366                 }
3367         }
3368
3369         if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3370                 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3371                         if (check_suitable_state(target->state_ipv4,
3372                                                 service->state_ipv4) == FALSE)
3373                                 return __connman_error_invalid_service(msg);
3374                 }
3375         }
3376
3377         if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3378                 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3379                         if (check_suitable_state(target->state_ipv6,
3380                                                 service->state_ipv6) == FALSE)
3381                                 return __connman_error_invalid_service(msg);
3382                 }
3383         }
3384
3385         if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3386                 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3387                         if (check_suitable_state(target->state_ipv4,
3388                                                 service->state_ipv4) == FALSE)
3389                                 return __connman_error_invalid_service(msg);
3390                 }
3391         }
3392
3393         g_get_current_time(&service->modified);
3394         service_save(service);
3395
3396         src = g_hash_table_lookup(service_hash, service->identifier);
3397         dst = g_hash_table_lookup(service_hash, target->identifier);
3398
3399         /*
3400          * If the service which goes down is the default service and is
3401          * online, we downgrade directly its state to ready so:
3402          * the service which goes up, needs to recompute its state which
3403          * is triggered via downgrading it - if relevant - to state ready.
3404          */
3405         if (before == TRUE) {
3406                 apply_relevant_default_downgrade(target);
3407                 g_sequence_move(src, dst);
3408                 downgrade_state(service);
3409         } else {
3410                 apply_relevant_default_downgrade(service);
3411                 g_sequence_move(dst, src);
3412                 downgrade_state(target);
3413         }
3414
3415         services_changed(FALSE);
3416
3417         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3418 }
3419
3420 static DBusMessage *move_before(DBusConnection *conn,
3421                                         DBusMessage *msg, void *user_data)
3422 {
3423         return move_service(conn, msg, user_data, TRUE);
3424 }
3425
3426 static DBusMessage *move_after(DBusConnection *conn,
3427                                         DBusMessage *msg, void *user_data)
3428 {
3429         return move_service(conn, msg, user_data, FALSE);
3430 }
3431
3432 static DBusMessage *reset_counters(DBusConnection *conn,
3433                                         DBusMessage *msg, void *user_data)
3434 {
3435         struct connman_service *service = user_data;
3436
3437         reset_stats(service);
3438
3439         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3440 }
3441
3442 static GDBusMethodTable service_methods[] = {
3443         { "GetProperties", "",   "a{sv}", get_properties     },
3444         { "SetProperty",   "sv", "",      set_property       },
3445         { "ClearProperty", "s",  "",      clear_property     },
3446         { "Connect",       "",   "",      connect_service,
3447                                                 G_DBUS_METHOD_FLAG_ASYNC },
3448         { "Disconnect",    "",   "",      disconnect_service },
3449         { "Remove",        "",   "",      remove_service     },
3450         { "MoveBefore",    "o",  "",      move_before        },
3451         { "MoveAfter",     "o",  "",      move_after         },
3452         { "ResetCounters", "",   "",      reset_counters     },
3453         { },
3454 };
3455
3456 static GDBusSignalTable service_signals[] = {
3457         { "PropertyChanged", "sv" },
3458         { },
3459 };
3460
3461 static void service_free(gpointer user_data)
3462 {
3463         struct connman_service *service = user_data;
3464         char *path = service->path;
3465
3466         DBG("service %p", service);
3467
3468         reply_pending(service, ENOENT);
3469
3470         g_hash_table_remove(service_hash, service->identifier);
3471
3472         __connman_notifier_service_remove(service);
3473
3474         stats_stop(service);
3475
3476         service->path = NULL;
3477
3478         if (path != NULL) {
3479                 services_changed(FALSE);
3480
3481                 g_dbus_unregister_interface(connection, path,
3482                                                 CONNMAN_SERVICE_INTERFACE);
3483                 g_free(path);
3484         }
3485
3486         g_hash_table_destroy(service->counter_table);
3487
3488         if (service->network != NULL) {
3489                 if (service->network_created == TRUE)
3490                         connman_network_unref(service->network);
3491         }
3492
3493         if (service->provider != NULL)
3494                 connman_provider_unref(service->provider);
3495
3496         if (service->ipconfig_ipv4 != NULL) {
3497                 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3498                 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3499                 connman_ipconfig_unref(service->ipconfig_ipv4);
3500                 service->ipconfig_ipv4 = NULL;
3501         }
3502
3503         if (service->ipconfig_ipv6 != NULL) {
3504                 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3505                 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3506                 connman_ipconfig_unref(service->ipconfig_ipv6);
3507                 service->ipconfig_ipv6 = NULL;
3508         }
3509
3510         g_strfreev(service->nameservers);
3511         g_strfreev(service->nameservers_config);
3512         g_strfreev(service->nameservers_auto);
3513         g_strfreev(service->domains);
3514         g_strfreev(service->proxies);
3515         g_strfreev(service->excludes);
3516
3517         g_free(service->domainname);
3518         g_free(service->pac);
3519         g_free(service->name);
3520         g_free(service->passphrase);
3521         g_free(service->agent_passphrase);
3522         g_free(service->identifier);
3523         g_free(service->eap);
3524         g_free(service->identity);
3525         g_free(service->agent_identity);
3526         g_free(service->ca_cert_file);
3527         g_free(service->client_cert_file);
3528         g_free(service->private_key_file);
3529         g_free(service->private_key_passphrase);
3530         g_free(service->phase2);
3531
3532         if (service->stats.timer != NULL)
3533                 g_timer_destroy(service->stats.timer);
3534         if (service->stats_roaming.timer != NULL)
3535                 g_timer_destroy(service->stats_roaming.timer);
3536
3537         g_free(service);
3538 }
3539
3540 /**
3541  * __connman_service_put:
3542  * @service: service structure
3543  *
3544  * Release service if no longer needed
3545  */
3546 void __connman_service_put(struct connman_service *service)
3547 {
3548         GSequenceIter *iter;
3549
3550         DBG("service %p", service);
3551
3552         if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
3553                 return;
3554
3555         iter = g_hash_table_lookup(service_hash, service->identifier);
3556         if (iter != NULL) {
3557                 reply_pending(service, ECONNABORTED);
3558
3559                 __connman_service_disconnect(service);
3560
3561                 g_sequence_remove(iter);
3562         } else {
3563                 service_free(service);
3564         }
3565 }
3566
3567 static void stats_init(struct connman_service *service)
3568 {
3569         /* home */
3570         service->stats.valid = FALSE;
3571         service->stats.enabled = FALSE;
3572         service->stats.timer = g_timer_new();
3573
3574         /* roaming */
3575         service->stats_roaming.valid = FALSE;
3576         service->stats_roaming.enabled = FALSE;
3577         service->stats_roaming.timer = g_timer_new();
3578 }
3579
3580 static void service_initialize(struct connman_service *service)
3581 {
3582         DBG("service %p", service);
3583
3584         service->refcount = 1;
3585         service->session_usage_count = 0;
3586
3587         service->network_created = FALSE;
3588
3589         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
3590         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3591
3592         service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3593         service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3594         service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3595
3596         service->favorite  = FALSE;
3597         service->immutable = FALSE;
3598         service->hidden = FALSE;
3599
3600         service->ignore = FALSE;
3601
3602         service->userconnect = FALSE;
3603
3604         service->order = 0;
3605
3606         stats_init(service);
3607
3608         service->provider = NULL;
3609
3610         service->wps = FALSE;
3611 }
3612
3613 /**
3614  * connman_service_create:
3615  *
3616  * Allocate a new service.
3617  *
3618  * Returns: a newly-allocated #connman_service structure
3619  */
3620 struct connman_service *connman_service_create(void)
3621 {
3622         GSList *list;
3623         struct connman_stats_counter *counters;
3624         const char *counter;
3625
3626         struct connman_service *service;
3627
3628         service = g_try_new0(struct connman_service, 1);
3629         if (service == NULL)
3630                 return NULL;
3631
3632         DBG("service %p", service);
3633
3634         service->counter_table = g_hash_table_new_full(g_str_hash,
3635                                                 g_str_equal, NULL, g_free);
3636
3637         for (list = counter_list; list; list = list->next) {
3638                 counter = list->data;
3639
3640                 counters = g_try_new0(struct connman_stats_counter, 1);
3641                 if (counters == NULL) {
3642                         g_hash_table_destroy(service->counter_table);
3643                         g_free(service);
3644                         return NULL;
3645                 }
3646
3647                 counters->append_all = TRUE;
3648
3649                 g_hash_table_replace(service->counter_table, (gpointer)counter,
3650                                 counters);
3651         }
3652
3653         service_initialize(service);
3654
3655         return service;
3656 }
3657
3658 /**
3659  * connman_service_ref:
3660  * @service: service structure
3661  *
3662  * Increase reference counter of service
3663  */
3664 struct connman_service *connman_service_ref(struct connman_service *service)
3665 {
3666         DBG("%p", service);
3667
3668         __sync_fetch_and_add(&service->refcount, 1);
3669
3670         return service;
3671 }
3672
3673 /**
3674  * connman_service_unref:
3675  * @service: service structure
3676  *
3677  * Decrease reference counter of service
3678  */
3679 void connman_service_unref(struct connman_service *service)
3680 {
3681         __connman_service_put(service);
3682 }
3683
3684 static gint service_compare(gconstpointer a, gconstpointer b,
3685                                                         gpointer user_data)
3686 {
3687         struct connman_service *service_a = (void *) a;
3688         struct connman_service *service_b = (void *) b;
3689         enum connman_service_state state_a, state_b;
3690
3691         state_a = service_a->state;
3692         state_b = service_b->state;
3693
3694         if (state_a != state_b) {
3695                 gboolean a_connected = is_connected(service_a);
3696                 gboolean b_connected = is_connected(service_b);
3697
3698                 if (a_connected == TRUE && b_connected == TRUE) {
3699                         /* We prefer online over ready state */
3700                         if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3701                                 return -1;
3702
3703                         if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3704                                 return 1;
3705                 }
3706
3707                 if (a_connected == TRUE)
3708                         return -1;
3709                 if (b_connected == TRUE)
3710                         return 1;
3711
3712                 if (is_connecting(service_a) == TRUE)
3713                         return -1;
3714                 if (is_connecting(service_b) == TRUE)
3715                         return 1;
3716         }
3717
3718         if (service_a->order > service_b->order)
3719                 return -1;
3720
3721         if (service_a->order < service_b->order)
3722                 return 1;
3723
3724         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3725                 return -1;
3726
3727         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3728                 return 1;
3729
3730         if (service_a->type != service_b->type) {
3731                 switch (service_a->type) {
3732                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3733                 case CONNMAN_SERVICE_TYPE_SYSTEM:
3734                 case CONNMAN_SERVICE_TYPE_ETHERNET:
3735                 case CONNMAN_SERVICE_TYPE_GPS:
3736                 case CONNMAN_SERVICE_TYPE_VPN:
3737                 case CONNMAN_SERVICE_TYPE_GADGET:
3738                         break;
3739                 case CONNMAN_SERVICE_TYPE_WIFI:
3740                         return 1;
3741                 case CONNMAN_SERVICE_TYPE_WIMAX:
3742                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3743                 case CONNMAN_SERVICE_TYPE_CELLULAR:
3744                         return -1;
3745                 }
3746         }
3747
3748         return (gint) service_b->strength - (gint) service_a->strength;
3749 }
3750
3751 /**
3752  * connman_service_get_type:
3753  * @service: service structure
3754  *
3755  * Get the type of service
3756  */
3757 enum connman_service_type connman_service_get_type(struct connman_service *service)
3758 {
3759         if (service == NULL)
3760                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3761
3762         return service->type;
3763 }
3764
3765 /**
3766  * connman_service_get_interface:
3767  * @service: service structure
3768  *
3769  * Get network interface of service
3770  */
3771 char *connman_service_get_interface(struct connman_service *service)
3772 {
3773         int index;
3774
3775         if (service == NULL)
3776                 return NULL;
3777
3778         if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3779                 if (service->ipconfig_ipv4)
3780                         index = connman_ipconfig_get_index(
3781                                                 service->ipconfig_ipv4);
3782                 else if (service->ipconfig_ipv6)
3783                         index = connman_ipconfig_get_index(
3784                                                 service->ipconfig_ipv6);
3785                 else
3786                         return NULL;
3787
3788                 return connman_inet_ifname(index);
3789         }
3790
3791         if (service->network == NULL)
3792                 return NULL;
3793
3794         index = connman_network_get_index(service->network);
3795
3796         return connman_inet_ifname(index);
3797 }
3798
3799 /**
3800  * connman_service_get_network:
3801  * @service: service structure
3802  *
3803  * Get the service network
3804  */
3805 struct connman_network *
3806 __connman_service_get_network(struct connman_service *service)
3807 {
3808         if (service == NULL)
3809                 return NULL;
3810
3811         return service->network;
3812 }
3813
3814 struct connman_ipconfig *
3815 __connman_service_get_ip4config(struct connman_service *service)
3816 {
3817         if (service == NULL)
3818                 return NULL;
3819
3820         return service->ipconfig_ipv4;
3821 }
3822
3823 struct connman_ipconfig *
3824 __connman_service_get_ip6config(struct connman_service *service)
3825 {
3826         if (service == NULL)
3827                 return NULL;
3828
3829         return service->ipconfig_ipv6;
3830 }
3831
3832 struct connman_ipconfig *
3833 __connman_service_get_ipconfig(struct connman_service *service, int family)
3834 {
3835         if (family == AF_INET)
3836                 return __connman_service_get_ip4config(service);
3837         else if (family == AF_INET6)
3838                 return __connman_service_get_ip6config(service);
3839         else
3840                 return NULL;
3841
3842 }
3843
3844 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3845 {
3846         if (service == NULL)
3847                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3848
3849         return service->security;
3850 }
3851
3852 const char *__connman_service_get_phase2(struct connman_service *service)
3853 {
3854         if (service == NULL)
3855                 return NULL;
3856
3857         return service->phase2;
3858 }
3859
3860 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3861 {
3862         if (service == NULL)
3863                 return FALSE;
3864
3865         return service->wps;
3866 }
3867
3868 /**
3869  * __connman_service_set_favorite:
3870  * @service: service structure
3871  * @favorite: favorite value
3872  *
3873  * Change the favorite setting of service
3874  */
3875 int __connman_service_set_favorite(struct connman_service *service,
3876                                                 connman_bool_t favorite)
3877 {
3878         GSequenceIter *iter;
3879
3880         iter = g_hash_table_lookup(service_hash, service->identifier);
3881         if (iter == NULL)
3882                 return -ENOENT;
3883
3884         if (service->favorite == favorite)
3885                 return -EALREADY;
3886
3887         service->favorite = favorite;
3888         service->order = __connman_service_get_order(service);
3889
3890         favorite_changed(service);
3891
3892         g_sequence_sort_changed(iter, service_compare, NULL);
3893
3894         services_changed(FALSE);
3895
3896         return 0;
3897 }
3898
3899 int __connman_service_set_immutable(struct connman_service *service,
3900                                                 connman_bool_t immutable)
3901 {
3902         service->immutable = immutable;
3903
3904         immutable_changed(service);
3905
3906         return 0;
3907 }
3908
3909 void __connman_service_set_string(struct connman_service *service,
3910                                   const char *key, const char *value)
3911 {
3912         if (g_str_equal(key, "EAP") == TRUE) {
3913                 g_free(service->eap);
3914                 service->eap = g_strdup(value);
3915         } else if (g_str_equal(key, "Identity") == TRUE) {
3916                 g_free(service->identity);
3917                 service->identity = g_strdup(value);
3918         } else if (g_str_equal(key, "CACertFile") == TRUE) {
3919                 g_free(service->ca_cert_file);
3920                 service->ca_cert_file = g_strdup(value);
3921         } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3922                 g_free(service->client_cert_file);
3923                 service->client_cert_file = g_strdup(value);
3924         } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3925                 g_free(service->private_key_file);
3926                 service->private_key_file = g_strdup(value);
3927         } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3928                 g_free(service->private_key_passphrase);
3929                 service->private_key_passphrase = g_strdup(value);
3930         } else if (g_str_equal(key, "Phase2") == TRUE) {
3931                 g_free(service->phase2);
3932                 service->phase2 = g_strdup(value);
3933         } else if (g_str_equal(key, "Passphrase") == TRUE) {
3934                 g_free(service->passphrase);
3935                 service->passphrase = g_strdup(value);
3936         }
3937 }
3938
3939 static void service_complete(struct connman_service *service)
3940 {
3941         reply_pending(service, EIO);
3942
3943         if (service->userconnect == FALSE)
3944                 __connman_service_auto_connect();
3945
3946         g_get_current_time(&service->modified);
3947         service_save(service);
3948 }
3949
3950 static void report_error_cb(struct connman_service *service,
3951                         gboolean retry, void *user_data)
3952 {
3953         if (retry == TRUE)
3954                 __connman_service_connect(service);
3955         else {
3956                 service_complete(service);
3957                 services_changed(FALSE);
3958                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3959         }
3960 }
3961
3962 static void request_input_cb (struct connman_service *service,
3963                         const char *identity, const char *passphrase,
3964                         void *user_data)
3965 {
3966         DBG ("RequestInput return, %p", service);
3967
3968         if (identity == NULL && passphrase == NULL && service->wps == FALSE) {
3969                 service_complete(service);
3970                 services_changed(FALSE);
3971                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3972                 return;
3973         }
3974
3975         if (identity != NULL)
3976                 __connman_service_set_agent_identity(service, identity);
3977
3978         if (passphrase != NULL) {
3979                 switch (service->security) {
3980                 case CONNMAN_SERVICE_SECURITY_WEP:
3981                 case CONNMAN_SERVICE_SECURITY_PSK:
3982                         __connman_service_set_passphrase(service, passphrase);
3983                         break;
3984                 case CONNMAN_SERVICE_SECURITY_8021X:
3985                         __connman_service_set_agent_passphrase(service,
3986                                                         passphrase);
3987                         break;
3988                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3989                 case CONNMAN_SERVICE_SECURITY_NONE:
3990                 case CONNMAN_SERVICE_SECURITY_WPA:
3991                 case CONNMAN_SERVICE_SECURITY_RSN:
3992                         DBG("service security '%s' not handled",
3993                                 security2string(service->security));
3994                         break;
3995                 }
3996         }
3997
3998         __connman_service_connect(service);
3999
4000         /* Never cache agent provided credentials */
4001         __connman_service_set_agent_identity(service, NULL);
4002         __connman_service_set_agent_passphrase(service, NULL);
4003 }
4004
4005 static void downgrade_connected_services(void)
4006 {
4007         struct connman_service *up_service;
4008         GSequenceIter *iter;
4009
4010         iter = g_sequence_get_begin_iter(service_list);
4011         while (g_sequence_iter_is_end(iter) == FALSE) {
4012                 up_service = g_sequence_get(iter);
4013
4014                 if (is_connected(up_service) == FALSE) {
4015                         iter = g_sequence_iter_next(iter);
4016                         continue;
4017                 }
4018
4019                 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4020                         return;
4021
4022                 downgrade_state(up_service);
4023
4024                 iter = g_sequence_iter_next(iter);
4025         }
4026 }
4027
4028 static int service_indicate_state(struct connman_service *service)
4029 {
4030         enum connman_service_state old_state, new_state;
4031         struct connman_service *def_service;
4032         GSequenceIter *iter;
4033
4034         if (service == NULL)
4035                 return -EINVAL;
4036
4037         old_state = service->state;
4038         new_state = combine_state(service->state_ipv4, service->state_ipv6);
4039
4040         DBG("service %p old %s - new %s/%s => %s",
4041                                         service,
4042                                         state2string(old_state),
4043                                         state2string(service->state_ipv4),
4044                                         state2string(service->state_ipv6),
4045                                         state2string(new_state));
4046
4047         if (old_state == new_state)
4048                 return -EALREADY;
4049
4050         def_service = get_default();
4051
4052         if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4053                 if (def_service != NULL && def_service != service &&
4054                         def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4055                         return -EALREADY;
4056         }
4057
4058         service->state = new_state;
4059         state_changed(service);
4060
4061         if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4062                         old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4063                 reply_pending(service, ECONNABORTED);
4064
4065                 __connman_service_disconnect(service);
4066         }
4067
4068         if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4069                 if (__connman_stats_service_register(service) == 0) {
4070                         __connman_stats_get(service, FALSE,
4071                                                 &service->stats.data);
4072                         __connman_stats_get(service, TRUE,
4073                                                 &service->stats_roaming.data);
4074                 }
4075         }
4076
4077         if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4078                 if (service->login_required == TRUE) {
4079                         service->login_required = FALSE;
4080                         login_changed(service);
4081                 }
4082
4083                 connman_timeserver_sync();
4084         }
4085
4086         if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4087                 connman_bool_t reconnect;
4088
4089                 reconnect = get_reconnect_state(service);
4090                 if (reconnect == TRUE)
4091                         __connman_service_auto_connect();
4092
4093                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4094         }
4095
4096         if (new_state == CONNMAN_SERVICE_STATE_READY) {
4097                 enum connman_ipconfig_method method;
4098
4099                 set_reconnect_state(service, TRUE);
4100
4101                 __connman_service_set_favorite(service, TRUE);
4102
4103                 reply_pending(service, 0);
4104
4105                 service->userconnect = FALSE;
4106
4107                 g_get_current_time(&service->modified);
4108                 service_save(service);
4109
4110                 update_nameservers(service);
4111                 dns_changed(service);
4112                 domain_changed(service);
4113
4114                 __connman_notifier_connect(service->type);
4115
4116                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4117                         connman_network_get_bool(service->network,
4118                                                 "WiFi.UseWPS") == TRUE) {
4119                         const char *pass;
4120
4121                         pass = connman_network_get_string(service->network,
4122                                                         "WiFi.Passphrase");
4123
4124                         __connman_service_set_passphrase(service, pass);
4125
4126                         connman_network_set_bool(service->network,
4127                                                         "WiFi.UseWPS", FALSE);
4128                 }
4129
4130                 default_changed();
4131
4132                 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4133                 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4134                         __connman_ipconfig_disable_ipv6(
4135                                                 service->ipconfig_ipv6);
4136
4137         } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4138                 def_service = get_default();
4139
4140                 if (__connman_notifier_count_connected() == 0 &&
4141                         def_service != NULL &&
4142                                 def_service->provider != NULL)
4143                         __connman_provider_disconnect(def_service->provider);
4144
4145                 default_changed();
4146
4147                 __connman_wispr_stop(service);
4148
4149                 __connman_wpad_stop(service);
4150
4151                 update_nameservers(service);
4152                 dns_changed(service);
4153                 domain_changed(service);
4154
4155                 __connman_notifier_disconnect(service->type);
4156
4157                 /*
4158                  * Previous services which are connected and which states
4159                  * are set to online should reset relevantly ipconfig_state
4160                  * to ready so wispr/portal will be rerun on those
4161                  */
4162                 downgrade_connected_services();
4163         }
4164
4165         if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4166                 if (service->userconnect == TRUE &&
4167                         __connman_agent_report_error(service,
4168                                         error2string(service->error),
4169                                         report_error_cb, NULL) == -EIO)
4170                         return 0;
4171                 service_complete(service);
4172
4173                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4174         } else
4175                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4176
4177         iter = g_hash_table_lookup(service_hash, service->identifier);
4178         if (iter != NULL)
4179                 g_sequence_sort_changed(iter, service_compare, NULL);
4180
4181         services_changed(FALSE);
4182
4183         if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4184                 default_changed();
4185
4186         return 0;
4187 }
4188
4189 int __connman_service_indicate_error(struct connman_service *service,
4190                                         enum connman_service_error error)
4191 {
4192         DBG("service %p error %d", service, error);
4193
4194         if (service == NULL)
4195                 return -EINVAL;
4196
4197         service->error = error;
4198
4199         if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4200                 __connman_service_set_passphrase(service, NULL);
4201
4202         __connman_service_ipconfig_indicate_state(service,
4203                                                 CONNMAN_SERVICE_STATE_FAILURE,
4204                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4205         __connman_service_ipconfig_indicate_state(service,
4206                                                 CONNMAN_SERVICE_STATE_FAILURE,
4207                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4208         return 0;
4209 }
4210
4211 int __connman_service_clear_error(struct connman_service *service)
4212 {
4213         DBG("service %p", service);
4214
4215         if (service == NULL)
4216                 return -EINVAL;
4217
4218         if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4219                 return -EINVAL;
4220
4221         service->state_ipv4 = service->state_ipv6 =
4222                                                 CONNMAN_SERVICE_STATE_UNKNOWN;
4223         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4224
4225         if (service->favorite == TRUE)
4226                 set_reconnect_state(service, TRUE);
4227
4228         __connman_service_ipconfig_indicate_state(service,
4229                                         CONNMAN_SERVICE_STATE_IDLE,
4230                                         CONNMAN_IPCONFIG_TYPE_IPV6);
4231
4232         /*
4233          * Toggling the IPv6 state to IDLE could trigger the auto connect
4234          * machinery and consequently the IPv4 state.
4235          */
4236         if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4237                         service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4238                 return 0;
4239
4240         return __connman_service_ipconfig_indicate_state(service,
4241                                                 CONNMAN_SERVICE_STATE_IDLE,
4242                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4243 }
4244
4245 int __connman_service_indicate_default(struct connman_service *service)
4246 {
4247         DBG("service %p", service);
4248
4249         default_changed();
4250
4251         return 0;
4252 }
4253
4254 enum connman_service_state __connman_service_ipconfig_get_state(
4255                                         struct connman_service *service,
4256                                         enum connman_ipconfig_type type)
4257 {
4258         if (service == NULL)
4259                 return CONNMAN_SERVICE_STATE_UNKNOWN;
4260
4261         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4262                 return service->state_ipv4;
4263
4264         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4265                 return service->state_ipv6;
4266
4267         return CONNMAN_SERVICE_STATE_UNKNOWN;
4268 }
4269
4270 static void check_proxy_setup(struct connman_service *service)
4271 {
4272         /*
4273          * We start WPAD if we haven't got a PAC URL from DHCP and
4274          * if our proxy manual configuration is either empty or set
4275          * to AUTO with an empty URL.
4276          */
4277
4278         if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4279                 goto done;
4280
4281         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4282                 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4283                         service->pac != NULL))
4284                 goto done;
4285
4286         if (__connman_wpad_start(service) < 0) {
4287                 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4288                 __connman_notifier_proxy_changed(service);
4289                 goto done;
4290         }
4291
4292         return;
4293
4294 done:
4295         __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4296 }
4297
4298 /*
4299  * How many networks are connected at the same time. If more than 1,
4300  * then set the rp_filter setting properly (loose mode routing) so that network
4301  * connectivity works ok. This is only done for IPv4 networks as IPv6
4302  * does not have rp_filter knob.
4303  */
4304 static int connected_networks_count;
4305 static int original_rp_filter;
4306
4307 static void service_rp_filter(struct connman_service *service,
4308                                 gboolean connected)
4309 {
4310         enum connman_ipconfig_method method;
4311
4312         method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4313
4314         switch (method) {
4315         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4316         case CONNMAN_IPCONFIG_METHOD_OFF:
4317         case CONNMAN_IPCONFIG_METHOD_AUTO:
4318                 return;
4319         case CONNMAN_IPCONFIG_METHOD_FIXED:
4320         case CONNMAN_IPCONFIG_METHOD_MANUAL:
4321         case CONNMAN_IPCONFIG_METHOD_DHCP:
4322                 break;
4323         }
4324
4325         if (connected == TRUE) {
4326                 if (connected_networks_count == 1) {
4327                         int filter_value;
4328                         filter_value = __connman_ipconfig_set_rp_filter();
4329                         if (filter_value < 0)
4330                                 return;
4331
4332                         original_rp_filter = filter_value;
4333                 }
4334                 connected_networks_count++;
4335
4336         } else {
4337                 if (connected_networks_count == 2)
4338                         __connman_ipconfig_unset_rp_filter(original_rp_filter);
4339
4340                 connected_networks_count--;
4341                 if (connected_networks_count < 0)
4342                         connected_networks_count = 0;
4343         }
4344
4345         DBG("%s %s ipconfig %p method %d count %d filter %d",
4346                 connected ? "connected" : "disconnected", service->identifier,
4347                 service->ipconfig_ipv4, method,
4348                 connected_networks_count, original_rp_filter);
4349 }
4350
4351 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4352                                         enum connman_service_state new_state,
4353                                         enum connman_ipconfig_type type)
4354 {
4355         struct connman_ipconfig *ipconfig = NULL;
4356         enum connman_service_state old_state;
4357         int ret;
4358
4359         if (service == NULL)
4360                 return -EINVAL;
4361
4362         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4363                 old_state = service->state_ipv4;
4364                 ipconfig = service->ipconfig_ipv4;
4365         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4366                 old_state = service->state_ipv6;
4367                 ipconfig = service->ipconfig_ipv6;
4368         }
4369
4370         if (ipconfig == NULL)
4371                 return -EINVAL;
4372
4373         /* Any change? */
4374         if (old_state == new_state)
4375                 return -EALREADY;
4376
4377         DBG("service %p (%s) state %d (%s) type %d (%s)",
4378                 service, service ? service->identifier : NULL,
4379                 new_state, state2string(new_state),
4380                 type, __connman_ipconfig_type2string(type));
4381
4382         switch (new_state) {
4383         case CONNMAN_SERVICE_STATE_UNKNOWN:
4384         case CONNMAN_SERVICE_STATE_IDLE:
4385                 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4386                         return -EINVAL;
4387                 break;
4388         case CONNMAN_SERVICE_STATE_ASSOCIATION:
4389                 break;
4390         case CONNMAN_SERVICE_STATE_CONFIGURATION:
4391                 __connman_ipconfig_enable(ipconfig);
4392                 break;
4393         case CONNMAN_SERVICE_STATE_READY:
4394                 update_nameservers(service);
4395
4396                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4397                         check_proxy_setup(service);
4398                         service_rp_filter(service, TRUE);
4399                 } else
4400                         __connman_wispr_start(service, type);
4401                 break;
4402         case CONNMAN_SERVICE_STATE_ONLINE:
4403                 break;
4404         case CONNMAN_SERVICE_STATE_DISCONNECT:
4405                 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4406                         return -EINVAL;
4407
4408                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4409                         service_rp_filter(service, FALSE);
4410
4411                 break;
4412         case CONNMAN_SERVICE_STATE_FAILURE:
4413                 break;
4414         }
4415
4416         /* We keep that state */
4417         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4418                 service->state_ipv4 = new_state;
4419         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4420                 service->state_ipv6 = new_state;
4421
4422         ret = service_indicate_state(service);
4423
4424         /*
4425          * If the ipconfig method is OFF, then we set the state to IDLE
4426          * so that it will not affect the combined state in the future.
4427          */
4428         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4429                 enum connman_ipconfig_method method;
4430                 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4431                 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4432                                 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4433                         service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4434                         ret = service_indicate_state(service);
4435                 }
4436
4437         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4438                 enum connman_ipconfig_method method;
4439                 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4440                 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4441                                 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4442                         service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4443                         ret = service_indicate_state(service);
4444                 }
4445         }
4446
4447         return ret;
4448 }
4449
4450 int __connman_service_request_login(struct connman_service *service)
4451 {
4452         DBG("service %p", service);
4453
4454         if (service == NULL)
4455                 return -EINVAL;
4456
4457         service->login_required = TRUE;
4458         login_changed(service);
4459
4460         return 0;
4461 }
4462
4463 static connman_bool_t prepare_network(struct connman_service *service)
4464 {
4465         enum connman_network_type type;
4466         unsigned int ssid_len;
4467
4468         type = connman_network_get_type(service->network);
4469
4470         switch (type) {
4471         case CONNMAN_NETWORK_TYPE_UNKNOWN:
4472         case CONNMAN_NETWORK_TYPE_VENDOR:
4473                 return FALSE;
4474         case CONNMAN_NETWORK_TYPE_WIFI:
4475                 if (connman_network_get_blob(service->network, "WiFi.SSID",
4476                                                         &ssid_len) == NULL)
4477                         return FALSE;
4478
4479                 if (service->passphrase != NULL)
4480                         connman_network_set_string(service->network,
4481                                 "WiFi.Passphrase", service->passphrase);
4482                 break;
4483         case CONNMAN_NETWORK_TYPE_ETHERNET:
4484         case CONNMAN_NETWORK_TYPE_WIMAX:
4485         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4486         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4487         case CONNMAN_NETWORK_TYPE_CELLULAR:
4488                 break;
4489         }
4490
4491         return TRUE;
4492 }
4493
4494 static void prepare_8021x(struct connman_service *service)
4495 {
4496         if (service->eap != NULL)
4497                 connman_network_set_string(service->network, "WiFi.EAP",
4498                                                                 service->eap);
4499
4500         if (service->identity != NULL)
4501                 connman_network_set_string(service->network, "WiFi.Identity",
4502                                                         service->identity);
4503
4504         if (service->ca_cert_file != NULL)
4505                 connman_network_set_string(service->network, "WiFi.CACertFile",
4506                                                         service->ca_cert_file);
4507
4508         if (service->client_cert_file != NULL)
4509                 connman_network_set_string(service->network,
4510                                                 "WiFi.ClientCertFile",
4511                                                 service->client_cert_file);
4512
4513         if (service->private_key_file != NULL)
4514                 connman_network_set_string(service->network,
4515                                                 "WiFi.PrivateKeyFile",
4516                                                 service->private_key_file);
4517
4518         if (service->private_key_passphrase != NULL)
4519                 connman_network_set_string(service->network,
4520                                         "WiFi.PrivateKeyPassphrase",
4521                                         service->private_key_passphrase);
4522
4523         if (service->phase2 != NULL)
4524                 connman_network_set_string(service->network, "WiFi.Phase2",
4525                                                         service->phase2);
4526 }
4527
4528 static int service_connect(struct connman_service *service)
4529 {
4530         int err;
4531
4532         switch (service->type) {
4533         case CONNMAN_SERVICE_TYPE_UNKNOWN:
4534         case CONNMAN_SERVICE_TYPE_SYSTEM:
4535         case CONNMAN_SERVICE_TYPE_GPS:
4536         case CONNMAN_SERVICE_TYPE_GADGET:
4537                 return -EINVAL;
4538         case CONNMAN_SERVICE_TYPE_ETHERNET:
4539         case CONNMAN_SERVICE_TYPE_WIMAX:
4540         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4541         case CONNMAN_SERVICE_TYPE_CELLULAR:
4542         case CONNMAN_SERVICE_TYPE_VPN:
4543                 break;
4544         case CONNMAN_SERVICE_TYPE_WIFI:
4545                 switch (service->security) {
4546                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4547                 case CONNMAN_SERVICE_SECURITY_NONE:
4548                         break;
4549                 case CONNMAN_SERVICE_SECURITY_WEP:
4550                 case CONNMAN_SERVICE_SECURITY_PSK:
4551                 case CONNMAN_SERVICE_SECURITY_WPA:
4552                 case CONNMAN_SERVICE_SECURITY_RSN:
4553                         if (service->passphrase == NULL) {
4554                                 if (service->network == NULL)
4555                                         return -EOPNOTSUPP;
4556
4557                                 if (service->wps == FALSE ||
4558                                         connman_network_get_bool(
4559                                                         service->network,
4560                                                         "WiFi.UseWPS") == FALSE)
4561                                         return -ENOKEY;
4562                         }
4563                         break;
4564                 case CONNMAN_SERVICE_SECURITY_8021X:
4565                         if (service->eap == NULL)
4566                                 return -EINVAL;
4567
4568                         /*
4569                          * never request credentials if using EAP-TLS
4570                          * (EAP-TLS networks need to be fully provisioned)
4571                          */
4572                         if (g_str_equal(service->eap, "tls") == TRUE)
4573                                 break;
4574
4575                         /*
4576                          * Return -ENOKEY if either identity or passphrase is
4577                          * missing. Agent provided credentials can be used as
4578                          * fallback if needed.
4579                          */
4580                         if ((service->identity == NULL &&
4581                                         service->agent_identity == NULL) ||
4582                                         (service->passphrase == NULL &&
4583                                         service->agent_passphrase == NULL))
4584                                 return -ENOKEY;
4585
4586                         break;
4587                 }
4588                 break;
4589         }
4590
4591         if (service->network != NULL) {
4592                 if (prepare_network(service) == FALSE)
4593                         return -EINVAL;
4594
4595                 switch (service->security) {
4596                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4597                 case CONNMAN_SERVICE_SECURITY_NONE:
4598                 case CONNMAN_SERVICE_SECURITY_WEP:
4599                 case CONNMAN_SERVICE_SECURITY_PSK:
4600                 case CONNMAN_SERVICE_SECURITY_WPA:
4601                 case CONNMAN_SERVICE_SECURITY_RSN:
4602                         break;
4603                 case CONNMAN_SERVICE_SECURITY_8021X:
4604                         prepare_8021x(service);
4605                         break;
4606                 }
4607
4608                 if (__connman_stats_service_register(service) == 0) {
4609                         __connman_stats_get(service, FALSE,
4610                                                 &service->stats.data);
4611                         __connman_stats_get(service, TRUE,
4612                                                 &service->stats_roaming.data);
4613                 }
4614
4615                 if (service->ipconfig_ipv4)
4616                         __connman_ipconfig_enable(service->ipconfig_ipv4);
4617                 if (service->ipconfig_ipv6)
4618                         __connman_ipconfig_enable(service->ipconfig_ipv6);
4619
4620                 err = __connman_network_connect(service->network);
4621         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4622                                         service->provider != NULL)
4623                 err = __connman_provider_connect(service->provider);
4624         else
4625                 return -EOPNOTSUPP;
4626
4627         if (err < 0) {
4628                 if (err != -EINPROGRESS) {
4629                         __connman_ipconfig_disable(service->ipconfig_ipv4);
4630                         __connman_ipconfig_disable(service->ipconfig_ipv6);
4631                         __connman_stats_service_unregister(service);
4632                 }
4633         }
4634
4635         return err;
4636 }
4637
4638
4639 int __connman_service_connect(struct connman_service *service)
4640 {
4641         int err;
4642
4643         DBG("service %p state %s", service, state2string(service->state));
4644
4645         if (is_connected(service) == TRUE)
4646                 return -EISCONN;
4647
4648         if (is_connecting(service) == TRUE)
4649                 return -EALREADY;
4650
4651         switch (service->type) {
4652         case CONNMAN_SERVICE_TYPE_UNKNOWN:
4653         case CONNMAN_SERVICE_TYPE_SYSTEM:
4654         case CONNMAN_SERVICE_TYPE_GPS:
4655         case CONNMAN_SERVICE_TYPE_GADGET:
4656                 return -EINVAL;
4657         default:
4658                 err = service_connect(service);
4659         }
4660
4661         if (err >= 0)
4662                 return 0;
4663
4664         if (err == -EINPROGRESS) {
4665                 if (service->timeout == 0)
4666                         service->timeout = g_timeout_add_seconds(
4667                                 CONNECT_TIMEOUT, connect_timeout, service);
4668
4669                 return -EINPROGRESS;
4670         }
4671
4672         __connman_service_ipconfig_indicate_state(service,
4673                                         CONNMAN_SERVICE_STATE_FAILURE,
4674                                         CONNMAN_IPCONFIG_TYPE_IPV4);
4675         __connman_service_ipconfig_indicate_state(service,
4676                                         CONNMAN_SERVICE_STATE_FAILURE,
4677                                         CONNMAN_IPCONFIG_TYPE_IPV6);
4678
4679         if (service->network != NULL)
4680                 __connman_network_disconnect(service->network);
4681         else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4682                                 service->provider != NULL)
4683                         __connman_provider_disconnect(service->provider);
4684
4685         if (service->userconnect == TRUE) {
4686                 if (err == -ENOKEY) {
4687                         if (__connman_agent_request_passphrase_input(service,
4688                                                         request_input_cb,
4689                                                         NULL) == -EIO)
4690                                 return -EINPROGRESS;
4691                 }
4692                 reply_pending(service, err);
4693         }
4694
4695         return err;
4696 }
4697
4698 int __connman_service_disconnect(struct connman_service *service)
4699 {
4700         int err;
4701
4702         DBG("service %p", service);
4703
4704         if (service->network != NULL) {
4705                 err = __connman_network_disconnect(service->network);
4706         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4707                                         service->provider != NULL)
4708                 err = __connman_provider_disconnect(service->provider);
4709         else
4710                 return -EOPNOTSUPP;
4711
4712         if (err < 0 && err != -EINPROGRESS)
4713                 return err;
4714
4715         __connman_6to4_remove(service->ipconfig_ipv4);
4716
4717         if (service->ipconfig_ipv4)
4718                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4719                                                         NULL);
4720         else
4721                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4722                                                         NULL);
4723
4724         __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4725         __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4726
4727         __connman_ipconfig_disable(service->ipconfig_ipv4);
4728         __connman_ipconfig_disable(service->ipconfig_ipv6);
4729
4730         __connman_stats_service_unregister(service);
4731
4732         return err;
4733 }
4734
4735 int __connman_service_disconnect_all(void)
4736 {
4737         GSequenceIter *iter;
4738         GSList *services = NULL, *list;
4739
4740         DBG("");
4741
4742         iter = g_sequence_get_begin_iter(service_list);
4743
4744         while (g_sequence_iter_is_end(iter) == FALSE) {
4745                 struct connman_service *service = g_sequence_get(iter);
4746
4747                 services = g_slist_prepend(services, service);
4748
4749                 iter = g_sequence_iter_next(iter);
4750         }
4751
4752         for (list = services; list != NULL; list = list->next) {
4753                 struct connman_service *service = list->data;
4754
4755                 service->ignore = TRUE;
4756
4757                 set_reconnect_state(service, FALSE);
4758
4759                 __connman_service_disconnect(service);
4760         }
4761
4762         g_slist_free(list);
4763
4764         return 0;
4765
4766 }
4767
4768 /**
4769  * __connman_service_lookup:
4770  * @pattern: search pattern
4771  * @path: return object path
4772  *
4773  * Look up a service path from a search pattern
4774  */
4775 int __connman_service_lookup(const char *pattern, const char **path)
4776 {
4777         GHashTableIter iter;
4778         gpointer key, value;
4779         struct connman_device *device;
4780         const char *ifname;
4781
4782         g_hash_table_iter_init(&iter, service_hash);
4783
4784         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4785                 GSequenceIter *iter = value;
4786                 struct connman_service *service = g_sequence_get(iter);
4787
4788                 if (g_strcmp0(service->identifier, pattern) == 0 ||
4789                                 g_strcmp0(service->name, pattern) == 0) {
4790                         *path = (const char *) service->path;
4791                         return 0;
4792                 }
4793
4794                 if (service->network == NULL)
4795                         continue;
4796
4797                 device = connman_network_get_device(service->network);
4798                 if (device == NULL)
4799                         continue;
4800
4801                 ifname = connman_device_get_string(device, "Interface");
4802                 if (ifname != NULL && g_strcmp0(ifname, pattern) == 0) {
4803                         *path = (const char *) service->path;
4804                         return 0;
4805                 }
4806
4807         }
4808
4809         return -ENXIO;
4810 }
4811
4812 /**
4813  * lookup_by_identifier:
4814  * @identifier: service identifier
4815  *
4816  * Look up a service by identifier (reference count will not be increased)
4817  */
4818 static struct connman_service *lookup_by_identifier(const char *identifier)
4819 {
4820         GSequenceIter *iter;
4821
4822         iter = g_hash_table_lookup(service_hash, identifier);
4823         if (iter != NULL)
4824                 return g_sequence_get(iter);
4825
4826         return NULL;
4827 }
4828
4829 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4830                 const char *ssid, const char *mode, const char *security,
4831                 const char *group)
4832 {
4833         struct connman_network *network;
4834         char *name;
4835         int index;
4836         unsigned int i, ssid_len;
4837
4838         ssid_len = strlen(ssid);
4839         if (ssid_len < 1)
4840                 return NULL;
4841
4842         network = connman_network_create(group, CONNMAN_NETWORK_TYPE_WIFI);
4843         if (network == NULL)
4844                 return NULL;
4845
4846         connman_network_set_blob(network, "WiFi.SSID",
4847                                         (unsigned char *) ssid, ssid_len);
4848
4849         connman_network_set_string(network, "WiFi.Mode", mode);
4850         connman_network_set_string(network, "WiFi.Security", security);
4851
4852         name = g_try_malloc0(ssid_len + 1);
4853         if (name == NULL) {
4854                 connman_network_unref(network);
4855                 return NULL;
4856         }
4857
4858         for (i = 0; i < ssid_len; i++) {
4859                 if (g_ascii_isprint(ssid[i]))
4860                         name[i] = ssid[i];
4861                 else
4862                         name[i] = ' ';
4863         }
4864
4865         connman_network_set_name(network, name);
4866
4867         g_free(name);
4868
4869         index = connman_device_get_index(device);
4870         connman_network_set_index(network, index);
4871
4872         if (connman_device_add_network(device, network) < 0) {
4873                 connman_network_unref(network);
4874                 return NULL;
4875         }
4876
4877         connman_network_set_available(network, TRUE);
4878
4879         return network;
4880 }
4881
4882 int __connman_service_create_and_connect(DBusMessage *msg)
4883 {
4884         struct connman_service *service;
4885         struct connman_network *network;
4886         struct connman_device *device;
4887         DBusMessageIter iter, array;
4888         const char *mode = "managed", *security = "none", *group_security;
4889         const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4890         connman_bool_t network_created = FALSE;
4891         unsigned int ssid_len = 0;
4892         const char *ident;
4893         char *name, *group;
4894         int err;
4895
4896         dbus_message_iter_init(msg, &iter);
4897         dbus_message_iter_recurse(&iter, &array);
4898
4899         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4900                 DBusMessageIter entry, value;
4901                 const char *key;
4902
4903                 dbus_message_iter_recurse(&array, &entry);
4904                 dbus_message_iter_get_basic(&entry, &key);
4905
4906                 dbus_message_iter_next(&entry);
4907                 dbus_message_iter_recurse(&entry, &value);
4908
4909                 switch (dbus_message_iter_get_arg_type(&value)) {
4910                 case DBUS_TYPE_STRING:
4911                         if (g_str_equal(key, "Type") == TRUE)
4912                                 dbus_message_iter_get_basic(&value, &type);
4913                         else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4914                                         g_str_equal(key, "Mode") == TRUE)
4915                                 dbus_message_iter_get_basic(&value, &mode);
4916                         else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4917                                         g_str_equal(key, "Security") == TRUE)
4918                                 dbus_message_iter_get_basic(&value, &security);
4919                         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4920                                         g_str_equal(key, "Passphrase") == TRUE)
4921                                 dbus_message_iter_get_basic(&value, &passphrase);
4922                         else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4923                                         g_str_equal(key, "SSID") == TRUE)
4924                                 dbus_message_iter_get_basic(&value, &ssid);
4925                 }
4926
4927                 dbus_message_iter_next(&array);
4928         }
4929
4930         if (type == NULL)
4931                 return -EINVAL;
4932
4933         if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4934                 return -EOPNOTSUPP;
4935
4936         if (ssid == NULL)
4937                 return -EINVAL;
4938
4939         ssid_len = strlen(ssid);
4940         if (ssid_len < 1)
4941                 return -EINVAL;
4942
4943         if (g_strcmp0(security, "none") != 0 &&
4944                                 g_strcmp0(security, "wep") != 0 &&
4945                                 g_strcmp0(security, "psk") != 0 &&
4946                                 g_strcmp0(security, "wpa") != 0 &&
4947                                 g_strcmp0(security, "rsn") != 0 &&
4948                                 g_strcmp0(security, "ieee8021x") != 0)
4949                 return -EINVAL;
4950
4951         device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4952         if (device == NULL)
4953                 return -EOPNOTSUPP;
4954
4955         ident = connman_device_get_ident(device);
4956         if (ident == NULL)
4957                 return -EOPNOTSUPP;
4958
4959
4960         if (!g_strcmp0(security, "wpa") ||
4961                 !g_strcmp0(security, "rsn"))
4962                 group_security = "psk";
4963         else
4964                 group_security = security;
4965
4966         group = wifi_build_group_name((unsigned char *) ssid,
4967                                                 ssid_len, mode, group_security);
4968         if (group == NULL)
4969                 return -EINVAL;
4970
4971         name = g_strdup_printf("%s_%s_%s", type, ident, group);
4972
4973         service = lookup_by_identifier(name);
4974
4975         if (service == NULL) {
4976                 network = create_hidden_wifi(device, ssid,
4977                                                 mode, security, group);
4978                 if (network != NULL) {
4979                         connman_network_set_group(network, group);
4980                         network_created = TRUE;
4981                 }
4982
4983                 service = lookup_by_identifier(name);
4984         }
4985
4986         g_free(name);
4987         g_free(group);
4988
4989         if (service == NULL) {
4990                 err = -EOPNOTSUPP;
4991                 goto failed;
4992         }
4993
4994         service->network_created = network_created;
4995
4996         if (is_connected(service) == TRUE) {
4997                 err = -EISCONN;
4998                 goto failed;
4999         }
5000
5001         if (is_connecting(service) == TRUE) {
5002                 err = -EALREADY;
5003                 goto failed;
5004         }
5005
5006         set_reconnect_state(service, FALSE);
5007
5008         __connman_device_disconnect(device);
5009
5010         if (passphrase != NULL) {
5011                 g_free(service->passphrase);
5012                 service->passphrase = g_strdup(passphrase);
5013         }
5014
5015         service->userconnect = TRUE;
5016
5017         err = __connman_service_connect(service);
5018         if (err < 0 && err != -EINPROGRESS)
5019                 goto failed;
5020
5021         service->pending = dbus_message_ref(msg);
5022
5023         return 0;
5024
5025 failed:
5026         if (service != NULL && service->network_created == TRUE) {
5027                 struct connman_network *network = service->network;
5028
5029                 if (network != NULL) {
5030                         connman_network_set_available(network, FALSE);
5031                         __connman_device_cleanup_networks(device);
5032                 } else
5033                         __connman_service_put(service);
5034         }
5035
5036         return err;
5037 }
5038
5039 static void provision_changed(gpointer value, gpointer user_data)
5040 {
5041         struct connman_service *service = value;
5042         char *path = user_data;
5043
5044         __connman_config_provision_service_ident(service, path);
5045 }
5046
5047 void __connman_service_provision_changed(const char *ident)
5048 {
5049         g_sequence_foreach(service_list, provision_changed, (void *)ident);
5050 }
5051
5052 int __connman_service_provision(DBusMessage *msg)
5053 {
5054         GKeyFile *keyfile = NULL;
5055         const char *config_str = NULL;
5056         char *group = NULL, *ident = NULL;
5057         int err = 0;
5058         struct connman_service *service;
5059
5060         DBG("");
5061
5062         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
5063                                                         DBUS_TYPE_INVALID);
5064
5065         if (config_str == NULL || strlen(config_str) == 0)
5066                 return -EINVAL;
5067
5068         keyfile = g_key_file_new();
5069
5070         /* populate GKeyFile with config_str */
5071         if (g_key_file_load_from_data(keyfile, config_str,
5072                                         strlen(config_str), 0, NULL) == FALSE) {
5073                 err = -EINVAL;
5074                 goto done;
5075         }
5076
5077         /*
5078          * read only one group of settings (only one service supported, no
5079          * global settings)
5080          */
5081         group = g_key_file_get_start_group(keyfile);
5082
5083         if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
5084                 err = -EINVAL;
5085                 goto done;
5086         }
5087
5088         err = __connman_config_load_service(keyfile, group, TRUE);
5089         if (err < 0)
5090                 goto done;
5091
5092         ident = group + strlen("service_");
5093
5094         /* trigger service provisioning if service exists */
5095         service = lookup_by_identifier(ident);
5096         if (service != NULL)
5097                 __connman_config_provision_service(service);
5098
5099         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
5100
5101 done:
5102         if (group != NULL)
5103                 g_free(group);
5104
5105         if (keyfile != NULL)
5106                 g_key_file_free(keyfile);
5107
5108         return err;
5109 }
5110
5111 /**
5112  * __connman_service_get:
5113  * @identifier: service identifier
5114  *
5115  * Look up a service by identifier or create a new one if not found
5116  */
5117 static struct connman_service *service_get(const char *identifier)
5118 {
5119         struct connman_service *service;
5120         GSequenceIter *iter;
5121
5122         iter = g_hash_table_lookup(service_hash, identifier);
5123         if (iter != NULL) {
5124                 service = g_sequence_get(iter);
5125                 if (service != NULL)
5126                         connman_service_ref(service);
5127                 return service;
5128         }
5129
5130         service = connman_service_create();
5131         if (service == NULL)
5132                 return NULL;
5133
5134         DBG("service %p", service);
5135
5136         service->identifier = g_strdup(identifier);
5137
5138         iter = g_sequence_insert_sorted(service_list, service,
5139                                                 service_compare, NULL);
5140
5141         g_hash_table_insert(service_hash, service->identifier, iter);
5142
5143         return service;
5144 }
5145
5146 static int service_register(struct connman_service *service)
5147 {
5148         GSequenceIter *iter;
5149
5150         DBG("service %p", service);
5151
5152         if (service->path != NULL)
5153                 return -EALREADY;
5154
5155         service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
5156                                                 service->identifier);
5157
5158         DBG("path %s", service->path);
5159
5160         __connman_config_provision_service(service);
5161
5162         service_load(service);
5163
5164         g_dbus_register_interface(connection, service->path,
5165                                         CONNMAN_SERVICE_INTERFACE,
5166                                         service_methods, service_signals,
5167                                                         NULL, service, NULL);
5168
5169         iter = g_hash_table_lookup(service_hash, service->identifier);
5170         if (iter != NULL)
5171                 g_sequence_sort_changed(iter, service_compare, NULL);
5172
5173         services_changed(TRUE);
5174
5175         return 0;
5176 }
5177
5178 static void service_up(struct connman_ipconfig *ipconfig)
5179 {
5180         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5181
5182         DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
5183
5184         link_changed(service);
5185
5186         service->stats.valid = FALSE;
5187         service->stats_roaming.valid = FALSE;
5188 }
5189
5190 static void service_down(struct connman_ipconfig *ipconfig)
5191 {
5192         DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
5193 }
5194
5195 static void service_lower_up(struct connman_ipconfig *ipconfig)
5196 {
5197         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5198
5199         DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
5200
5201         stats_start(service);
5202 }
5203
5204 static void service_lower_down(struct connman_ipconfig *ipconfig)
5205 {
5206         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5207
5208         DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
5209
5210         stats_stop(service);
5211         service_save(service);
5212 }
5213
5214 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5215 {
5216         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5217         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5218         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5219
5220         DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
5221
5222         type = __connman_ipconfig_get_config_type(ipconfig);
5223         method = __connman_ipconfig_get_method(ipconfig);
5224
5225         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5226                                                         type, method);
5227
5228         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5229                         method == CONNMAN_IPCONFIG_METHOD_AUTO)
5230                 __connman_service_ipconfig_indicate_state(service,
5231                                                 CONNMAN_SERVICE_STATE_READY,
5232                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
5233
5234         settings_changed(service, ipconfig);
5235 }
5236
5237 static void service_ip_release(struct connman_ipconfig *ipconfig)
5238 {
5239         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5240         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5241         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5242
5243         DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
5244
5245         type = __connman_ipconfig_get_config_type(ipconfig);
5246         method = __connman_ipconfig_get_method(ipconfig);
5247
5248         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5249                                                         type, method);
5250
5251         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5252                         method == CONNMAN_IPCONFIG_METHOD_OFF)
5253                 __connman_service_ipconfig_indicate_state(service,
5254                                         CONNMAN_SERVICE_STATE_DISCONNECT,
5255                                         CONNMAN_IPCONFIG_TYPE_IPV6);
5256
5257         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5258                         method == CONNMAN_IPCONFIG_METHOD_OFF)
5259                 __connman_service_ipconfig_indicate_state(service,
5260                                         CONNMAN_SERVICE_STATE_DISCONNECT,
5261                                         CONNMAN_IPCONFIG_TYPE_IPV4);
5262
5263         settings_changed(service, ipconfig);
5264 }
5265
5266 static const struct connman_ipconfig_ops service_ops = {
5267         .up             = service_up,
5268         .down           = service_down,
5269         .lower_up       = service_lower_up,
5270         .lower_down     = service_lower_down,
5271         .ip_bound       = service_ip_bound,
5272         .ip_release     = service_ip_release,
5273 };
5274
5275 static void setup_ip4config(struct connman_service *service, int index,
5276                         enum connman_ipconfig_method method)
5277 {
5278         if (index < 0)
5279                 return;
5280
5281         service->ipconfig_ipv4 = connman_ipconfig_create(index,
5282                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
5283         if (service->ipconfig_ipv4 == NULL)
5284                 return;
5285
5286         connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5287
5288         connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5289
5290         connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5291 }
5292
5293 static void setup_ip6config(struct connman_service *service, int index)
5294 {
5295         if (index < 0)
5296                 return;
5297
5298         service->ipconfig_ipv6 = connman_ipconfig_create(index,
5299                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
5300         if (service->ipconfig_ipv6 == NULL)
5301                 return;
5302
5303         connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5304
5305         connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5306 }
5307
5308 void __connman_service_read_ip4config(struct connman_service *service)
5309 {
5310         GKeyFile *keyfile;
5311
5312         if (service->ipconfig_ipv4 == NULL)
5313                 return;
5314
5315         keyfile = connman_storage_load_service(service->identifier);
5316         if (keyfile == NULL)
5317                 return;
5318
5319         __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5320                                 service->identifier, "IPv4.");
5321
5322         g_key_file_free(keyfile);
5323 }
5324
5325 void __connman_service_create_ip4config(struct connman_service *service,
5326                                         int index)
5327 {
5328         DBG("ipv4 %p", service->ipconfig_ipv4);
5329
5330         if (service->ipconfig_ipv4 != NULL)
5331                 return;
5332
5333         setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5334         __connman_service_read_ip4config(service);
5335 }
5336
5337 void __connman_service_read_ip6config(struct connman_service *service)
5338 {
5339         GKeyFile *keyfile;
5340
5341         if (service->ipconfig_ipv6 == NULL)
5342                 return;
5343
5344         keyfile = connman_storage_load_service(service->identifier);
5345         if (keyfile == NULL)
5346                 return;
5347
5348         __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5349                                 service->identifier, "IPv6.");
5350
5351         g_key_file_free(keyfile);
5352 }
5353
5354 void __connman_service_create_ip6config(struct connman_service *service,
5355                                                                 int index)
5356 {
5357         DBG("ipv6 %p", service->ipconfig_ipv6);
5358
5359         if (service->ipconfig_ipv6 != NULL)
5360                 return;
5361
5362         setup_ip6config(service, index);
5363
5364         __connman_service_read_ip6config(service);
5365 }
5366
5367 /**
5368  * __connman_service_lookup_from_network:
5369  * @network: network structure
5370  *
5371  * Look up a service by network (reference count will not be increased)
5372  */
5373 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5374 {
5375         struct connman_service *service;
5376         const char *ident, *group;
5377         char *name;
5378
5379         DBG("network %p", network);
5380
5381         if (network == NULL)
5382                 return NULL;
5383
5384         ident = __connman_network_get_ident(network);
5385         if (ident == NULL)
5386                 return NULL;
5387
5388         group = connman_network_get_group(network);
5389         if (group == NULL)
5390                 return NULL;
5391
5392         name = g_strdup_printf("%s_%s_%s",
5393                         __connman_network_get_type(network), ident, group);
5394         service = lookup_by_identifier(name);
5395         g_free(name);
5396
5397         return service;
5398 }
5399
5400 struct connman_service *__connman_service_lookup_from_index(int index)
5401 {
5402         struct connman_service *service;
5403         GSequenceIter *iter;
5404
5405         iter = g_sequence_get_begin_iter(service_list);
5406
5407         while (g_sequence_iter_is_end(iter) == FALSE) {
5408                 service = g_sequence_get(iter);
5409
5410                 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
5411                                                         == index)
5412                         return service;
5413
5414                 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
5415                                                         == index)
5416                         return service;
5417
5418                 iter = g_sequence_iter_next(iter);
5419         }
5420
5421         return NULL;
5422 }
5423
5424 const char *__connman_service_get_ident(struct connman_service *service)
5425 {
5426         return service->identifier;
5427 }
5428
5429 const char *__connman_service_get_path(struct connman_service *service)
5430 {
5431         return service->path;
5432 }
5433
5434 unsigned int __connman_service_get_order(struct connman_service *service)
5435 {
5436         GSequenceIter *iter;
5437
5438         if (service == NULL)
5439                 return 0;
5440
5441         if (service->favorite == FALSE) {
5442                 service->order = 0;
5443                 goto done;
5444         }
5445
5446         iter = g_hash_table_lookup(service_hash, service->identifier);
5447         if (iter != NULL) {
5448                 if (g_sequence_iter_get_position(iter) == 0)
5449                         service->order = 1;
5450                 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5451                         service->order = 10;
5452                 else
5453                         service->order = 0;
5454         }
5455
5456 done:
5457         return service->order;
5458 }
5459
5460 static enum connman_service_type convert_network_type(struct connman_network *network)
5461 {
5462         enum connman_network_type type = connman_network_get_type(network);
5463
5464         switch (type) {
5465         case CONNMAN_NETWORK_TYPE_UNKNOWN:
5466         case CONNMAN_NETWORK_TYPE_VENDOR:
5467                 break;
5468         case CONNMAN_NETWORK_TYPE_ETHERNET:
5469                 return CONNMAN_SERVICE_TYPE_ETHERNET;
5470         case CONNMAN_NETWORK_TYPE_WIFI:
5471                 return CONNMAN_SERVICE_TYPE_WIFI;
5472         case CONNMAN_NETWORK_TYPE_WIMAX:
5473                 return CONNMAN_SERVICE_TYPE_WIMAX;
5474         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5475         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5476                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5477         case CONNMAN_NETWORK_TYPE_CELLULAR:
5478                 return CONNMAN_SERVICE_TYPE_CELLULAR;
5479         }
5480
5481         return CONNMAN_SERVICE_TYPE_UNKNOWN;
5482 }
5483
5484 static enum connman_service_security convert_wifi_security(const char *security)
5485 {
5486         if (security == NULL)
5487                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5488         else if (g_str_equal(security, "none") == TRUE)
5489                 return CONNMAN_SERVICE_SECURITY_NONE;
5490         else if (g_str_equal(security, "wep") == TRUE)
5491                 return CONNMAN_SERVICE_SECURITY_WEP;
5492         else if (g_str_equal(security, "psk") == TRUE)
5493                 return CONNMAN_SERVICE_SECURITY_PSK;
5494         else if (g_str_equal(security, "ieee8021x") == TRUE)
5495                 return CONNMAN_SERVICE_SECURITY_8021X;
5496         else if (g_str_equal(security, "wpa") == TRUE)
5497                 return CONNMAN_SERVICE_SECURITY_WPA;
5498         else if (g_str_equal(security, "rsn") == TRUE)
5499                 return CONNMAN_SERVICE_SECURITY_RSN;
5500         else
5501                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5502 }
5503
5504 static void update_from_network(struct connman_service *service,
5505                                         struct connman_network *network)
5506 {
5507         connman_uint8_t strength = service->strength;
5508         GSequenceIter *iter;
5509         const char *str;
5510
5511         DBG("service %p network %p", service, network);
5512
5513         if (is_connected(service) == TRUE)
5514                 return;
5515
5516         if (is_connecting(service) == TRUE)
5517                 return;
5518
5519         str = connman_network_get_string(network, "Name");
5520         if (str != NULL) {
5521                 g_free(service->name);
5522                 service->name = g_strdup(str);
5523                 service->hidden = FALSE;
5524         } else {
5525                 g_free(service->name);
5526                 service->name = NULL;
5527                 service->hidden = TRUE;
5528         }
5529
5530         service->strength = connman_network_get_strength(network);
5531         service->roaming = connman_network_get_bool(network, "Roaming");
5532
5533         if (service->strength == 0) {
5534                 /*
5535                  * Filter out 0-values; it's unclear what they mean
5536                  * and they cause anomalous sorting of the priority list.
5537                  */
5538                 service->strength = strength;
5539         }
5540
5541         str = connman_network_get_string(network, "WiFi.Security");
5542         service->security = convert_wifi_security(str);
5543
5544         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5545                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5546
5547         if (service->strength > strength && service->network != NULL) {
5548                 service->network = network;
5549
5550                 strength_changed(service);
5551         }
5552
5553         if (service->network == NULL)
5554                 service->network = network;
5555
5556         iter = g_hash_table_lookup(service_hash, service->identifier);
5557         if (iter != NULL)
5558                 g_sequence_sort_changed(iter, service_compare, NULL);
5559 }
5560
5561 /**
5562  * __connman_service_create_from_network:
5563  * @network: network structure
5564  *
5565  * Look up service by network and if not found, create one
5566  */
5567 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5568 {
5569         struct connman_service *service;
5570         struct connman_device *device;
5571         const char *ident, *group;
5572         char *name;
5573         int index;
5574
5575         DBG("network %p", network);
5576
5577         if (network == NULL)
5578                 return NULL;
5579
5580         ident = __connman_network_get_ident(network);
5581         if (ident == NULL)
5582                 return NULL;
5583
5584         group = connman_network_get_group(network);
5585         if (group == NULL)
5586                 return NULL;
5587
5588         name = g_strdup_printf("%s_%s_%s",
5589                         __connman_network_get_type(network), ident, group);
5590         service = service_get(name);
5591         g_free(name);
5592
5593         if (service == NULL)
5594                 return NULL;
5595
5596         if (__connman_network_get_weakness(network) == TRUE)
5597                 return service;
5598
5599         if (service->path != NULL) {
5600                 update_from_network(service, network);
5601                 services_changed(TRUE);
5602                 return service;
5603         }
5604
5605         service->type = convert_network_type(network);
5606
5607         switch (service->type) {
5608         case CONNMAN_SERVICE_TYPE_UNKNOWN:
5609         case CONNMAN_SERVICE_TYPE_SYSTEM:
5610         case CONNMAN_SERVICE_TYPE_ETHERNET:
5611         case CONNMAN_SERVICE_TYPE_WIMAX:
5612         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5613         case CONNMAN_SERVICE_TYPE_GPS:
5614         case CONNMAN_SERVICE_TYPE_VPN:
5615         case CONNMAN_SERVICE_TYPE_GADGET:
5616                 service->autoconnect = FALSE;
5617                 break;
5618         case CONNMAN_SERVICE_TYPE_WIFI:
5619         case CONNMAN_SERVICE_TYPE_CELLULAR:
5620                 service->autoconnect = TRUE;
5621                 break;
5622         }
5623
5624         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5625         service->state = combine_state(service->state_ipv4, service->state_ipv6);
5626
5627         update_from_network(service, network);
5628
5629         index = connman_network_get_index(network);
5630
5631         if (service->ipconfig_ipv4 == NULL)
5632                 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5633
5634         if (service->ipconfig_ipv6 == NULL)
5635                 setup_ip6config(service, index);
5636
5637         service_register(service);
5638
5639         if (service->favorite == TRUE) {
5640                 device = connman_network_get_device(service->network);
5641                 if (device && __connman_device_scanning(device) == FALSE)
5642                         __connman_service_auto_connect();
5643         }
5644
5645         __connman_notifier_service_add(service, service->name);
5646
5647         return service;
5648 }
5649
5650 void __connman_service_update_from_network(struct connman_network *network)
5651 {
5652         connman_bool_t need_sort = FALSE;
5653         struct connman_service *service;
5654         connman_uint8_t strength;
5655         connman_bool_t roaming;
5656         GSequenceIter *iter;
5657         const char *name;
5658         connman_bool_t stats_enable;
5659
5660         DBG("network %p", network);
5661
5662         service = __connman_service_lookup_from_network(network);
5663         if (service == NULL)
5664                 return;
5665
5666         if (service->network == NULL)
5667                 return;
5668
5669         name = connman_network_get_string(service->network, "Name");
5670         if (g_strcmp0(service->name, name) != 0) {
5671                 g_free(service->name);
5672                 service->name = g_strdup(name);
5673                 connman_dbus_property_changed_basic(service->path,
5674                                 CONNMAN_SERVICE_INTERFACE, "Name",
5675                                 DBUS_TYPE_STRING, &service->name);
5676         }
5677
5678         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5679                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5680
5681         strength = connman_network_get_strength(service->network);
5682         if (strength == service->strength)
5683                 goto roaming;
5684
5685         service->strength = strength;
5686         need_sort = TRUE;
5687
5688         strength_changed(service);
5689
5690 roaming:
5691         roaming = connman_network_get_bool(service->network, "Roaming");
5692         if (roaming == service->roaming)
5693                 goto sorting;
5694
5695         stats_enable = stats_enabled(service);
5696         if (stats_enable == TRUE)
5697                 stats_stop(service);
5698
5699         service->roaming = roaming;
5700         need_sort = TRUE;
5701
5702         if (stats_enable == TRUE)
5703                 stats_start(service);
5704
5705         roaming_changed(service);
5706
5707 sorting:
5708         if (need_sort == TRUE) {
5709                 iter = g_hash_table_lookup(service_hash, service->identifier);
5710                 if (iter != NULL)
5711                         g_sequence_sort_changed(iter, service_compare, NULL);
5712         }
5713 }
5714
5715 void __connman_service_remove_from_network(struct connman_network *network)
5716 {
5717         struct connman_service *service;
5718
5719         DBG("network %p", network);
5720
5721         service = __connman_service_lookup_from_network(network);
5722         if (service == NULL)
5723                 return;
5724
5725         __connman_connection_gateway_remove(service,
5726                                         CONNMAN_IPCONFIG_TYPE_ALL);
5727
5728         __connman_service_put(service);
5729 }
5730
5731 /**
5732  * __connman_service_create_from_provider:
5733  * @provider: provider structure
5734  *
5735  * Look up service by provider and if not found, create one
5736  */
5737 struct connman_service *
5738 __connman_service_create_from_provider(struct connman_provider *provider)
5739 {
5740         struct connman_service *service;
5741         const char *ident, *str;
5742         char *name;
5743         int index = connman_provider_get_index(provider);
5744
5745         DBG("provider %p", provider);
5746
5747         ident = __connman_provider_get_ident(provider);
5748         if (ident == NULL)
5749                 return NULL;
5750
5751         name = g_strdup_printf("vpn_%s", ident);
5752         service = service_get(name);
5753         g_free(name);
5754
5755         if (service == NULL)
5756                 return NULL;
5757
5758         service->type = CONNMAN_SERVICE_TYPE_VPN;
5759         service->provider = connman_provider_ref(provider);
5760         service->autoconnect = FALSE;
5761         service->userconnect = TRUE;
5762
5763         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5764
5765         str = connman_provider_get_string(provider, "Name");
5766         if (str != NULL) {
5767                 g_free(service->name);
5768                 service->name = g_strdup(str);
5769                 service->hidden = FALSE;
5770         } else {
5771                 g_free(service->name);
5772                 service->name = NULL;
5773                 service->hidden = TRUE;
5774         }
5775
5776         service->strength = 0;
5777
5778         if (service->ipconfig_ipv4 == NULL)
5779                 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5780
5781         if (service->ipconfig_ipv6 == NULL)
5782                 setup_ip6config(service, index);
5783
5784         service_register(service);
5785
5786         __connman_notifier_service_add(service, service->name);
5787
5788         return service;
5789 }
5790
5791 int __connman_service_init(void)
5792 {
5793         DBG("");
5794
5795         connection = connman_dbus_get_connection();
5796
5797         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5798                                                                 NULL, NULL);
5799
5800         service_list = g_sequence_new(service_free);
5801
5802         return 0;
5803 }
5804
5805 void __connman_service_cleanup(void)
5806 {
5807         GSequence *list;
5808
5809         DBG("");
5810
5811         list = service_list;
5812         service_list = NULL;
5813         g_sequence_free(list);
5814
5815         g_hash_table_destroy(service_hash);
5816         service_hash = NULL;
5817
5818         g_slist_free(counter_list);
5819         counter_list = NULL;
5820
5821         dbus_connection_unref(connection);
5822 }