Fix to check favorit value into service
[platform/upstream/connman.git] / src / service.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2014  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 #include <ctype.h>
32 #include <stdint.h>
33 #include <pwd.h>
34 #include <utmpx.h>
35
36 #include <connman/storage.h>
37 #include <connman/setting.h>
38 #include <connman/agent.h>
39
40 #include "connman.h"
41
42 #define CONNECT_TIMEOUT         120
43
44 #define USER_ROOT               0
45 #define USER_NONE               (uid_t)-1
46
47 #if defined TIZEN_EXT
48 #define WIFI_BSSID_STR_LEN      18
49 #endif
50
51 static DBusConnection *connection = NULL;
52
53 static GList *service_list = NULL;
54 static GHashTable *service_hash = NULL;
55 static GSList *counter_list = NULL;
56 static unsigned int autoconnect_timeout = 0;
57 static unsigned int vpn_autoconnect_timeout = 0;
58 static struct connman_service *current_default = NULL;
59 static bool services_dirty = false;
60
61 struct connman_stats {
62         bool valid;
63         bool enabled;
64         struct connman_stats_data data_last;
65         struct connman_stats_data data;
66         GTimer *timer;
67 };
68
69 struct connman_stats_counter {
70         bool append_all;
71         struct connman_stats stats;
72         struct connman_stats stats_roaming;
73 };
74
75 struct connman_service_user {
76         uid_t favorite_user;
77         uid_t current_user;
78 };
79
80 #if defined TIZEN_TV_EXT
81 enum connman_dnsconfig_method {
82         CONNMAN_DNSCONFIG_METHOD_UNKNOWN = 0,
83         CONNMAN_DNSCONFIG_METHOD_MANUAL  = 1,
84         CONNMAN_DNSCONFIG_METHOD_DHCP    = 2,
85 };
86 #endif
87
88 struct connman_service {
89         int refcount;
90         char *identifier;
91         char *path;
92         enum connman_service_type type;
93         enum connman_service_security security;
94         enum connman_service_state state;
95         enum connman_service_state state_ipv4;
96         enum connman_service_state state_ipv6;
97         enum connman_service_error error;
98         enum connman_service_connect_reason connect_reason;
99         uint8_t strength;
100         bool favorite;
101         bool immutable;
102         bool hidden;
103         bool ignore;
104         bool autoconnect;
105         GTimeVal modified;
106         unsigned int order;
107         char *name;
108         char *passphrase;
109         bool roaming;
110         bool request_passphrase_input;
111         struct connman_service_user user;
112         struct connman_ipconfig *ipconfig_ipv4;
113         struct connman_ipconfig *ipconfig_ipv6;
114         struct connman_network *network;
115         struct connman_provider *provider;
116         char **nameservers;
117         char **nameservers_config;
118         char **nameservers_auto;
119         char **domains;
120         char *hostname;
121         char *domainname;
122         char **timeservers;
123         char **timeservers_config;
124         /* 802.1x settings from the config files */
125         char *eap;
126         char *identity;
127         char *agent_identity;
128         char *ca_cert_file;
129         char *client_cert_file;
130         char *private_key_file;
131         char *private_key_passphrase;
132         char *phase2;
133         DBusMessage *pending;
134         DBusMessage *provider_pending;
135         guint timeout;
136         struct connman_stats stats;
137         struct connman_stats stats_roaming;
138         GHashTable *counter_table;
139         enum connman_service_proxy_method proxy;
140         enum connman_service_proxy_method proxy_config;
141         char **proxies;
142         char **excludes;
143         char *pac;
144         bool wps;
145         int online_check_count;
146         bool do_split_routing;
147         bool new_service;
148         bool hidden_service;
149         char *config_file;
150         char *config_entry;
151 #if defined TIZEN_EXT
152         /*
153          * Description: TIZEN implements system global connection management.
154          *              It's only for PDP (cellular) bearer. Wi-Fi is managed
155          *              by ConnMan automatically. Reference count can help to
156          *              manage open/close connection requests by each application.
157          */
158         int user_pdn_connection_refcount;
159         bool storage_reload;
160         /*
161          * Description: In case of EAP security type,
162          *                                user can select the keymgmt type for roaming(802.11r).
163          *                                - FT, CCKM, OKC, ...
164          */
165         char *keymgmt_type;
166         int disconnect_reason;
167 #endif
168 #if defined TIZEN_TV_EXT
169         enum connman_dnsconfig_method dns_config_method;
170 #endif
171 };
172
173 static bool allow_property_changed(struct connman_service *service);
174
175 static struct connman_ipconfig *create_ip4config(struct connman_service *service,
176                 int index, enum connman_ipconfig_method method);
177 static struct connman_ipconfig *create_ip6config(struct connman_service *service,
178                 int index);
179
180
181 struct find_data {
182         const char *path;
183         struct connman_service *service;
184 };
185
186 #if defined TIZEN_EXT
187 /*
188  * Public APIs to use user_pdn_connection_refcount
189  */
190 void connman_service_user_pdn_connection_ref(struct connman_service *service)
191 {
192         __sync_fetch_and_add(&service->user_pdn_connection_refcount, 1);
193
194         DBG("User made PDN connection referenced: %d",
195                                 service->user_pdn_connection_refcount);
196 }
197
198 gboolean connman_service_user_pdn_connection_unref_and_test(
199                                         struct connman_service *service)
200 {
201         __sync_synchronize();
202
203         DBG("User made PDN connection referenced: %d, which will be decreased",
204                                 service->user_pdn_connection_refcount);
205
206         if (service->user_pdn_connection_refcount < 1)
207                 return TRUE;
208
209         if (__sync_sub_and_fetch(&service->user_pdn_connection_refcount, 1) == 0)
210                 return TRUE;
211
212         return FALSE;
213 }
214
215 gboolean connman_service_is_no_ref_user_pdn_connection(
216                                         struct connman_service *cellular)
217 {
218         if (cellular == NULL)
219                 return TRUE;
220
221         __sync_synchronize();
222         if (cellular->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
223                         cellular->user_pdn_connection_refcount == 0)
224                 return TRUE;
225
226         return FALSE;
227 }
228 #endif
229
230 static void compare_path(gpointer value, gpointer user_data)
231 {
232         struct connman_service *service = value;
233         struct find_data *data = user_data;
234
235         if (data->service)
236                 return;
237
238         if (g_strcmp0(service->path, data->path) == 0)
239                 data->service = service;
240 }
241
242 static struct connman_service *find_service(const char *path)
243 {
244         struct find_data data = { .path = path, .service = NULL };
245
246         DBG("path %s", path);
247
248         g_list_foreach(service_list, compare_path, &data);
249
250         return data.service;
251 }
252
253 static const char *reason2string(enum connman_service_connect_reason reason)
254 {
255
256         switch (reason) {
257         case CONNMAN_SERVICE_CONNECT_REASON_NONE:
258                 return "none";
259         case CONNMAN_SERVICE_CONNECT_REASON_USER:
260                 return "user";
261         case CONNMAN_SERVICE_CONNECT_REASON_AUTO:
262                 return "auto";
263         case CONNMAN_SERVICE_CONNECT_REASON_SESSION:
264                 return "session";
265         }
266
267         return "unknown";
268 }
269
270 const char *__connman_service_type2string(enum connman_service_type type)
271 {
272         switch (type) {
273         case CONNMAN_SERVICE_TYPE_UNKNOWN:
274                 break;
275         case CONNMAN_SERVICE_TYPE_SYSTEM:
276                 return "system";
277         case CONNMAN_SERVICE_TYPE_ETHERNET:
278                 return "ethernet";
279         case CONNMAN_SERVICE_TYPE_WIFI:
280                 return "wifi";
281         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
282                 return "bluetooth";
283         case CONNMAN_SERVICE_TYPE_CELLULAR:
284                 return "cellular";
285         case CONNMAN_SERVICE_TYPE_GPS:
286                 return "gps";
287         case CONNMAN_SERVICE_TYPE_VPN:
288                 return "vpn";
289         case CONNMAN_SERVICE_TYPE_GADGET:
290                 return "gadget";
291         case CONNMAN_SERVICE_TYPE_P2P:
292                 return "p2p";
293         }
294
295         return NULL;
296 }
297
298 enum connman_service_type __connman_service_string2type(const char *str)
299 {
300         if (!str)
301                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
302
303         if (strcmp(str, "ethernet") == 0)
304                 return CONNMAN_SERVICE_TYPE_ETHERNET;
305         if (strcmp(str, "gadget") == 0)
306                 return CONNMAN_SERVICE_TYPE_GADGET;
307         if (strcmp(str, "wifi") == 0)
308                 return CONNMAN_SERVICE_TYPE_WIFI;
309         if (strcmp(str, "cellular") == 0)
310                 return CONNMAN_SERVICE_TYPE_CELLULAR;
311         if (strcmp(str, "bluetooth") == 0)
312                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
313         if (strcmp(str, "vpn") == 0)
314                 return CONNMAN_SERVICE_TYPE_VPN;
315         if (strcmp(str, "gps") == 0)
316                 return CONNMAN_SERVICE_TYPE_GPS;
317         if (strcmp(str, "system") == 0)
318                 return CONNMAN_SERVICE_TYPE_SYSTEM;
319         if (strcmp(str, "p2p") == 0)
320                 return CONNMAN_SERVICE_TYPE_P2P;
321
322         return CONNMAN_SERVICE_TYPE_UNKNOWN;
323 }
324
325 enum connman_service_security __connman_service_string2security(const char *str)
326 {
327         if (!str)
328                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
329
330         if (!strcmp(str, "psk"))
331                 return CONNMAN_SERVICE_SECURITY_PSK;
332         if (!strcmp(str, "ieee8021x"))
333                 return CONNMAN_SERVICE_SECURITY_8021X;
334         if (!strcmp(str, "none"))
335                 return CONNMAN_SERVICE_SECURITY_NONE;
336         if (!strcmp(str, "wep"))
337                 return CONNMAN_SERVICE_SECURITY_WEP;
338 #if defined TIZEN_EXT
339         if (!strcmp(str, "rsn"))
340                 return CONNMAN_SERVICE_SECURITY_RSN;
341 #endif
342
343         return CONNMAN_SERVICE_SECURITY_UNKNOWN;
344 }
345
346 static const char *security2string(enum connman_service_security security)
347 {
348         switch (security) {
349         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
350                 break;
351         case CONNMAN_SERVICE_SECURITY_NONE:
352                 return "none";
353         case CONNMAN_SERVICE_SECURITY_WEP:
354                 return "wep";
355         case CONNMAN_SERVICE_SECURITY_PSK:
356         case CONNMAN_SERVICE_SECURITY_WPA:
357 #if defined TIZEN_EXT
358                 return "psk";
359         case CONNMAN_SERVICE_SECURITY_RSN:
360                 return "rsn";
361 #else
362         case CONNMAN_SERVICE_SECURITY_RSN:
363                 return "psk";
364 #endif
365         case CONNMAN_SERVICE_SECURITY_8021X:
366                 return "ieee8021x";
367         }
368
369         return NULL;
370 }
371
372 static const char *state2string(enum connman_service_state state)
373 {
374         switch (state) {
375         case CONNMAN_SERVICE_STATE_UNKNOWN:
376                 break;
377         case CONNMAN_SERVICE_STATE_IDLE:
378                 return "idle";
379         case CONNMAN_SERVICE_STATE_ASSOCIATION:
380                 return "association";
381         case CONNMAN_SERVICE_STATE_CONFIGURATION:
382                 return "configuration";
383         case CONNMAN_SERVICE_STATE_READY:
384                 return "ready";
385         case CONNMAN_SERVICE_STATE_ONLINE:
386                 return "online";
387         case CONNMAN_SERVICE_STATE_DISCONNECT:
388                 return "disconnect";
389         case CONNMAN_SERVICE_STATE_FAILURE:
390                 return "failure";
391         }
392
393         return NULL;
394 }
395
396 static const char *error2string(enum connman_service_error error)
397 {
398         switch (error) {
399         case CONNMAN_SERVICE_ERROR_UNKNOWN:
400                 break;
401         case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
402                 return "out-of-range";
403         case CONNMAN_SERVICE_ERROR_PIN_MISSING:
404                 return "pin-missing";
405         case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
406                 return "dhcp-failed";
407         case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
408                 return "connect-failed";
409         case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
410                 return "login-failed";
411         case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
412                 return "auth-failed";
413         case CONNMAN_SERVICE_ERROR_INVALID_KEY:
414                 return "invalid-key";
415         }
416
417         return NULL;
418 }
419
420 static const char *proxymethod2string(enum connman_service_proxy_method method)
421 {
422         switch (method) {
423         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
424                 return "direct";
425         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
426                 return "manual";
427         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
428                 return "auto";
429         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
430                 break;
431         }
432
433         return NULL;
434 }
435
436 static enum connman_service_proxy_method string2proxymethod(const char *method)
437 {
438         if (g_strcmp0(method, "direct") == 0)
439                 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
440         else if (g_strcmp0(method, "auto") == 0)
441                 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
442         else if (g_strcmp0(method, "manual") == 0)
443                 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
444         else
445                 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
446 }
447
448 #if defined TIZEN_TV_EXT
449 static const char *__connman_dnsconfig_method2string(enum connman_dnsconfig_method method)
450 {
451         switch (method) {
452         case CONNMAN_DNSCONFIG_METHOD_UNKNOWN:
453                 return "unknown";
454         case CONNMAN_DNSCONFIG_METHOD_MANUAL:
455                 return "manual";
456         case CONNMAN_DNSCONFIG_METHOD_DHCP:
457                 return "dhcp";
458         }
459
460         return NULL;
461 }
462
463 static enum connman_dnsconfig_method __connman_dnsconfig_string2method(
464                 const char *method)
465 {
466         if (g_strcmp0(method, "manual") == 0)
467                 return CONNMAN_DNSCONFIG_METHOD_MANUAL;
468         else if (g_strcmp0(method, "dhcp") == 0)
469                 return CONNMAN_DNSCONFIG_METHOD_DHCP;
470         else
471                 return CONNMAN_DNSCONFIG_METHOD_UNKNOWN;
472 }
473 #endif
474
475 static bool
476 connman_service_is_user_allowed(struct connman_service *service, uid_t uid)
477 {
478         uid_t favorite_user = service->user.favorite_user;
479         uid_t current_user = uid;
480
481         DBG("Service favorite UID: %d, current UID: %d", favorite_user, current_user);
482         if (favorite_user == USER_NONE || current_user == USER_ROOT)
483                 return true;
484
485         if (favorite_user != current_user || current_user == USER_NONE) {
486                 DBG("Current user is not a favorite user to this service!");
487                 return false;
488         }
489
490         return true;
491 }
492
493 #if !defined TIZEN_EXT
494 static GList *connman_service_get_login_users()
495 {
496         struct utmpx *utmp;
497         struct passwd *pwd;
498         GList *user_list = NULL;
499
500         setutxent();
501
502         while ((utmp = getutxent()) != NULL) {
503                 DBG("User Name: %s", utmp->ut_user);
504
505                 pwd = getpwnam(utmp->ut_user);
506                 if (pwd) {
507                         if (!g_list_find(user_list, GUINT_TO_POINTER(pwd->pw_uid)))
508                                 user_list = g_list_append(user_list,
509                                                 GUINT_TO_POINTER(pwd->pw_uid));
510
511                         DBG("User Name: %s, UID: %d", utmp->ut_user, pwd->pw_uid);
512                 }
513         }
514
515         endutxent();
516
517         return user_list;
518 }
519 #endif
520
521 static bool is_service_owner_user_login(struct connman_service *service)
522 {
523 #if defined TIZEN_EXT
524         return true;
525 #else
526         GList *list, *user_list;
527         bool ret = false;
528
529         /* Here we only care about wifi service */
530         if (service->type != CONNMAN_SERVICE_TYPE_WIFI)
531                 return true;
532
533         DBG("service favorite user id is: %d", service->user.favorite_user);
534
535         user_list = connman_service_get_login_users();
536         if (user_list == NULL) {
537                 DBG("Can not get any logged in user info.");
538                 return true;
539         }
540
541         for (list = user_list; list; list = list->next) {
542                 uid_t uid = GPOINTER_TO_UINT(list->data);
543
544                 DBG("login user id is %d", uid);
545
546                 if (service->user.favorite_user == uid) {
547                         ret = true;
548                         break;
549                 }
550         }
551
552         g_list_free(user_list);
553
554         return ret;
555 #endif
556 }
557
558 int __connman_service_load_modifiable(struct connman_service *service)
559 {
560         GKeyFile *keyfile;
561         GError *error = NULL;
562         gchar *str;
563         bool autoconnect;
564
565         DBG("service %p", service);
566
567         keyfile = connman_storage_load_service(service->identifier);
568         if (!keyfile)
569                 return -EIO;
570
571         switch (service->type) {
572         case CONNMAN_SERVICE_TYPE_UNKNOWN:
573         case CONNMAN_SERVICE_TYPE_SYSTEM:
574         case CONNMAN_SERVICE_TYPE_GPS:
575         case CONNMAN_SERVICE_TYPE_P2P:
576                 break;
577         case CONNMAN_SERVICE_TYPE_VPN:
578                 service->do_split_routing = g_key_file_get_boolean(keyfile,
579                                 service->identifier, "SplitRouting", NULL);
580                 /* fall through */
581         case CONNMAN_SERVICE_TYPE_WIFI:
582         case CONNMAN_SERVICE_TYPE_GADGET:
583         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
584         case CONNMAN_SERVICE_TYPE_CELLULAR:
585         case CONNMAN_SERVICE_TYPE_ETHERNET:
586                 autoconnect = g_key_file_get_boolean(keyfile,
587                                 service->identifier, "AutoConnect", &error);
588                 if (!error)
589                         service->autoconnect = autoconnect;
590                 g_clear_error(&error);
591                 break;
592         }
593
594         str = g_key_file_get_string(keyfile,
595                                 service->identifier, "Modified", NULL);
596         if (str) {
597                 g_time_val_from_iso8601(str, &service->modified);
598                 g_free(str);
599         }
600
601         g_key_file_free(keyfile);
602
603         return 0;
604 }
605
606 static int service_load_passphrase(struct connman_service *service)
607 {
608         GKeyFile *keyfile;
609         gchar *str;
610
611         keyfile = connman_storage_load_service(service->identifier);
612         if (!keyfile)
613                 return -EIO;
614
615         str = g_key_file_get_string(keyfile,
616                                 service->identifier, "Passphrase", NULL);
617         if (str)
618                 service->passphrase = str;
619
620         return 0;
621 }
622
623 static int service_load(struct connman_service *service)
624 {
625         GKeyFile *keyfile;
626         GError *error = NULL;
627         gsize length;
628         gchar *str;
629         bool autoconnect;
630         unsigned int ssid_len;
631         int err = 0;
632
633         DBG("service %p", service);
634
635         keyfile = connman_storage_load_service(service->identifier);
636         if (!keyfile) {
637                 service->new_service = true;
638                 return -EIO;
639         } else
640                 service->new_service = false;
641
642         switch (service->type) {
643         case CONNMAN_SERVICE_TYPE_UNKNOWN:
644         case CONNMAN_SERVICE_TYPE_SYSTEM:
645         case CONNMAN_SERVICE_TYPE_GPS:
646         case CONNMAN_SERVICE_TYPE_P2P:
647                 break;
648         case CONNMAN_SERVICE_TYPE_VPN:
649                 service->do_split_routing = g_key_file_get_boolean(keyfile,
650                                 service->identifier, "SplitRouting", NULL);
651                 autoconnect = g_key_file_get_boolean(keyfile,
652                                 service->identifier, "AutoConnect", &error);
653                 if (!error)
654                         service->autoconnect = autoconnect;
655                 g_clear_error(&error);
656                 break;
657         case CONNMAN_SERVICE_TYPE_WIFI:
658                 if (!service->name) {
659                         gchar *name;
660
661                         name = g_key_file_get_string(keyfile,
662                                         service->identifier, "Name", NULL);
663                         if (name) {
664                                 g_free(service->name);
665                                 service->name = name;
666                         }
667
668                         if (service->network)
669                                 connman_network_set_name(service->network,
670                                                                         name);
671                 }
672
673                 if (service->network &&
674                                 !connman_network_get_blob(service->network,
675                                                 "WiFi.SSID", &ssid_len)) {
676                         gchar *hex_ssid;
677
678                         hex_ssid = g_key_file_get_string(keyfile,
679                                                         service->identifier,
680                                                                 "SSID", NULL);
681
682                         if (hex_ssid) {
683                                 gchar *ssid;
684                                 unsigned int i, j = 0, hex;
685                                 size_t hex_ssid_len = strlen(hex_ssid);
686
687                                 ssid = g_try_malloc0(hex_ssid_len / 2);
688                                 if (!ssid) {
689                                         g_free(hex_ssid);
690                                         err = -ENOMEM;
691                                         goto done;
692                                 }
693
694                                 for (i = 0; i < hex_ssid_len; i += 2) {
695                                         sscanf(hex_ssid + i, "%02x", &hex);
696                                         ssid[j++] = hex;
697                                 }
698
699                                 connman_network_set_blob(service->network,
700                                         "WiFi.SSID", ssid, hex_ssid_len / 2);
701
702                                 g_free(ssid);
703                         }
704
705                         g_free(hex_ssid);
706                 }
707                 /* fall through */
708
709         case CONNMAN_SERVICE_TYPE_GADGET:
710         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
711         case CONNMAN_SERVICE_TYPE_CELLULAR:
712                 service->favorite = g_key_file_get_boolean(keyfile,
713                                 service->identifier, "Favorite", NULL);
714
715                 /* fall through */
716
717         case CONNMAN_SERVICE_TYPE_ETHERNET:
718                 autoconnect = g_key_file_get_boolean(keyfile,
719                                 service->identifier, "AutoConnect", &error);
720                 if (!error)
721                         service->autoconnect = autoconnect;
722                 g_clear_error(&error);
723                 break;
724         }
725
726         str = g_key_file_get_string(keyfile,
727                                 service->identifier, "Modified", NULL);
728         if (str) {
729                 g_time_val_from_iso8601(str, &service->modified);
730                 g_free(str);
731         }
732
733         str = g_key_file_get_string(keyfile,
734                                 service->identifier, "Passphrase", NULL);
735         if (str) {
736                 g_free(service->passphrase);
737                 service->passphrase = str;
738         }
739
740         if (service->ipconfig_ipv4)
741                 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
742                                         service->identifier, "IPv4.");
743
744         if (service->ipconfig_ipv6)
745                 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
746                                         service->identifier, "IPv6.");
747
748         service->nameservers_config = g_key_file_get_string_list(keyfile,
749                         service->identifier, "Nameservers", &length, NULL);
750         if (service->nameservers_config && length == 0) {
751                 g_strfreev(service->nameservers_config);
752                 service->nameservers_config = NULL;
753         }
754
755 #if defined TIZEN_TV_EXT
756         char *dns_method;
757         dns_method = g_key_file_get_string(keyfile, service->identifier,
758                         "Nameservers.method", NULL);
759         service->dns_config_method = __connman_dnsconfig_string2method(dns_method);
760 #endif
761
762         service->timeservers_config = g_key_file_get_string_list(keyfile,
763                         service->identifier, "Timeservers", &length, NULL);
764         if (service->timeservers_config && length == 0) {
765                 g_strfreev(service->timeservers_config);
766                 service->timeservers_config = NULL;
767         }
768
769         service->domains = g_key_file_get_string_list(keyfile,
770                         service->identifier, "Domains", &length, NULL);
771         if (service->domains && length == 0) {
772                 g_strfreev(service->domains);
773                 service->domains = NULL;
774         }
775
776         str = g_key_file_get_string(keyfile,
777                                 service->identifier, "Proxy.Method", NULL);
778         if (str)
779                 service->proxy_config = string2proxymethod(str);
780
781         g_free(str);
782
783         service->proxies = g_key_file_get_string_list(keyfile,
784                         service->identifier, "Proxy.Servers", &length, NULL);
785         if (service->proxies && length == 0) {
786                 g_strfreev(service->proxies);
787                 service->proxies = NULL;
788         }
789
790         service->excludes = g_key_file_get_string_list(keyfile,
791                         service->identifier, "Proxy.Excludes", &length, NULL);
792         if (service->excludes && length == 0) {
793                 g_strfreev(service->excludes);
794                 service->excludes = NULL;
795         }
796
797         str = g_key_file_get_string(keyfile,
798                                 service->identifier, "Proxy.URL", NULL);
799         if (str) {
800                 g_free(service->pac);
801                 service->pac = str;
802         }
803
804         service->hidden_service = g_key_file_get_boolean(keyfile,
805                                         service->identifier, "Hidden", NULL);
806
807 #if defined TIZEN_EXT
808         if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
809                         service->security == CONNMAN_SERVICE_SECURITY_8021X) {
810                 str = g_key_file_get_string(keyfile,
811                                 service->identifier, "EAP", NULL);
812                 if (str != NULL) {
813                         g_free(service->eap);
814                         service->eap = str;
815                 }
816
817                 str = g_key_file_get_string(keyfile,
818                                 service->identifier, "Phase2", NULL);
819                 if (str != NULL) {
820                         g_free(service->phase2);
821                         service->phase2 = str;
822                 }
823
824                 str = g_key_file_get_string(keyfile,
825                                 service->identifier, "Identity", NULL);
826                 if (str != NULL) {
827                         g_free(service->identity);
828                         service->identity = str;
829                 }
830
831                 str = g_key_file_get_string(keyfile,
832                                 service->identifier, "CACertFile", NULL);
833                 if (str != NULL) {
834                         g_free(service->ca_cert_file);
835                         service->ca_cert_file = str;
836                 }
837
838                 str = g_key_file_get_string(keyfile,
839                                 service->identifier, "ClientCertFile", NULL);
840                 if (str != NULL) {
841                         g_free(service->client_cert_file);
842                         service->client_cert_file = str;
843                 }
844
845                 str = g_key_file_get_string(keyfile,
846                                 service->identifier, "PrivateKeyFile", NULL);
847                 if (str != NULL) {
848                         g_free(service->private_key_file);
849                         service->private_key_file = str;
850                 }
851
852                 str = g_key_file_get_string(keyfile,
853                                 service->identifier, "PrivateKeyPassphrase", NULL);
854                 if (str != NULL) {
855                         g_free(service->private_key_passphrase);
856                         service->private_key_passphrase = str;
857                 }
858         }
859 #endif
860
861         if (g_key_file_has_key(keyfile, service->identifier, "UID", NULL))
862                 service->user.favorite_user = g_key_file_get_integer(keyfile,
863                                         service->identifier, "UID", NULL);
864 done:
865         g_key_file_free(keyfile);
866
867         return err;
868 }
869
870 static int service_save(struct connman_service *service)
871 {
872         GKeyFile *keyfile;
873         gchar *str;
874         guint freq;
875         const char *cst_str = NULL;
876         int err = 0;
877
878         DBG("service %p new %d", service, service->new_service);
879
880         if (service->new_service)
881                 return -ESRCH;
882
883         keyfile = __connman_storage_open_service(service->identifier);
884         if (!keyfile)
885                 return -EIO;
886
887         if (service->name)
888                 g_key_file_set_string(keyfile, service->identifier,
889                                                 "Name", service->name);
890
891         switch (service->type) {
892         case CONNMAN_SERVICE_TYPE_UNKNOWN:
893         case CONNMAN_SERVICE_TYPE_SYSTEM:
894         case CONNMAN_SERVICE_TYPE_GPS:
895         case CONNMAN_SERVICE_TYPE_P2P:
896                 break;
897         case CONNMAN_SERVICE_TYPE_VPN:
898                 g_key_file_set_boolean(keyfile, service->identifier,
899                                 "SplitRouting", service->do_split_routing);
900                 if (service->favorite)
901                         g_key_file_set_boolean(keyfile, service->identifier,
902                                         "AutoConnect", service->autoconnect);
903                 break;
904         case CONNMAN_SERVICE_TYPE_WIFI:
905                 if (service->network) {
906                         const unsigned char *ssid;
907                         unsigned int ssid_len = 0;
908
909                         if (service->user.favorite_user == USER_NONE)
910                                 g_key_file_remove_key(keyfile, service->identifier,
911                                                         "UID", NULL);
912                         else
913                                 g_key_file_set_integer(keyfile, service->identifier,
914                                                         "UID", service->user.favorite_user);
915
916                         ssid = connman_network_get_blob(service->network,
917                                                         "WiFi.SSID", &ssid_len);
918
919                         if (ssid && ssid_len > 0 && ssid[0] != '\0') {
920                                 char *identifier = service->identifier;
921                                 GString *ssid_str;
922                                 unsigned int i;
923
924                                 ssid_str = g_string_sized_new(ssid_len * 2);
925                                 if (!ssid_str) {
926                                         err = -ENOMEM;
927                                         goto done;
928                                 }
929
930                                 for (i = 0; i < ssid_len; i++)
931                                         g_string_append_printf(ssid_str,
932                                                         "%02x", ssid[i]);
933
934                                 g_key_file_set_string(keyfile, identifier,
935                                                         "SSID", ssid_str->str);
936
937                                 g_string_free(ssid_str, TRUE);
938                         }
939
940                         freq = connman_network_get_frequency(service->network);
941                         g_key_file_set_integer(keyfile, service->identifier,
942                                                 "Frequency", freq);
943                 }
944                 /* fall through */
945
946         case CONNMAN_SERVICE_TYPE_GADGET:
947         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
948         case CONNMAN_SERVICE_TYPE_CELLULAR:
949                 g_key_file_set_boolean(keyfile, service->identifier,
950                                         "Favorite", service->favorite);
951
952                 g_key_file_remove_key(keyfile, service->identifier,
953                                 "Failure", NULL);
954
955                 /* fall through */
956
957         case CONNMAN_SERVICE_TYPE_ETHERNET:
958                 if (service->favorite)
959                         g_key_file_set_boolean(keyfile, service->identifier,
960                                         "AutoConnect", service->autoconnect);
961                 break;
962         }
963
964         str = g_time_val_to_iso8601(&service->modified);
965         if (str) {
966                 g_key_file_set_string(keyfile, service->identifier,
967                                                         "Modified", str);
968                 g_free(str);
969         }
970
971         if (service->user.current_user == service->user.favorite_user) {
972                 if (service->passphrase && strlen(service->passphrase) > 0)
973                         g_key_file_set_string(keyfile, service->identifier,
974                                         "Passphrase", service->passphrase);
975                 else
976                         g_key_file_remove_key(keyfile, service->identifier,
977                                         "Passphrase", NULL);
978         }
979
980         if (service->ipconfig_ipv4)
981                 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
982                                         service->identifier, "IPv4.");
983
984         if (service->ipconfig_ipv6)
985                 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
986                                                 service->identifier, "IPv6.");
987
988         if (service->nameservers_config) {
989                 guint len = g_strv_length(service->nameservers_config);
990
991                 g_key_file_set_string_list(keyfile, service->identifier,
992                                                                 "Nameservers",
993                                 (const gchar **) service->nameservers_config, len);
994         } else
995         g_key_file_remove_key(keyfile, service->identifier,
996                                                         "Nameservers", NULL);
997
998 #if defined TIZEN_TV_EXT
999         if(service->dns_config_method != 0) {
1000                 const char *method;
1001                 method = __connman_dnsconfig_method2string(
1002                                 service->dns_config_method);
1003                 g_key_file_set_string(keyfile, service->identifier,
1004                                 "Nameservers.method", method);
1005         } else
1006         g_key_file_remove_key(keyfile, service->identifier,
1007                                                 "Nameservers.method", NULL);
1008 #endif
1009
1010         if (service->timeservers_config) {
1011                 guint len = g_strv_length(service->timeservers_config);
1012
1013                 g_key_file_set_string_list(keyfile, service->identifier,
1014                                                                 "Timeservers",
1015                                 (const gchar **) service->timeservers_config, len);
1016         } else
1017                 g_key_file_remove_key(keyfile, service->identifier,
1018                                                         "Timeservers", NULL);
1019
1020         if (service->domains) {
1021                 guint len = g_strv_length(service->domains);
1022
1023                 g_key_file_set_string_list(keyfile, service->identifier,
1024                                                                 "Domains",
1025                                 (const gchar **) service->domains, len);
1026         } else
1027                 g_key_file_remove_key(keyfile, service->identifier,
1028                                                         "Domains", NULL);
1029
1030         cst_str = proxymethod2string(service->proxy_config);
1031         if (cst_str)
1032                 g_key_file_set_string(keyfile, service->identifier,
1033                                 "Proxy.Method", cst_str);
1034
1035         if (service->proxies) {
1036                 guint len = g_strv_length(service->proxies);
1037
1038                 g_key_file_set_string_list(keyfile, service->identifier,
1039                                 "Proxy.Servers",
1040                                 (const gchar **) service->proxies, len);
1041         } else
1042                 g_key_file_remove_key(keyfile, service->identifier,
1043                                                 "Proxy.Servers", NULL);
1044
1045         if (service->excludes) {
1046                 guint len = g_strv_length(service->excludes);
1047
1048                 g_key_file_set_string_list(keyfile, service->identifier,
1049                                 "Proxy.Excludes",
1050                                 (const gchar **) service->excludes, len);
1051         } else
1052                 g_key_file_remove_key(keyfile, service->identifier,
1053                                                 "Proxy.Excludes", NULL);
1054
1055         if (service->pac && strlen(service->pac) > 0)
1056                 g_key_file_set_string(keyfile, service->identifier,
1057                                         "Proxy.URL", service->pac);
1058         else
1059                 g_key_file_remove_key(keyfile, service->identifier,
1060                                                         "Proxy.URL", NULL);
1061
1062         if (service->hidden_service)
1063                 g_key_file_set_boolean(keyfile, service->identifier, "Hidden",
1064                                                                         TRUE);
1065
1066         if (service->config_file && strlen(service->config_file) > 0)
1067                 g_key_file_set_string(keyfile, service->identifier,
1068                                 "Config.file", service->config_file);
1069
1070         if (service->config_entry &&
1071                                         strlen(service->config_entry) > 0)
1072                 g_key_file_set_string(keyfile, service->identifier,
1073                                 "Config.ident", service->config_entry);
1074
1075 #if defined TIZEN_EXT
1076         if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
1077                         service->security == CONNMAN_SERVICE_SECURITY_8021X) {
1078                 if (service->eap != NULL && strlen(service->eap) > 0)
1079                         g_key_file_set_string(keyfile, service->identifier,
1080                                         "EAP", service->eap);
1081                 else
1082                         g_key_file_remove_key(keyfile, service->identifier,
1083                                         "EAP", NULL);
1084
1085                 if (service->phase2 != NULL && strlen(service->phase2) > 0)
1086                         g_key_file_set_string(keyfile, service->identifier,
1087                                         "Phase2", service->phase2);
1088                 else
1089                         g_key_file_remove_key(keyfile, service->identifier,
1090                                         "Phase2", NULL);
1091
1092                 if (service->identity != NULL && strlen(service->identity) > 0)
1093                         g_key_file_set_string(keyfile, service->identifier,
1094                                         "Identity", service->identity);
1095                 else
1096                         g_key_file_remove_key(keyfile, service->identifier,
1097                                         "Identity", NULL);
1098
1099                 if (service->ca_cert_file != NULL && strlen(service->ca_cert_file) > 0)
1100                         g_key_file_set_string(keyfile, service->identifier,
1101                                         "CACertFile", service->ca_cert_file);
1102                 else
1103                         g_key_file_remove_key(keyfile, service->identifier,
1104                                         "CACertFile", NULL);
1105
1106                 if (service->client_cert_file != NULL && strlen(service->client_cert_file) > 0)
1107                         g_key_file_set_string(keyfile, service->identifier,
1108                                         "ClientCertFile", service->client_cert_file);
1109                 else
1110                         g_key_file_remove_key(keyfile, service->identifier,
1111                                         "ClientCertFile", NULL);
1112
1113                 if (service->private_key_file != NULL && strlen(service->private_key_file) > 0)
1114                         g_key_file_set_string(keyfile, service->identifier,
1115                                         "PrivateKeyFile", service->private_key_file);
1116                 else
1117                         g_key_file_remove_key(keyfile, service->identifier,
1118                                         "PrivateKeyFile", NULL);
1119
1120                 if (service->private_key_passphrase != NULL && strlen(service->private_key_passphrase) > 0)
1121                         g_key_file_set_string(keyfile, service->identifier,
1122                                         "PrivateKeyPassphrase", service->private_key_passphrase);
1123                 else
1124                         g_key_file_remove_key(keyfile, service->identifier,
1125                                         "PrivateKeyPassphrase", NULL);
1126         }
1127 #endif
1128
1129 done:
1130         __connman_storage_save_service(keyfile, service->identifier);
1131
1132         g_key_file_free(keyfile);
1133
1134         return err;
1135 }
1136
1137 void __connman_service_save(struct connman_service *service)
1138 {
1139         if (!service)
1140                 return;
1141
1142         service_save(service);
1143 }
1144
1145 static enum connman_service_state combine_state(
1146                                         enum connman_service_state state_a,
1147                                         enum connman_service_state state_b)
1148 {
1149         enum connman_service_state result;
1150
1151         if (state_a == state_b) {
1152                 result = state_a;
1153                 goto done;
1154         }
1155
1156         if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
1157                 result = state_b;
1158                 goto done;
1159         }
1160
1161         if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
1162                 result = state_a;
1163                 goto done;
1164         }
1165
1166         if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
1167                 result = state_b;
1168                 goto done;
1169         }
1170
1171         if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
1172                 result = state_a;
1173                 goto done;
1174         }
1175
1176         if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
1177                 result = state_a;
1178                 goto done;
1179         }
1180
1181         if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
1182                 result = state_b;
1183                 goto done;
1184         }
1185
1186         if (state_a == CONNMAN_SERVICE_STATE_READY) {
1187                 result = state_a;
1188                 goto done;
1189         }
1190
1191         if (state_b == CONNMAN_SERVICE_STATE_READY) {
1192                 result = state_b;
1193                 goto done;
1194         }
1195
1196         if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
1197                 result = state_a;
1198                 goto done;
1199         }
1200
1201         if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
1202                 result = state_b;
1203                 goto done;
1204         }
1205
1206         if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
1207                 result = state_a;
1208                 goto done;
1209         }
1210
1211         if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
1212                 result = state_b;
1213                 goto done;
1214         }
1215
1216         if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
1217                 result = state_a;
1218                 goto done;
1219         }
1220
1221         if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
1222                 result = state_b;
1223                 goto done;
1224         }
1225
1226         result = CONNMAN_SERVICE_STATE_FAILURE;
1227
1228 done:
1229         return result;
1230 }
1231
1232 static bool is_connecting_state(struct connman_service *service,
1233                                         enum connman_service_state state)
1234 {
1235         switch (state) {
1236         case CONNMAN_SERVICE_STATE_UNKNOWN:
1237         case CONNMAN_SERVICE_STATE_IDLE:
1238         case CONNMAN_SERVICE_STATE_FAILURE:
1239                 if (service->network)
1240                         return connman_network_get_connecting(service->network);
1241         case CONNMAN_SERVICE_STATE_DISCONNECT:
1242         case CONNMAN_SERVICE_STATE_READY:
1243         case CONNMAN_SERVICE_STATE_ONLINE:
1244                 break;
1245         case CONNMAN_SERVICE_STATE_ASSOCIATION:
1246         case CONNMAN_SERVICE_STATE_CONFIGURATION:
1247                 return true;
1248         }
1249
1250         return false;
1251 }
1252
1253 static bool is_connected_state(const struct connman_service *service,
1254                                         enum connman_service_state state)
1255 {
1256         switch (state) {
1257         case CONNMAN_SERVICE_STATE_UNKNOWN:
1258         case CONNMAN_SERVICE_STATE_IDLE:
1259         case CONNMAN_SERVICE_STATE_ASSOCIATION:
1260         case CONNMAN_SERVICE_STATE_CONFIGURATION:
1261         case CONNMAN_SERVICE_STATE_DISCONNECT:
1262         case CONNMAN_SERVICE_STATE_FAILURE:
1263                 break;
1264         case CONNMAN_SERVICE_STATE_READY:
1265         case CONNMAN_SERVICE_STATE_ONLINE:
1266                 return true;
1267         }
1268
1269         return false;
1270 }
1271
1272 static bool is_idle_state(const struct connman_service *service,
1273                                 enum connman_service_state state)
1274 {
1275         switch (state) {
1276         case CONNMAN_SERVICE_STATE_UNKNOWN:
1277         case CONNMAN_SERVICE_STATE_ASSOCIATION:
1278         case CONNMAN_SERVICE_STATE_CONFIGURATION:
1279         case CONNMAN_SERVICE_STATE_READY:
1280         case CONNMAN_SERVICE_STATE_ONLINE:
1281         case CONNMAN_SERVICE_STATE_DISCONNECT:
1282         case CONNMAN_SERVICE_STATE_FAILURE:
1283                 break;
1284         case CONNMAN_SERVICE_STATE_IDLE:
1285                 return true;
1286         }
1287
1288         return false;
1289 }
1290
1291 static bool is_connecting(struct connman_service *service)
1292 {
1293         return is_connecting_state(service, service->state);
1294 }
1295
1296 static bool is_connected(struct connman_service *service)
1297 {
1298         return is_connected_state(service, service->state);
1299 }
1300
1301 static bool nameserver_available(struct connman_service *service,
1302                                 const char *ns)
1303 {
1304         int family;
1305
1306         family = connman_inet_check_ipaddress(ns);
1307
1308         if (family == AF_INET)
1309                 return is_connected_state(service, service->state_ipv4);
1310
1311         if (family == AF_INET6)
1312                 return is_connected_state(service, service->state_ipv6);
1313
1314         return false;
1315 }
1316
1317 static int nameserver_add(struct connman_service *service,
1318                         const char *nameserver)
1319 {
1320         int index;
1321
1322         if (!nameserver_available(service, nameserver))
1323                 return 0;
1324
1325         index = __connman_service_get_index(service);
1326         if (index < 0)
1327                 return -ENXIO;
1328
1329         return connman_resolver_append(index, NULL, nameserver);
1330 }
1331
1332 static int nameserver_add_all(struct connman_service *service)
1333 {
1334         int i = 0;
1335
1336         if (service->nameservers_config) {
1337                 while (service->nameservers_config[i]) {
1338                         nameserver_add(service, service->nameservers_config[i]);
1339                         i++;
1340                 }
1341
1342                 return 0;
1343         }
1344
1345         if (service->nameservers) {
1346                 while (service->nameservers[i]) {
1347                         nameserver_add(service, service->nameservers[i]);
1348                         i++;
1349                 }
1350         }
1351
1352         return 0;
1353 }
1354
1355 static int nameserver_remove(struct connman_service *service,
1356                         const char *nameserver)
1357 {
1358         int index;
1359
1360         if (!nameserver_available(service, nameserver))
1361                 return 0;
1362
1363         index = __connman_service_get_index(service);
1364         if (index < 0)
1365                 return -ENXIO;
1366
1367         return connman_resolver_remove(index, NULL, nameserver);
1368 }
1369
1370 static int nameserver_remove_all(struct connman_service *service)
1371 {
1372 #if defined TIZEN_EXT
1373         /**
1374           * Skip this function if there is any connected profiles
1375           * that use same interface
1376           */
1377         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
1378                         __connman_service_get_connected_count_of_iface(service) > 0)
1379                 return 0;
1380 #endif
1381         int index, i = 0;
1382
1383         index = __connman_service_get_index(service);
1384         if (index < 0)
1385                 return -ENXIO;
1386
1387         while (service->nameservers_config && service->nameservers_config[i]) {
1388
1389                 nameserver_remove(service, service->nameservers_config[i]);
1390                 i++;
1391         }
1392
1393         i = 0;
1394         while (service->nameservers && service->nameservers[i]) {
1395                 nameserver_remove(service, service->nameservers[i]);
1396                 i++;
1397         }
1398
1399         return 0;
1400 }
1401
1402 static int searchdomain_add_all(struct connman_service *service)
1403 {
1404         int index, i = 0;
1405
1406         if (!is_connected(service))
1407                 return -ENOTCONN;
1408
1409         index = __connman_service_get_index(service);
1410         if (index < 0)
1411                 return -ENXIO;
1412
1413         if (service->domains) {
1414                 while (service->domains[i]) {
1415                         connman_resolver_append(index, service->domains[i],
1416                                                 NULL);
1417                         i++;
1418                 }
1419
1420                 return 0;
1421         }
1422
1423         if (service->domainname)
1424                 connman_resolver_append(index, service->domainname, NULL);
1425
1426         return 0;
1427
1428 }
1429
1430 static int searchdomain_remove_all(struct connman_service *service)
1431 {
1432         int index, i = 0;
1433
1434         if (!is_connected(service))
1435                 return -ENOTCONN;
1436
1437         index = __connman_service_get_index(service);
1438         if (index < 0)
1439                 return -ENXIO;
1440
1441         while (service->domains && service->domains[i]) {
1442                 connman_resolver_remove(index, service->domains[i], NULL);
1443                 i++;
1444         }
1445
1446         if (service->domainname)
1447                 connman_resolver_remove(index, service->domainname, NULL);
1448
1449         return 0;
1450 }
1451
1452 /*
1453  * The is_auto variable is set to true when IPv6 autoconf nameservers are
1454  * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
1455  * for details) and not through service.c
1456  */
1457 int __connman_service_nameserver_append(struct connman_service *service,
1458                                 const char *nameserver, bool is_auto)
1459 {
1460         char **nameservers;
1461         int len, i;
1462
1463         DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
1464
1465         if (!nameserver)
1466                 return -EINVAL;
1467
1468         if (is_auto)
1469                 nameservers = service->nameservers_auto;
1470         else
1471                 nameservers = service->nameservers;
1472
1473         for (i = 0; nameservers && nameservers[i]; i++)
1474                 if (g_strcmp0(nameservers[i], nameserver) == 0)
1475                         return -EEXIST;
1476
1477         if (nameservers) {
1478                 len = g_strv_length(nameservers);
1479                 nameservers = g_try_renew(char *, nameservers, len + 2);
1480         } else {
1481                 len = 0;
1482                 nameservers = g_try_new0(char *, len + 2);
1483         }
1484
1485         if (!nameservers)
1486                 return -ENOMEM;
1487
1488         nameservers[len] = g_strdup(nameserver);
1489         if (!nameservers[len])
1490                 return -ENOMEM;
1491
1492         nameservers[len + 1] = NULL;
1493
1494 #if defined TIZEN_TV_EXT
1495         if(service->dns_config_method == CONNMAN_DNSCONFIG_METHOD_UNKNOWN)
1496                 service->dns_config_method = CONNMAN_DNSCONFIG_METHOD_DHCP;
1497 #endif
1498
1499         if (is_auto) {
1500                 service->nameservers_auto = nameservers;
1501         } else {
1502                 service->nameservers = nameservers;
1503                 nameserver_add(service, nameserver);
1504         }
1505
1506         return 0;
1507 }
1508
1509 int __connman_service_nameserver_remove(struct connman_service *service,
1510                                 const char *nameserver, bool is_auto)
1511 {
1512         char **servers, **nameservers;
1513         bool found = false;
1514         int len, i, j;
1515
1516         DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
1517
1518         if (!nameserver)
1519                 return -EINVAL;
1520
1521         if (is_auto)
1522                 nameservers = service->nameservers_auto;
1523         else
1524                 nameservers = service->nameservers;
1525
1526         if (!nameservers)
1527                 return 0;
1528
1529         for (i = 0; nameservers && nameservers[i]; i++)
1530                 if (g_strcmp0(nameservers[i], nameserver) == 0) {
1531                         found = true;
1532                         break;
1533                 }
1534
1535         if (!found)
1536                 return 0;
1537
1538         len = g_strv_length(nameservers);
1539
1540         if (len == 1) {
1541                 servers = NULL;
1542                 goto set_servers;
1543         }
1544
1545         servers = g_try_new0(char *, len);
1546         if (!servers)
1547                 return -ENOMEM;
1548
1549         for (i = 0, j = 0; i < len; i++) {
1550                 if (g_strcmp0(nameservers[i], nameserver)) {
1551                         servers[j] = nameservers[i];
1552                         j++;
1553                 } else
1554                         g_free(nameservers[i]);
1555
1556                 nameservers[i] = NULL;
1557         }
1558         servers[len - 1] = NULL;
1559
1560 set_servers:
1561         g_strfreev(nameservers);
1562         nameservers = servers;
1563
1564         if (is_auto) {
1565                 service->nameservers_auto = nameservers;
1566         } else {
1567                 service->nameservers = nameservers;
1568                 nameserver_remove(service, nameserver);
1569         }
1570
1571         return 0;
1572 }
1573
1574 void __connman_service_nameserver_clear(struct connman_service *service)
1575 {
1576         nameserver_remove_all(service);
1577
1578         g_strfreev(service->nameservers);
1579         service->nameservers = NULL;
1580
1581         nameserver_add_all(service);
1582 }
1583
1584 static void add_nameserver_route(int family, int index, char *nameserver,
1585                                 const char *gw)
1586 {
1587         switch (family) {
1588         case AF_INET:
1589                 if (connman_inet_compare_subnet(index, nameserver))
1590                         break;
1591
1592                 if (connman_inet_add_host_route(index, nameserver, gw) < 0)
1593                         /* For P-t-P link the above route add will fail */
1594                         connman_inet_add_host_route(index, nameserver, NULL);
1595                 break;
1596
1597         case AF_INET6:
1598                 if (connman_inet_add_ipv6_host_route(index, nameserver,
1599                                                                 gw) < 0)
1600                         connman_inet_add_ipv6_host_route(index, nameserver,
1601                                                         NULL);
1602                 break;
1603         }
1604 }
1605
1606 static void nameserver_add_routes(int index, char **nameservers,
1607                                         const char *gw)
1608 {
1609         int i, ns_family, gw_family;
1610
1611         gw_family = connman_inet_check_ipaddress(gw);
1612         if (gw_family < 0)
1613                 return;
1614
1615         for (i = 0; nameservers[i]; i++) {
1616                 ns_family = connman_inet_check_ipaddress(nameservers[i]);
1617                 if (ns_family < 0 || ns_family != gw_family)
1618                         continue;
1619
1620                 add_nameserver_route(ns_family, index, nameservers[i], gw);
1621         }
1622 }
1623
1624 static void nameserver_del_routes(int index, char **nameservers,
1625                                 enum connman_ipconfig_type type)
1626 {
1627         int i, family;
1628
1629         for (i = 0; nameservers[i]; i++) {
1630                 family = connman_inet_check_ipaddress(nameservers[i]);
1631                 if (family < 0)
1632                         continue;
1633
1634                 switch (family) {
1635                 case AF_INET:
1636                         if (type != CONNMAN_IPCONFIG_TYPE_IPV6)
1637                                 connman_inet_del_host_route(index,
1638                                                         nameservers[i]);
1639                         break;
1640                 case AF_INET6:
1641                         if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
1642                                 connman_inet_del_ipv6_host_route(index,
1643                                                         nameservers[i]);
1644                         break;
1645                 }
1646         }
1647 }
1648
1649 void __connman_service_nameserver_add_routes(struct connman_service *service,
1650                                                 const char *gw)
1651 {
1652         int index;
1653
1654         if (!service)
1655                 return;
1656
1657         index = __connman_service_get_index(service);
1658
1659         if (service->nameservers_config) {
1660                 /*
1661                  * Configured nameserver takes preference over the
1662                  * discoverd nameserver gathered from DHCP, VPN, etc.
1663                  */
1664                 nameserver_add_routes(index, service->nameservers_config, gw);
1665         } else if (service->nameservers) {
1666                 /*
1667                  * We add nameservers host routes for nameservers that
1668                  * are not on our subnet. For those who are, the subnet
1669                  * route will be installed by the time the dns proxy code
1670                  * tries to reach them. The subnet route is installed
1671                  * when setting the interface IP address.
1672                  */
1673                 nameserver_add_routes(index, service->nameservers, gw);
1674         }
1675 }
1676
1677 void __connman_service_nameserver_del_routes(struct connman_service *service,
1678                                         enum connman_ipconfig_type type)
1679 {
1680         int index;
1681
1682         if (!service)
1683                 return;
1684
1685         index = __connman_service_get_index(service);
1686
1687         if (service->nameservers_config)
1688                 nameserver_del_routes(index, service->nameservers_config,
1689                                         type);
1690         else if (service->nameservers)
1691                 nameserver_del_routes(index, service->nameservers, type);
1692 }
1693
1694 static struct connman_stats *stats_get(struct connman_service *service)
1695 {
1696         if (service->roaming)
1697                 return &service->stats_roaming;
1698         else
1699                 return &service->stats;
1700 }
1701
1702 static bool stats_enabled(struct connman_service *service)
1703 {
1704         struct connman_stats *stats = stats_get(service);
1705
1706         return stats->enabled;
1707 }
1708
1709 static void stats_start(struct connman_service *service)
1710 {
1711         struct connman_stats *stats = stats_get(service);
1712
1713         DBG("service %p", service);
1714
1715         if (!stats->timer)
1716                 return;
1717
1718         stats->enabled = true;
1719         stats->data_last.time = stats->data.time;
1720
1721         g_timer_start(stats->timer);
1722 }
1723
1724 static void stats_stop(struct connman_service *service)
1725 {
1726         struct connman_stats *stats = stats_get(service);
1727         unsigned int seconds;
1728
1729         DBG("service %p", service);
1730
1731         if (!stats->timer)
1732                 return;
1733
1734         if (!stats->enabled)
1735                 return;
1736
1737         g_timer_stop(stats->timer);
1738
1739         seconds = g_timer_elapsed(stats->timer, NULL);
1740         stats->data.time = stats->data_last.time + seconds;
1741
1742         stats->enabled = false;
1743 }
1744
1745 static void reset_stats(struct connman_service *service)
1746 {
1747         DBG("service %p", service);
1748
1749         /* home */
1750         service->stats.valid = false;
1751
1752         service->stats.data.rx_packets = 0;
1753         service->stats.data.tx_packets = 0;
1754         service->stats.data.rx_bytes = 0;
1755         service->stats.data.tx_bytes = 0;
1756         service->stats.data.rx_errors = 0;
1757         service->stats.data.tx_errors = 0;
1758         service->stats.data.rx_dropped = 0;
1759         service->stats.data.tx_dropped = 0;
1760         service->stats.data.time = 0;
1761         service->stats.data_last.time = 0;
1762
1763         g_timer_reset(service->stats.timer);
1764
1765         /* roaming */
1766         service->stats_roaming.valid = false;
1767
1768         service->stats_roaming.data.rx_packets = 0;
1769         service->stats_roaming.data.tx_packets = 0;
1770         service->stats_roaming.data.rx_bytes = 0;
1771         service->stats_roaming.data.tx_bytes = 0;
1772         service->stats_roaming.data.rx_errors = 0;
1773         service->stats_roaming.data.tx_errors = 0;
1774         service->stats_roaming.data.rx_dropped = 0;
1775         service->stats_roaming.data.tx_dropped = 0;
1776         service->stats_roaming.data.time = 0;
1777         service->stats_roaming.data_last.time = 0;
1778
1779         g_timer_reset(service->stats_roaming.timer);
1780 }
1781
1782 #if defined TIZEN_EXT
1783 static gboolean __connman_service_is_internet_profile(
1784                 struct connman_service *cellular)
1785 {
1786         const char internet_suffix[] = "_1";
1787
1788         DBG("Service path: %s", cellular->path);
1789
1790         if (g_str_has_suffix(cellular->path, internet_suffix) == TRUE)
1791                 return TRUE;
1792
1793         return FALSE;
1794 }
1795
1796 static gboolean __connman_service_is_tethering_profile(
1797                 struct connman_service *cellular)
1798 {
1799         const char tethering_suffix[] = "_5";
1800
1801         DBG("Service path: %s", cellular->path);
1802
1803         if (g_str_has_suffix(cellular->path, tethering_suffix) == TRUE)
1804                 return TRUE;
1805
1806         return FALSE;
1807 }
1808
1809 struct connman_service *connman_service_get_default_connection(void)
1810 {
1811         GList *list;
1812         struct connman_service *service;
1813         struct connman_service *default_service = NULL;
1814
1815         for (list = service_list; list; list = list->next) {
1816                 service = list->data;
1817
1818                 DBG("service: %p %s %s %s", service, service->name,
1819                                 state2string(service->state),
1820                                 __connman_service_type2string(service->type));
1821
1822                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
1823                                 is_connected(service) == TRUE) {
1824                         return service;
1825                 } else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
1826                                 __connman_service_is_internet_profile(service) == TRUE) {
1827                         if (default_service == NULL)
1828                                 default_service = service;
1829                         else if (is_connected(service) == TRUE &&
1830                                         is_connected(default_service) == FALSE)
1831                                 default_service = service;
1832                 } else if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET &&
1833                                 is_connected(service) == TRUE) {
1834                         if (default_service == NULL)
1835                                 default_service = service;
1836                 } else if (service->type == CONNMAN_SERVICE_TYPE_BLUETOOTH &&
1837                                 is_connected(service) == TRUE) {
1838                         if (default_service == NULL)
1839                                 default_service = service;
1840                 }
1841         }
1842
1843         return default_service;
1844 }
1845 #endif
1846
1847 struct connman_service *__connman_service_get_default(void)
1848 {
1849         struct connman_service *service;
1850
1851         if (!service_list)
1852                 return NULL;
1853
1854         service = service_list->data;
1855
1856         if (!is_connected(service))
1857                 return NULL;
1858
1859         return service;
1860 }
1861
1862 bool __connman_service_index_is_default(int index)
1863 {
1864         struct connman_service *service;
1865
1866         if (index < 0)
1867                 return false;
1868
1869         service = __connman_service_get_default();
1870
1871         return __connman_service_get_index(service) == index;
1872 }
1873
1874 static void default_changed(void)
1875 {
1876         struct connman_service *service = __connman_service_get_default();
1877
1878         if (service == current_default)
1879                 return;
1880
1881         DBG("current default %p %s", current_default,
1882                 current_default ? current_default->identifier : "");
1883         DBG("new default %p %s", service, service ? service->identifier : "");
1884
1885 #if defined TIZEN_EXT
1886         current_default = service;
1887
1888         __connman_service_timeserver_changed(service, NULL);
1889 #else
1890         __connman_service_timeserver_changed(current_default, NULL);
1891
1892         current_default = service;
1893 #endif
1894
1895         if (service) {
1896                 if (service->hostname &&
1897                                 connman_setting_get_bool("AllowHostnameUpdates"))
1898                         __connman_utsname_set_hostname(service->hostname);
1899
1900                 if (service->domainname)
1901                         __connman_utsname_set_domainname(service->domainname);
1902         }
1903
1904         __connman_notifier_default_changed(service);
1905 }
1906
1907 static void state_changed(struct connman_service *service)
1908 {
1909         const char *str;
1910
1911         __connman_notifier_service_state_changed(service, service->state);
1912
1913         str = state2string(service->state);
1914         if (!str)
1915                 return;
1916
1917 #if !defined TIZEN_EXT
1918         if (!allow_property_changed(service))
1919                 return;
1920 #endif
1921
1922         connman_dbus_property_changed_basic(service->path,
1923                                 CONNMAN_SERVICE_INTERFACE, "State",
1924                                                 DBUS_TYPE_STRING, &str);
1925 }
1926
1927 static void strength_changed(struct connman_service *service)
1928 {
1929         if (service->strength == 0)
1930                 return;
1931
1932         if (!allow_property_changed(service))
1933                 return;
1934
1935         connman_dbus_property_changed_basic(service->path,
1936                                 CONNMAN_SERVICE_INTERFACE, "Strength",
1937                                         DBUS_TYPE_BYTE, &service->strength);
1938 }
1939
1940 static void favorite_changed(struct connman_service *service)
1941 {
1942         dbus_bool_t favorite;
1943
1944         if (!service->path)
1945                 return;
1946
1947         if (!allow_property_changed(service))
1948                 return;
1949
1950         favorite = service->favorite;
1951         connman_dbus_property_changed_basic(service->path,
1952                                 CONNMAN_SERVICE_INTERFACE, "Favorite",
1953                                         DBUS_TYPE_BOOLEAN, &favorite);
1954 }
1955
1956 static void immutable_changed(struct connman_service *service)
1957 {
1958         dbus_bool_t immutable;
1959
1960         if (!service->path)
1961                 return;
1962
1963         if (!allow_property_changed(service))
1964                 return;
1965
1966         immutable = service->immutable;
1967         connman_dbus_property_changed_basic(service->path,
1968                                 CONNMAN_SERVICE_INTERFACE, "Immutable",
1969                                         DBUS_TYPE_BOOLEAN, &immutable);
1970 }
1971
1972 static void roaming_changed(struct connman_service *service)
1973 {
1974         dbus_bool_t roaming;
1975
1976         if (!service->path)
1977                 return;
1978
1979         if (!allow_property_changed(service))
1980                 return;
1981
1982         roaming = service->roaming;
1983         connman_dbus_property_changed_basic(service->path,
1984                                 CONNMAN_SERVICE_INTERFACE, "Roaming",
1985                                         DBUS_TYPE_BOOLEAN, &roaming);
1986 }
1987
1988 static void autoconnect_changed(struct connman_service *service)
1989 {
1990         dbus_bool_t autoconnect;
1991
1992         if (!service->path)
1993                 return;
1994
1995         if (!allow_property_changed(service))
1996                 return;
1997
1998         autoconnect = service->autoconnect;
1999         connman_dbus_property_changed_basic(service->path,
2000                                 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
2001                                 DBUS_TYPE_BOOLEAN, &autoconnect);
2002 }
2003
2004 static void append_security(DBusMessageIter *iter, void *user_data)
2005 {
2006         struct connman_service *service = user_data;
2007         const char *str;
2008
2009         str = security2string(service->security);
2010         if (str)
2011                 dbus_message_iter_append_basic(iter,
2012                                 DBUS_TYPE_STRING, &str);
2013
2014         /*
2015          * Some access points incorrectly advertise WPS even when they
2016          * are configured as open or no security, so filter
2017          * appropriately.
2018          */
2019         if (service->wps) {
2020                 switch (service->security) {
2021                 case CONNMAN_SERVICE_SECURITY_PSK:
2022                 case CONNMAN_SERVICE_SECURITY_WPA:
2023                 case CONNMAN_SERVICE_SECURITY_RSN:
2024                         str = "wps";
2025                         dbus_message_iter_append_basic(iter,
2026                                                 DBUS_TYPE_STRING, &str);
2027                         break;
2028                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2029                 case CONNMAN_SERVICE_SECURITY_NONE:
2030                 case CONNMAN_SERVICE_SECURITY_WEP:
2031                 case CONNMAN_SERVICE_SECURITY_8021X:
2032                         break;
2033                 }
2034         }
2035 }
2036
2037 static void append_ethernet(DBusMessageIter *iter, void *user_data)
2038 {
2039         struct connman_service *service = user_data;
2040
2041         if (service->ipconfig_ipv4)
2042                 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
2043                                                                         iter);
2044         else if (service->ipconfig_ipv6)
2045                 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
2046                                                                         iter);
2047 }
2048
2049 static void append_ipv4(DBusMessageIter *iter, void *user_data)
2050 {
2051         struct connman_service *service = user_data;
2052
2053         if (!is_connected_state(service, service->state_ipv4))
2054                 return;
2055
2056         if (service->ipconfig_ipv4)
2057                 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
2058 }
2059
2060 static void append_ipv6(DBusMessageIter *iter, void *user_data)
2061 {
2062         struct connman_service *service = user_data;
2063
2064         if (!is_connected_state(service, service->state_ipv6))
2065                 return;
2066
2067         if (service->ipconfig_ipv6)
2068                 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
2069                                                 service->ipconfig_ipv4);
2070 }
2071
2072 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
2073 {
2074         struct connman_service *service = user_data;
2075
2076         if (service->ipconfig_ipv4)
2077                 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
2078                                                         iter);
2079 }
2080
2081 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
2082 {
2083         struct connman_service *service = user_data;
2084
2085         if (service->ipconfig_ipv6)
2086                 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
2087                                                         iter);
2088 }
2089
2090 static void append_nameservers(DBusMessageIter *iter,
2091                 struct connman_service *service, char **servers)
2092 {
2093         int i;
2094         bool available = true;
2095
2096         for (i = 0; servers[i]; i++) {
2097                 if (service)
2098                         available = nameserver_available(service, servers[i]);
2099
2100                 DBG("servers[%d] %s available %d", i, servers[i], available);
2101
2102                 if (available)
2103                         dbus_message_iter_append_basic(iter,
2104                                         DBUS_TYPE_STRING, &servers[i]);
2105         }
2106 }
2107
2108 static void append_dns(DBusMessageIter *iter, void *user_data)
2109 {
2110         struct connman_service *service = user_data;
2111
2112         if (!is_connected(service))
2113                 return;
2114
2115 #if defined TIZEN_TV_EXT
2116         /* Append DNS Config Type */
2117         const char *str;
2118         str = __connman_dnsconfig_method2string(service->dns_config_method);
2119         if(str != NULL)
2120                 dbus_message_iter_append_basic(iter,
2121                         DBUS_TYPE_STRING, &str);
2122 #endif
2123
2124         if (service->nameservers_config) {
2125                 append_nameservers(iter, service, service->nameservers_config);
2126                 return;
2127         } else {
2128                 if (service->nameservers)
2129                         append_nameservers(iter, service,
2130                                         service->nameservers);
2131
2132                 if (service->nameservers_auto)
2133                         append_nameservers(iter, service,
2134                                         service->nameservers_auto);
2135
2136                 if (!service->nameservers && !service->nameservers_auto) {
2137                         char **ns;
2138
2139                         DBG("append fallback nameservers");
2140
2141                         ns = connman_setting_get_string_list("FallbackNameservers");
2142                         if (ns)
2143                                 append_nameservers(iter, service, ns);
2144                 }
2145         }
2146 }
2147
2148 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
2149 {
2150         struct connman_service *service = user_data;
2151
2152 #if defined TIZEN_TV_EXT
2153         /* Append DNS Config Type */
2154         const char *str;
2155         str = __connman_dnsconfig_method2string(service->dns_config_method);
2156         if(str != NULL)
2157                 dbus_message_iter_append_basic(iter,
2158                         DBUS_TYPE_STRING, &str);
2159 #endif
2160
2161         if (!service->nameservers_config)
2162                 return;
2163
2164         append_nameservers(iter, NULL, service->nameservers_config);
2165 }
2166
2167 static void append_ts(DBusMessageIter *iter, void *user_data)
2168 {
2169         GSList *list = user_data;
2170
2171         while (list) {
2172                 char *timeserver = list->data;
2173
2174                 if (timeserver)
2175                         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
2176                                         &timeserver);
2177
2178                 list = g_slist_next(list);
2179         }
2180 }
2181
2182 static void append_tsconfig(DBusMessageIter *iter, void *user_data)
2183 {
2184         struct connman_service *service = user_data;
2185         int i;
2186
2187         if (!service->timeservers_config)
2188                 return;
2189
2190         for (i = 0; service->timeservers_config[i]; i++) {
2191                 dbus_message_iter_append_basic(iter,
2192                                 DBUS_TYPE_STRING,
2193                                 &service->timeservers_config[i]);
2194         }
2195 }
2196
2197 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
2198 {
2199         struct connman_service *service = user_data;
2200         int i;
2201
2202         if (!service->domains)
2203                 return;
2204
2205         for (i = 0; service->domains[i]; i++)
2206                 dbus_message_iter_append_basic(iter,
2207                                 DBUS_TYPE_STRING, &service->domains[i]);
2208 }
2209
2210 static void append_domain(DBusMessageIter *iter, void *user_data)
2211 {
2212         struct connman_service *service = user_data;
2213
2214         if (!is_connected(service) &&
2215                                 !is_connecting(service))
2216                 return;
2217
2218         if (service->domains)
2219                 append_domainconfig(iter, user_data);
2220         else if (service->domainname)
2221                 dbus_message_iter_append_basic(iter,
2222                                 DBUS_TYPE_STRING, &service->domainname);
2223 }
2224
2225 static void append_proxies(DBusMessageIter *iter, void *user_data)
2226 {
2227         struct connman_service *service = user_data;
2228         int i;
2229
2230         if (!service->proxies)
2231                 return;
2232
2233         for (i = 0; service->proxies[i]; i++)
2234                 dbus_message_iter_append_basic(iter,
2235                                 DBUS_TYPE_STRING, &service->proxies[i]);
2236 }
2237
2238 static void append_excludes(DBusMessageIter *iter, void *user_data)
2239 {
2240         struct connman_service *service = user_data;
2241         int i;
2242
2243         if (!service->excludes)
2244                 return;
2245
2246         for (i = 0; service->excludes[i]; i++)
2247                 dbus_message_iter_append_basic(iter,
2248                                 DBUS_TYPE_STRING, &service->excludes[i]);
2249 }
2250
2251 static void append_proxy(DBusMessageIter *iter, void *user_data)
2252 {
2253         struct connman_service *service = user_data;
2254         enum connman_service_proxy_method proxy;
2255         const char *pac = NULL;
2256         const char *method = proxymethod2string(
2257                 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
2258
2259         if (!is_connected(service))
2260                 return;
2261
2262         proxy = connman_service_get_proxy_method(service);
2263
2264         switch (proxy) {
2265         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2266                 return;
2267         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2268                 goto done;
2269         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2270                 connman_dbus_dict_append_array(iter, "Servers",
2271                                         DBUS_TYPE_STRING, append_proxies,
2272                                         service);
2273
2274                 connman_dbus_dict_append_array(iter, "Excludes",
2275                                         DBUS_TYPE_STRING, append_excludes,
2276                                         service);
2277                 break;
2278         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2279                 /* Maybe DHCP, or WPAD,  has provided an url for a pac file */
2280                 if (service->ipconfig_ipv4)
2281                         pac = __connman_ipconfig_get_proxy_autoconfig(
2282                                 service->ipconfig_ipv4);
2283                 else if (service->ipconfig_ipv6)
2284                         pac = __connman_ipconfig_get_proxy_autoconfig(
2285                                 service->ipconfig_ipv6);
2286
2287                 if (!service->pac && !pac)
2288                         goto done;
2289
2290                 if (service->pac)
2291                         pac = service->pac;
2292
2293                 connman_dbus_dict_append_basic(iter, "URL",
2294                                         DBUS_TYPE_STRING, &pac);
2295                 break;
2296         }
2297
2298         method = proxymethod2string(proxy);
2299
2300 done:
2301         connman_dbus_dict_append_basic(iter, "Method",
2302                                         DBUS_TYPE_STRING, &method);
2303 }
2304
2305 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
2306 {
2307         struct connman_service *service = user_data;
2308         const char *method;
2309
2310         if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
2311                 return;
2312
2313         switch (service->proxy_config) {
2314         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2315                 return;
2316         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2317                 break;
2318         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2319                 if (service->proxies)
2320                         connman_dbus_dict_append_array(iter, "Servers",
2321                                                 DBUS_TYPE_STRING,
2322                                                 append_proxies, service);
2323
2324                 if (service->excludes)
2325                         connman_dbus_dict_append_array(iter, "Excludes",
2326                                                 DBUS_TYPE_STRING,
2327                                                 append_excludes, service);
2328                 break;
2329         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2330                 if (service->pac)
2331                         connman_dbus_dict_append_basic(iter, "URL",
2332                                         DBUS_TYPE_STRING, &service->pac);
2333                 break;
2334         }
2335
2336         method = proxymethod2string(service->proxy_config);
2337
2338         connman_dbus_dict_append_basic(iter, "Method",
2339                                 DBUS_TYPE_STRING, &method);
2340 }
2341
2342 static void append_provider(DBusMessageIter *iter, void *user_data)
2343 {
2344         struct connman_service *service = user_data;
2345
2346         if (!is_connected(service))
2347                 return;
2348
2349         if (service->provider)
2350                 __connman_provider_append_properties(service->provider, iter);
2351 }
2352
2353
2354 static void settings_changed(struct connman_service *service,
2355                                 struct connman_ipconfig *ipconfig)
2356 {
2357         enum connman_ipconfig_type type;
2358
2359         if (!allow_property_changed(service))
2360                 return;
2361
2362         type = __connman_ipconfig_get_config_type(ipconfig);
2363
2364         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
2365                 connman_dbus_property_changed_dict(service->path,
2366                                         CONNMAN_SERVICE_INTERFACE, "IPv4",
2367                                         append_ipv4, service);
2368         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
2369                 connman_dbus_property_changed_dict(service->path,
2370                                         CONNMAN_SERVICE_INTERFACE, "IPv6",
2371                                         append_ipv6, service);
2372
2373         __connman_notifier_ipconfig_changed(service, ipconfig);
2374 }
2375
2376 static void ipv4_configuration_changed(struct connman_service *service)
2377 {
2378         if (!allow_property_changed(service))
2379                 return;
2380
2381         connman_dbus_property_changed_dict(service->path,
2382                                         CONNMAN_SERVICE_INTERFACE,
2383                                                         "IPv4.Configuration",
2384                                                         append_ipv4config,
2385                                                         service);
2386 }
2387
2388 static void ipv6_configuration_changed(struct connman_service *service)
2389 {
2390         if (!allow_property_changed(service))
2391                 return;
2392
2393         connman_dbus_property_changed_dict(service->path,
2394                                         CONNMAN_SERVICE_INTERFACE,
2395                                                         "IPv6.Configuration",
2396                                                         append_ipv6config,
2397                                                         service);
2398 }
2399
2400 static void dns_changed(struct connman_service *service)
2401 {
2402         if (!allow_property_changed(service))
2403                 return;
2404
2405         connman_dbus_property_changed_array(service->path,
2406                                 CONNMAN_SERVICE_INTERFACE, "Nameservers",
2407                                         DBUS_TYPE_STRING, append_dns, service);
2408 }
2409
2410 static void dns_configuration_changed(struct connman_service *service)
2411 {
2412         if (!allow_property_changed(service))
2413                 return;
2414
2415         connman_dbus_property_changed_array(service->path,
2416                                 CONNMAN_SERVICE_INTERFACE,
2417                                 "Nameservers.Configuration",
2418                                 DBUS_TYPE_STRING, append_dnsconfig, service);
2419
2420         dns_changed(service);
2421 }
2422
2423 static void domain_changed(struct connman_service *service)
2424 {
2425         if (!allow_property_changed(service))
2426                 return;
2427
2428         connman_dbus_property_changed_array(service->path,
2429                                 CONNMAN_SERVICE_INTERFACE, "Domains",
2430                                 DBUS_TYPE_STRING, append_domain, service);
2431 }
2432
2433 static void domain_configuration_changed(struct connman_service *service)
2434 {
2435         if (!allow_property_changed(service))
2436                 return;
2437
2438         connman_dbus_property_changed_array(service->path,
2439                                 CONNMAN_SERVICE_INTERFACE,
2440                                 "Domains.Configuration",
2441                                 DBUS_TYPE_STRING, append_domainconfig, service);
2442 }
2443
2444 static void proxy_changed(struct connman_service *service)
2445 {
2446         if (!allow_property_changed(service))
2447                 return;
2448
2449         connman_dbus_property_changed_dict(service->path,
2450                                         CONNMAN_SERVICE_INTERFACE, "Proxy",
2451                                                         append_proxy, service);
2452 }
2453
2454 static void proxy_configuration_changed(struct connman_service *service)
2455 {
2456         if (!allow_property_changed(service))
2457                 return;
2458
2459         connman_dbus_property_changed_dict(service->path,
2460                         CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
2461                                                 append_proxyconfig, service);
2462
2463         proxy_changed(service);
2464 }
2465
2466 static void timeservers_configuration_changed(struct connman_service *service)
2467 {
2468         if (!allow_property_changed(service))
2469                 return;
2470
2471         connman_dbus_property_changed_array(service->path,
2472                         CONNMAN_SERVICE_INTERFACE,
2473                         "Timeservers.Configuration",
2474                         DBUS_TYPE_STRING,
2475                         append_tsconfig, service);
2476 }
2477
2478 static void link_changed(struct connman_service *service)
2479 {
2480         if (!allow_property_changed(service))
2481                 return;
2482
2483         connman_dbus_property_changed_dict(service->path,
2484                                         CONNMAN_SERVICE_INTERFACE, "Ethernet",
2485                                                 append_ethernet, service);
2486 }
2487
2488 static void stats_append_counters(DBusMessageIter *dict,
2489                         struct connman_stats_data *stats,
2490                         struct connman_stats_data *counters,
2491                         bool append_all)
2492 {
2493         if (counters->rx_packets != stats->rx_packets || append_all) {
2494                 counters->rx_packets = stats->rx_packets;
2495                 connman_dbus_dict_append_basic(dict, "RX.Packets",
2496                                         DBUS_TYPE_UINT32, &stats->rx_packets);
2497         }
2498
2499         if (counters->tx_packets != stats->tx_packets || append_all) {
2500                 counters->tx_packets = stats->tx_packets;
2501                 connman_dbus_dict_append_basic(dict, "TX.Packets",
2502                                         DBUS_TYPE_UINT32, &stats->tx_packets);
2503         }
2504
2505         if (counters->rx_bytes != stats->rx_bytes || append_all) {
2506                 counters->rx_bytes = stats->rx_bytes;
2507                 connman_dbus_dict_append_basic(dict, "RX.Bytes",
2508                                         DBUS_TYPE_UINT32, &stats->rx_bytes);
2509         }
2510
2511         if (counters->tx_bytes != stats->tx_bytes || append_all) {
2512                 counters->tx_bytes = stats->tx_bytes;
2513                 connman_dbus_dict_append_basic(dict, "TX.Bytes",
2514                                         DBUS_TYPE_UINT32, &stats->tx_bytes);
2515         }
2516
2517         if (counters->rx_errors != stats->rx_errors || append_all) {
2518                 counters->rx_errors = stats->rx_errors;
2519                 connman_dbus_dict_append_basic(dict, "RX.Errors",
2520                                         DBUS_TYPE_UINT32, &stats->rx_errors);
2521         }
2522
2523         if (counters->tx_errors != stats->tx_errors || append_all) {
2524                 counters->tx_errors = stats->tx_errors;
2525                 connman_dbus_dict_append_basic(dict, "TX.Errors",
2526                                         DBUS_TYPE_UINT32, &stats->tx_errors);
2527         }
2528
2529         if (counters->rx_dropped != stats->rx_dropped || append_all) {
2530                 counters->rx_dropped = stats->rx_dropped;
2531                 connman_dbus_dict_append_basic(dict, "RX.Dropped",
2532                                         DBUS_TYPE_UINT32, &stats->rx_dropped);
2533         }
2534
2535         if (counters->tx_dropped != stats->tx_dropped || append_all) {
2536                 counters->tx_dropped = stats->tx_dropped;
2537                 connman_dbus_dict_append_basic(dict, "TX.Dropped",
2538                                         DBUS_TYPE_UINT32, &stats->tx_dropped);
2539         }
2540
2541         if (counters->time != stats->time || append_all) {
2542                 counters->time = stats->time;
2543                 connman_dbus_dict_append_basic(dict, "Time",
2544                                         DBUS_TYPE_UINT32, &stats->time);
2545         }
2546 }
2547
2548 static void stats_append(struct connman_service *service,
2549                                 const char *counter,
2550                                 struct connman_stats_counter *counters,
2551                                 bool append_all)
2552 {
2553         DBusMessageIter array, dict;
2554         DBusMessage *msg;
2555
2556         DBG("service %p counter %s", service, counter);
2557
2558         msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
2559         if (!msg)
2560                 return;
2561
2562         dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
2563                                 &service->path, DBUS_TYPE_INVALID);
2564
2565         dbus_message_iter_init_append(msg, &array);
2566
2567         /* home counter */
2568         connman_dbus_dict_open(&array, &dict);
2569
2570         stats_append_counters(&dict, &service->stats.data,
2571                                 &counters->stats.data, append_all);
2572
2573         connman_dbus_dict_close(&array, &dict);
2574
2575         /* roaming counter */
2576         connman_dbus_dict_open(&array, &dict);
2577
2578         stats_append_counters(&dict, &service->stats_roaming.data,
2579                                 &counters->stats_roaming.data, append_all);
2580
2581         connman_dbus_dict_close(&array, &dict);
2582
2583         __connman_counter_send_usage(counter, msg);
2584 }
2585
2586 static void stats_update(struct connman_service *service,
2587                                 unsigned int rx_packets, unsigned int tx_packets,
2588                                 unsigned int rx_bytes, unsigned int tx_bytes,
2589                                 unsigned int rx_errors, unsigned int tx_errors,
2590                                 unsigned int rx_dropped, unsigned int tx_dropped)
2591 {
2592         struct connman_stats *stats = stats_get(service);
2593         struct connman_stats_data *data_last = &stats->data_last;
2594         struct connman_stats_data *data = &stats->data;
2595         unsigned int seconds;
2596
2597         DBG("service %p", service);
2598
2599         if (stats->valid) {
2600                 data->rx_packets +=
2601                         rx_packets - data_last->rx_packets;
2602                 data->tx_packets +=
2603                         tx_packets - data_last->tx_packets;
2604                 data->rx_bytes +=
2605                         rx_bytes - data_last->rx_bytes;
2606                 data->tx_bytes +=
2607                         tx_bytes - data_last->tx_bytes;
2608                 data->rx_errors +=
2609                         rx_errors - data_last->rx_errors;
2610                 data->tx_errors +=
2611                         tx_errors - data_last->tx_errors;
2612                 data->rx_dropped +=
2613                         rx_dropped - data_last->rx_dropped;
2614                 data->tx_dropped +=
2615                         tx_dropped - data_last->tx_dropped;
2616         } else {
2617                 stats->valid = true;
2618         }
2619
2620         data_last->rx_packets = rx_packets;
2621         data_last->tx_packets = tx_packets;
2622         data_last->rx_bytes = rx_bytes;
2623         data_last->tx_bytes = tx_bytes;
2624         data_last->rx_errors = rx_errors;
2625         data_last->tx_errors = tx_errors;
2626         data_last->rx_dropped = rx_dropped;
2627         data_last->tx_dropped = tx_dropped;
2628
2629         seconds = g_timer_elapsed(stats->timer, NULL);
2630         stats->data.time = stats->data_last.time + seconds;
2631 }
2632
2633 void __connman_service_notify(struct connman_service *service,
2634                         unsigned int rx_packets, unsigned int tx_packets,
2635                         unsigned int rx_bytes, unsigned int tx_bytes,
2636                         unsigned int rx_errors, unsigned int tx_errors,
2637                         unsigned int rx_dropped, unsigned int tx_dropped)
2638 {
2639         GHashTableIter iter;
2640         gpointer key, value;
2641         const char *counter;
2642         struct connman_stats_counter *counters;
2643         struct connman_stats_data *data;
2644         int err;
2645
2646         if (!service)
2647                 return;
2648
2649         if (!is_connected(service))
2650                 return;
2651
2652         stats_update(service,
2653                 rx_packets, tx_packets,
2654                 rx_bytes, tx_bytes,
2655                 rx_errors, tx_errors,
2656                 rx_dropped, tx_dropped);
2657
2658         data = &stats_get(service)->data;
2659         err = __connman_stats_update(service, service->roaming, data);
2660         if (err < 0)
2661                 connman_error("Failed to store statistics for %s",
2662                                 service->identifier);
2663
2664         g_hash_table_iter_init(&iter, service->counter_table);
2665         while (g_hash_table_iter_next(&iter, &key, &value)) {
2666                 counter = key;
2667                 counters = value;
2668
2669                 stats_append(service, counter, counters, counters->append_all);
2670                 counters->append_all = false;
2671         }
2672 }
2673
2674 int __connman_service_counter_register(const char *counter)
2675 {
2676         struct connman_service *service;
2677         GList *list;
2678         struct connman_stats_counter *counters;
2679
2680         DBG("counter %s", counter);
2681
2682         counter_list = g_slist_prepend(counter_list, (gpointer)counter);
2683
2684         for (list = service_list; list; list = list->next) {
2685                 service = list->data;
2686
2687                 counters = g_try_new0(struct connman_stats_counter, 1);
2688                 if (!counters)
2689                         return -ENOMEM;
2690
2691                 counters->append_all = true;
2692
2693                 g_hash_table_replace(service->counter_table, (gpointer)counter,
2694                                         counters);
2695         }
2696
2697         return 0;
2698 }
2699
2700 void __connman_service_counter_unregister(const char *counter)
2701 {
2702         struct connman_service *service;
2703         GList *list;
2704
2705         DBG("counter %s", counter);
2706
2707         for (list = service_list; list; list = list->next) {
2708                 service = list->data;
2709
2710                 g_hash_table_remove(service->counter_table, counter);
2711         }
2712
2713         counter_list = g_slist_remove(counter_list, counter);
2714 }
2715
2716 int __connman_service_iterate_services(service_iterate_cb cb, void *user_data)
2717 {
2718         GList *list;
2719
2720         for (list = service_list; list; list = list->next) {
2721                 struct connman_service *service = list->data;
2722
2723                 cb(service, user_data);
2724         }
2725
2726         return 0;
2727 }
2728
2729 #if defined TIZEN_EXT
2730 static void append_wifi_ext_info(DBusMessageIter *dict,
2731                                         struct connman_network *network)
2732 {
2733         char bssid_buff[WIFI_BSSID_STR_LEN] = {0,};
2734         char *bssid_str = bssid_buff;
2735         unsigned char *bssid;
2736         unsigned int maxrate;
2737         unsigned int keymgmt;
2738         uint16_t frequency;
2739         const char *enc_mode;
2740         gboolean passpoint;
2741
2742         bssid = connman_network_get_bssid(network);
2743         maxrate = connman_network_get_maxrate(network);
2744         frequency = connman_network_get_frequency(network);
2745         enc_mode = connman_network_get_enc_mode(network);
2746         passpoint = connman_network_get_is_hs20AP(network);
2747         keymgmt = connman_network_get_keymgmt(network);
2748
2749         snprintf(bssid_str, WIFI_BSSID_STR_LEN, "%02x:%02x:%02x:%02x:%02x:%02x",
2750                                 bssid[0], bssid[1], bssid[2],
2751                                 bssid[3], bssid[4], bssid[5]);
2752
2753         connman_dbus_dict_append_basic(dict, "BSSID",
2754                                         DBUS_TYPE_STRING, &bssid_str);
2755         connman_dbus_dict_append_basic(dict, "MaxRate",
2756                                         DBUS_TYPE_UINT32, &maxrate);
2757         connman_dbus_dict_append_basic(dict, "Frequency",
2758                                         DBUS_TYPE_UINT16, &frequency);
2759         connman_dbus_dict_append_basic(dict, "EncryptionMode",
2760                                         DBUS_TYPE_STRING, &enc_mode);
2761         connman_dbus_dict_append_basic(dict, "Passpoint",
2762                                         DBUS_TYPE_BOOLEAN, &passpoint);
2763         connman_dbus_dict_append_basic(dict, "Keymgmt",
2764                                         DBUS_TYPE_UINT32, &keymgmt);
2765 }
2766 #endif
2767
2768 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2769                                         struct connman_service *service)
2770 {
2771         dbus_bool_t val;
2772         const char *str;
2773         GSList *list;
2774
2775 #if defined TIZEN_EXT
2776         unsigned int frequency = 0U;
2777         if (service && service->network) {
2778                 frequency = connman_network_get_frequency(service->network);
2779                 connman_dbus_dict_append_basic(dict, "Frequency",
2780                                 DBUS_TYPE_UINT16, &frequency);
2781         }
2782 #endif
2783
2784         str = __connman_service_type2string(service->type);
2785         if (str)
2786                 connman_dbus_dict_append_basic(dict, "Type",
2787                                                 DBUS_TYPE_STRING, &str);
2788
2789         connman_dbus_dict_append_array(dict, "Security",
2790                                 DBUS_TYPE_STRING, append_security, service);
2791
2792         str = state2string(service->state);
2793         if (str)
2794                 connman_dbus_dict_append_basic(dict, "State",
2795                                                 DBUS_TYPE_STRING, &str);
2796
2797 #if defined TIZEN_TV_EXT
2798         str = state2string(service->state_ipv6);
2799         if (str != NULL)
2800                 connman_dbus_dict_append_basic(dict, "StateIPv6",
2801                                                 DBUS_TYPE_STRING, &str);
2802 #endif
2803
2804         str = error2string(service->error);
2805         if (str)
2806                 connman_dbus_dict_append_basic(dict, "Error",
2807                                                 DBUS_TYPE_STRING, &str);
2808
2809         if (service->strength > 0)
2810                 connman_dbus_dict_append_basic(dict, "Strength",
2811                                         DBUS_TYPE_BYTE, &service->strength);
2812
2813         val = service->favorite;
2814         connman_dbus_dict_append_basic(dict, "Favorite",
2815                                         DBUS_TYPE_BOOLEAN, &val);
2816
2817         val = service->immutable;
2818         connman_dbus_dict_append_basic(dict, "Immutable",
2819                                         DBUS_TYPE_BOOLEAN, &val);
2820
2821         if (service->favorite)
2822                 val = service->autoconnect;
2823         else
2824                 val = service->favorite;
2825
2826         connman_dbus_dict_append_basic(dict, "AutoConnect",
2827                                 DBUS_TYPE_BOOLEAN, &val);
2828
2829         if (service->name)
2830                 connman_dbus_dict_append_basic(dict, "Name",
2831                                         DBUS_TYPE_STRING, &service->name);
2832
2833         switch (service->type) {
2834         case CONNMAN_SERVICE_TYPE_UNKNOWN:
2835         case CONNMAN_SERVICE_TYPE_SYSTEM:
2836         case CONNMAN_SERVICE_TYPE_GPS:
2837         case CONNMAN_SERVICE_TYPE_VPN:
2838         case CONNMAN_SERVICE_TYPE_P2P:
2839                 break;
2840         case CONNMAN_SERVICE_TYPE_CELLULAR:
2841                 val = service->roaming;
2842                 connman_dbus_dict_append_basic(dict, "Roaming",
2843                                         DBUS_TYPE_BOOLEAN, &val);
2844
2845                 connman_dbus_dict_append_dict(dict, "Ethernet",
2846                                                 append_ethernet, service);
2847                 break;
2848         case CONNMAN_SERVICE_TYPE_WIFI:
2849 #if defined TIZEN_EXT
2850                 if (service->network != NULL)
2851                         append_wifi_ext_info(dict, service->network);
2852
2853                 connman_dbus_dict_append_dict(dict, "Ethernet",
2854                                                 append_ethernet, service);
2855
2856                 connman_dbus_dict_append_basic(dict, "DisconnectReason",
2857                                 DBUS_TYPE_INT32, &service->disconnect_reason);
2858
2859                 break;
2860 #endif
2861         case CONNMAN_SERVICE_TYPE_ETHERNET:
2862         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2863         case CONNMAN_SERVICE_TYPE_GADGET:
2864                 connman_dbus_dict_append_dict(dict, "Ethernet",
2865                                                 append_ethernet, service);
2866                 break;
2867         }
2868
2869         connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2870
2871         connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2872                                                 append_ipv4config, service);
2873
2874         connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2875
2876         connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2877                                                 append_ipv6config, service);
2878
2879         connman_dbus_dict_append_array(dict, "Nameservers",
2880                                 DBUS_TYPE_STRING, append_dns, service);
2881
2882         connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2883                                 DBUS_TYPE_STRING, append_dnsconfig, service);
2884
2885         if (service->state == CONNMAN_SERVICE_STATE_READY ||
2886                         service->state == CONNMAN_SERVICE_STATE_ONLINE)
2887                 list = __connman_timeserver_get_all(service);
2888         else
2889                 list = NULL;
2890
2891         connman_dbus_dict_append_array(dict, "Timeservers",
2892                                 DBUS_TYPE_STRING, append_ts, list);
2893
2894         g_slist_free_full(list, g_free);
2895
2896         connman_dbus_dict_append_array(dict, "Timeservers.Configuration",
2897                                 DBUS_TYPE_STRING, append_tsconfig, service);
2898
2899         connman_dbus_dict_append_array(dict, "Domains",
2900                                 DBUS_TYPE_STRING, append_domain, service);
2901
2902         connman_dbus_dict_append_array(dict, "Domains.Configuration",
2903                                 DBUS_TYPE_STRING, append_domainconfig, service);
2904
2905         connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2906
2907         connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2908                                                 append_proxyconfig, service);
2909
2910         connman_dbus_dict_append_dict(dict, "Provider",
2911                                                 append_provider, service);
2912 }
2913
2914 static void append_struct_service(DBusMessageIter *iter,
2915                 connman_dbus_append_cb_t function,
2916                 struct connman_service *service)
2917 {
2918         DBusMessageIter entry, dict;
2919
2920         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2921
2922         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2923                                                         &service->path);
2924
2925         connman_dbus_dict_open(&entry, &dict);
2926         if (function)
2927                 function(&dict, service);
2928         connman_dbus_dict_close(&entry, &dict);
2929
2930         dbus_message_iter_close_container(iter, &entry);
2931 }
2932
2933 static void append_dict_properties(DBusMessageIter *dict, void *user_data)
2934 {
2935         struct connman_service *service = user_data;
2936
2937         append_properties(dict, TRUE, service);
2938 }
2939
2940 static void append_struct(gpointer value, gpointer user_data)
2941 {
2942         struct connman_service *service = value;
2943         DBusMessageIter *iter = user_data;
2944
2945         if (!service->path)
2946                 return;
2947
2948         append_struct_service(iter, append_dict_properties, service);
2949 }
2950
2951 void __connman_service_list_struct(DBusMessageIter *iter)
2952 {
2953         g_list_foreach(service_list, append_struct, iter);
2954 }
2955
2956 bool __connman_service_is_hidden(struct connman_service *service)
2957 {
2958         return service->hidden;
2959 }
2960
2961 bool
2962 __connman_service_is_split_routing(struct connman_service *service)
2963 {
2964         return service->do_split_routing;
2965 }
2966
2967 bool __connman_service_index_is_split_routing(int index)
2968 {
2969         struct connman_service *service;
2970
2971         if (index < 0)
2972                 return false;
2973
2974         service = __connman_service_lookup_from_index(index);
2975         if (!service)
2976                 return false;
2977
2978         return __connman_service_is_split_routing(service);
2979 }
2980
2981 int __connman_service_get_index(struct connman_service *service)
2982 {
2983         if (!service)
2984                 return -1;
2985
2986         if (service->network)
2987                 return connman_network_get_index(service->network);
2988         else if (service->provider)
2989                 return connman_provider_get_index(service->provider);
2990
2991         return -1;
2992 }
2993
2994 void __connman_service_set_hidden(struct connman_service *service)
2995 {
2996         if (!service || service->hidden)
2997                 return;
2998
2999         service->hidden_service = true;
3000 }
3001
3002 void __connman_service_set_hostname(struct connman_service *service,
3003                                                 const char *hostname)
3004 {
3005         if (!service || service->hidden)
3006                 return;
3007
3008         g_free(service->hostname);
3009         service->hostname = g_strdup(hostname);
3010 }
3011
3012 const char *__connman_service_get_hostname(struct connman_service *service)
3013 {
3014         if (!service)
3015                 return NULL;
3016
3017         return service->hostname;
3018 }
3019
3020 void __connman_service_set_domainname(struct connman_service *service,
3021                                                 const char *domainname)
3022 {
3023         if (!service || service->hidden)
3024                 return;
3025
3026         g_free(service->domainname);
3027         service->domainname = g_strdup(domainname);
3028
3029         domain_changed(service);
3030 }
3031
3032 const char *connman_service_get_domainname(struct connman_service *service)
3033 {
3034         if (!service)
3035                 return NULL;
3036
3037         if (service->domains)
3038                 return service->domains[0];
3039         else
3040                 return service->domainname;
3041 }
3042
3043 char **connman_service_get_nameservers(struct connman_service *service)
3044 {
3045         if (!service)
3046                 return NULL;
3047
3048         if (service->nameservers_config)
3049                 return g_strdupv(service->nameservers_config);
3050         else if (service->nameservers ||
3051                                         service->nameservers_auto) {
3052                 int len = 0, len_auto = 0, i;
3053                 char **nameservers;
3054
3055                 if (service->nameservers)
3056                         len = g_strv_length(service->nameservers);
3057                 if (service->nameservers_auto)
3058                         len_auto = g_strv_length(service->nameservers_auto);
3059
3060                 nameservers = g_try_new0(char *, len + len_auto + 1);
3061                 if (!nameservers)
3062                         return NULL;
3063
3064                 for (i = 0; i < len; i++)
3065                         nameservers[i] = g_strdup(service->nameservers[i]);
3066
3067                 for (i = 0; i < len_auto; i++)
3068                         nameservers[i + len] =
3069                                 g_strdup(service->nameservers_auto[i]);
3070
3071                 return nameservers;
3072         }
3073
3074         return g_strdupv(connman_setting_get_string_list("FallbackNameservers"));
3075 }
3076
3077 char **connman_service_get_timeservers_config(struct connman_service *service)
3078 {
3079         if (!service)
3080                 return NULL;
3081
3082         return service->timeservers_config;
3083 }
3084
3085 char **connman_service_get_timeservers(struct connman_service *service)
3086 {
3087         if (!service)
3088                 return NULL;
3089
3090         return service->timeservers;
3091 }
3092
3093 #if defined TIZEN_EXT
3094 /*
3095  * Description: Telephony plug-in requires manual PROXY setting function
3096  */
3097 int connman_service_set_proxy(struct connman_service *service,
3098                                         const char *proxy, gboolean active)
3099 {
3100         char **proxies_array = NULL;
3101
3102         if (service == NULL)
3103                 return -EINVAL;
3104
3105         switch (service->type) {
3106         case CONNMAN_SERVICE_TYPE_CELLULAR:
3107         case CONNMAN_SERVICE_TYPE_ETHERNET:
3108         case CONNMAN_SERVICE_TYPE_WIFI:
3109                 break;
3110
3111         default:
3112                 return -EINVAL;
3113         }
3114
3115         g_strfreev(service->proxies);
3116         service->proxies = NULL;
3117
3118         if (proxy != NULL)
3119                 proxies_array = g_strsplit(proxy, " ", 0);
3120
3121         service->proxies = proxies_array;
3122
3123         if (proxy == NULL) {
3124                 service->proxy_config = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3125                 DBG("proxy changed (%d)", active);
3126         } else {
3127                 service->proxy_config = CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
3128                 DBG("proxy chagned %s (%d)", proxy, active);
3129         }
3130
3131         if (active == TRUE) {
3132                 proxy_changed(service);
3133
3134                 __connman_notifier_proxy_changed(service);
3135         }
3136
3137         return 0;
3138 }
3139 #endif
3140
3141 void connman_service_set_proxy_method(struct connman_service *service,
3142                                         enum connman_service_proxy_method method)
3143 {
3144         if (!service || service->hidden)
3145                 return;
3146
3147         service->proxy = method;
3148
3149         proxy_changed(service);
3150
3151         if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
3152                 __connman_notifier_proxy_changed(service);
3153 }
3154
3155 enum connman_service_proxy_method connman_service_get_proxy_method(
3156                                         struct connman_service *service)
3157 {
3158         if (!service)
3159                 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
3160
3161         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
3162                 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3163                                 !service->pac)
3164                         return service->proxy;
3165
3166                 return service->proxy_config;
3167         }
3168
3169         return service->proxy;
3170 }
3171
3172 char **connman_service_get_proxy_servers(struct connman_service *service)
3173 {
3174         return g_strdupv(service->proxies);
3175 }
3176
3177 char **connman_service_get_proxy_excludes(struct connman_service *service)
3178 {
3179         return g_strdupv(service->excludes);
3180 }
3181
3182 const char *connman_service_get_proxy_url(struct connman_service *service)
3183 {
3184         if (!service)
3185                 return NULL;
3186
3187         return service->pac;
3188 }
3189
3190 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
3191                                                         const char *url)
3192 {
3193         if (!service || service->hidden)
3194                 return;
3195
3196         service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
3197
3198         if (service->ipconfig_ipv4) {
3199                 if (__connman_ipconfig_set_proxy_autoconfig(
3200                             service->ipconfig_ipv4, url) < 0)
3201                         return;
3202         } else if (service->ipconfig_ipv6) {
3203                 if (__connman_ipconfig_set_proxy_autoconfig(
3204                             service->ipconfig_ipv6, url) < 0)
3205                         return;
3206         } else
3207                 return;
3208
3209         proxy_changed(service);
3210
3211         __connman_notifier_proxy_changed(service);
3212 }
3213
3214 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
3215 {
3216         if (!service)
3217                 return NULL;
3218
3219         if (service->ipconfig_ipv4)
3220                 return __connman_ipconfig_get_proxy_autoconfig(
3221                                                 service->ipconfig_ipv4);
3222         else if (service->ipconfig_ipv6)
3223                 return __connman_ipconfig_get_proxy_autoconfig(
3224                                                 service->ipconfig_ipv6);
3225         return NULL;
3226 }
3227
3228 void __connman_service_set_timeservers(struct connman_service *service,
3229                                 char **timeservers)
3230 {
3231         int i;
3232
3233         if (!service)
3234                 return;
3235
3236         g_strfreev(service->timeservers);
3237         service->timeservers = NULL;
3238
3239         for (i = 0; timeservers && timeservers[i]; i++)
3240                 __connman_service_timeserver_append(service, timeservers[i]);
3241 }
3242
3243 int __connman_service_timeserver_append(struct connman_service *service,
3244                                                 const char *timeserver)
3245 {
3246         int len;
3247
3248         DBG("service %p timeserver %s", service, timeserver);
3249
3250         if (!timeserver)
3251                 return -EINVAL;
3252
3253         if (service->timeservers) {
3254                 int i;
3255
3256                 for (i = 0; service->timeservers[i]; i++)
3257                         if (g_strcmp0(service->timeservers[i], timeserver) == 0)
3258                                 return -EEXIST;
3259
3260                 len = g_strv_length(service->timeservers);
3261                 service->timeservers = g_try_renew(char *, service->timeservers,
3262                                                         len + 2);
3263         } else {
3264                 len = 0;
3265                 service->timeservers = g_try_new0(char *, len + 2);
3266         }
3267
3268         if (!service->timeservers)
3269                 return -ENOMEM;
3270
3271         service->timeservers[len] = g_strdup(timeserver);
3272         service->timeservers[len + 1] = NULL;
3273
3274         return 0;
3275 }
3276
3277 int __connman_service_timeserver_remove(struct connman_service *service,
3278                                                 const char *timeserver)
3279 {
3280         char **servers;
3281         int len, i, j, found = 0;
3282
3283         DBG("service %p timeserver %s", service, timeserver);
3284
3285         if (!timeserver)
3286                 return -EINVAL;
3287
3288         if (!service->timeservers)
3289                 return 0;
3290
3291         for (i = 0; service->timeservers &&
3292                                         service->timeservers[i]; i++)
3293                 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
3294                         found = 1;
3295                         break;
3296                 }
3297
3298         if (found == 0)
3299                 return 0;
3300
3301         len = g_strv_length(service->timeservers);
3302
3303         if (len == 1) {
3304                 g_strfreev(service->timeservers);
3305                 service->timeservers = NULL;
3306
3307                 return 0;
3308         }
3309
3310         servers = g_try_new0(char *, len);
3311         if (!servers)
3312                 return -ENOMEM;
3313
3314         for (i = 0, j = 0; i < len; i++) {
3315                 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
3316                         servers[j] = g_strdup(service->timeservers[i]);
3317                         if (!servers[j])
3318                                 return -ENOMEM;
3319                         j++;
3320                 }
3321         }
3322         servers[len - 1] = NULL;
3323
3324         g_strfreev(service->timeservers);
3325         service->timeservers = servers;
3326
3327         return 0;
3328 }
3329
3330 void __connman_service_timeserver_changed(struct connman_service *service,
3331                 GSList *ts_list)
3332 {
3333         if (!service)
3334                 return;
3335
3336         if (!allow_property_changed(service))
3337                 return;
3338
3339         connman_dbus_property_changed_array(service->path,
3340                         CONNMAN_SERVICE_INTERFACE, "Timeservers",
3341                         DBUS_TYPE_STRING, append_ts, ts_list);
3342 }
3343
3344 void __connman_service_set_pac(struct connman_service *service,
3345                                         const char *pac)
3346 {
3347         if (service->hidden)
3348                 return;
3349         g_free(service->pac);
3350         service->pac = g_strdup(pac);
3351
3352         proxy_changed(service);
3353 }
3354
3355 #if defined TIZEN_EXT
3356 void __connman_service_set_proxy(struct connman_service *service,
3357                                        const char *proxies)
3358 {
3359        char **proxies_array = NULL;
3360
3361        g_strfreev(service->proxies);
3362        service->proxies = NULL;
3363
3364        if (proxies != NULL)
3365                proxies_array = g_strsplit(proxies, " ", 0);
3366
3367        service->proxies = proxies_array;
3368 }
3369 #endif
3370
3371 void __connman_service_set_identity(struct connman_service *service,
3372                                         const char *identity)
3373 {
3374         if (service->immutable || service->hidden)
3375                 return;
3376
3377         g_free(service->identity);
3378         service->identity = g_strdup(identity);
3379
3380         if (service->network)
3381                 connman_network_set_string(service->network,
3382                                         "WiFi.Identity",
3383                                         service->identity);
3384 }
3385
3386 void __connman_service_set_agent_identity(struct connman_service *service,
3387                                                 const char *agent_identity)
3388 {
3389         if (service->hidden)
3390                 return;
3391         g_free(service->agent_identity);
3392         service->agent_identity = g_strdup(agent_identity);
3393
3394         if (service->network)
3395                 connman_network_set_string(service->network,
3396                                         "WiFi.AgentIdentity",
3397                                         service->agent_identity);
3398 }
3399
3400 static int check_passphrase(enum connman_service_security security,
3401                 const char *passphrase)
3402 {
3403         guint i;
3404         gsize length;
3405
3406         if (!passphrase)
3407                 return 0;
3408
3409         length = strlen(passphrase);
3410
3411         switch (security) {
3412         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3413         case CONNMAN_SERVICE_SECURITY_NONE:
3414         case CONNMAN_SERVICE_SECURITY_WPA:
3415 #if !defined TIZEN_EXT
3416         case CONNMAN_SERVICE_SECURITY_RSN:
3417 #endif
3418
3419                 DBG("service security '%s' (%d) not handled",
3420                                 security2string(security), security);
3421
3422                 return -EOPNOTSUPP;
3423
3424         case CONNMAN_SERVICE_SECURITY_PSK:
3425 #if defined TIZEN_EXT
3426         case CONNMAN_SERVICE_SECURITY_RSN:
3427 #endif
3428                 /* A raw key is always 64 bytes length,
3429                  * its content is in hex representation.
3430                  * A PSK key must be between [8..63].
3431                  */
3432                 if (length == 64) {
3433                         for (i = 0; i < 64; i++)
3434                                 if (!isxdigit((unsigned char)
3435                                               passphrase[i]))
3436                                         return -ENOKEY;
3437                 } else if (length < 8 || length > 63)
3438                         return -ENOKEY;
3439                 break;
3440         case CONNMAN_SERVICE_SECURITY_WEP:
3441                 /* length of WEP key is 10 or 26
3442                  * length of WEP passphrase is 5 or 13
3443                  */
3444                 if (length == 10 || length == 26) {
3445                         for (i = 0; i < length; i++)
3446                                 if (!isxdigit((unsigned char)
3447                                               passphrase[i]))
3448                                         return -ENOKEY;
3449                 } else if (length != 5 && length != 13)
3450                         return -ENOKEY;
3451                 break;
3452
3453         case CONNMAN_SERVICE_SECURITY_8021X:
3454                 break;
3455         }
3456
3457         return 0;
3458 }
3459
3460 int __connman_service_set_passphrase(struct connman_service *service,
3461                                         const char *passphrase)
3462 {
3463         int err;
3464
3465         if (service->hidden)
3466                 return -EINVAL;
3467
3468         if (service->immutable &&
3469                         service->security != CONNMAN_SERVICE_SECURITY_8021X)
3470                 return -EINVAL;
3471
3472         err = check_passphrase(service->security, passphrase);
3473
3474         if (err < 0)
3475                 return err;
3476
3477         g_free(service->passphrase);
3478         service->passphrase = g_strdup(passphrase);
3479
3480         if (service->network)
3481                 connman_network_set_string(service->network, "WiFi.Passphrase",
3482                                 service->passphrase);
3483
3484         return 0;
3485 }
3486
3487 const char *__connman_service_get_passphrase(struct connman_service *service)
3488 {
3489         if (!service)
3490                 return NULL;
3491
3492         return service->passphrase;
3493 }
3494
3495 static DBusMessage *get_properties(DBusConnection *conn,
3496                                         DBusMessage *msg, void *user_data)
3497 {
3498         struct connman_service *service = user_data;
3499         DBusMessage *reply;
3500         DBusMessageIter array, dict;
3501
3502         DBG("service %p", service);
3503
3504         reply = dbus_message_new_method_return(msg);
3505         if (!reply)
3506                 return NULL;
3507
3508         dbus_message_iter_init_append(reply, &array);
3509
3510         connman_dbus_dict_open(&array, &dict);
3511         append_properties(&dict, FALSE, service);
3512         connman_dbus_dict_close(&array, &dict);
3513
3514         return reply;
3515 }
3516
3517 static int update_proxy_configuration(struct connman_service *service,
3518                                 DBusMessageIter *array)
3519 {
3520         DBusMessageIter dict;
3521         enum connman_service_proxy_method method;
3522         GString *servers_str = NULL;
3523         GString *excludes_str = NULL;
3524         const char *url = NULL;
3525
3526         method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
3527
3528         dbus_message_iter_recurse(array, &dict);
3529
3530         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
3531                 DBusMessageIter entry, variant;
3532                 const char *key;
3533                 int type;
3534
3535                 dbus_message_iter_recurse(&dict, &entry);
3536
3537                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
3538                         goto error;
3539
3540                 dbus_message_iter_get_basic(&entry, &key);
3541                 dbus_message_iter_next(&entry);
3542
3543                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
3544                         goto error;
3545
3546                 dbus_message_iter_recurse(&entry, &variant);
3547
3548                 type = dbus_message_iter_get_arg_type(&variant);
3549
3550                 if (g_str_equal(key, "Method")) {
3551                         const char *val;
3552
3553                         if (type != DBUS_TYPE_STRING)
3554                                 goto error;
3555
3556                         dbus_message_iter_get_basic(&variant, &val);
3557                         method = string2proxymethod(val);
3558                 } else if (g_str_equal(key, "URL")) {
3559                         if (type != DBUS_TYPE_STRING)
3560                                 goto error;
3561
3562                         dbus_message_iter_get_basic(&variant, &url);
3563                 } else if (g_str_equal(key, "Servers")) {
3564                         DBusMessageIter str_array;
3565
3566                         if (type != DBUS_TYPE_ARRAY)
3567                                 goto error;
3568
3569                         servers_str = g_string_new(NULL);
3570                         if (!servers_str)
3571                                 goto error;
3572
3573                         dbus_message_iter_recurse(&variant, &str_array);
3574
3575                         while (dbus_message_iter_get_arg_type(&str_array) ==
3576                                                         DBUS_TYPE_STRING) {
3577                                 char *val = NULL;
3578
3579                                 dbus_message_iter_get_basic(&str_array, &val);
3580
3581                                 if (servers_str->len > 0)
3582                                         g_string_append_printf(servers_str,
3583                                                         " %s", val);
3584                                 else
3585                                         g_string_append(servers_str, val);
3586
3587                                 dbus_message_iter_next(&str_array);
3588                         }
3589                 } else if (g_str_equal(key, "Excludes")) {
3590                         DBusMessageIter str_array;
3591
3592                         if (type != DBUS_TYPE_ARRAY)
3593                                 goto error;
3594
3595                         excludes_str = g_string_new(NULL);
3596                         if (!excludes_str)
3597                                 goto error;
3598
3599                         dbus_message_iter_recurse(&variant, &str_array);
3600
3601                         while (dbus_message_iter_get_arg_type(&str_array) ==
3602                                                         DBUS_TYPE_STRING) {
3603                                 char *val = NULL;
3604
3605                                 dbus_message_iter_get_basic(&str_array, &val);
3606
3607                                 if (excludes_str->len > 0)
3608                                         g_string_append_printf(excludes_str,
3609                                                         " %s", val);
3610                                 else
3611                                         g_string_append(excludes_str, val);
3612
3613                                 dbus_message_iter_next(&str_array);
3614                         }
3615                 }
3616
3617                 dbus_message_iter_next(&dict);
3618         }
3619
3620         switch (method) {
3621         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
3622                 break;
3623         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
3624                 if (!servers_str && !service->proxies)
3625                         goto error;
3626
3627                 if (servers_str) {
3628                         g_strfreev(service->proxies);
3629
3630                         if (servers_str->len > 0)
3631                                 service->proxies = g_strsplit_set(
3632                                         servers_str->str, " ", 0);
3633                         else
3634                                 service->proxies = NULL;
3635                 }
3636
3637                 if (excludes_str) {
3638                         g_strfreev(service->excludes);
3639
3640                         if (excludes_str->len > 0)
3641                                 service->excludes = g_strsplit_set(
3642                                         excludes_str->str, " ", 0);
3643                         else
3644                                 service->excludes = NULL;
3645                 }
3646
3647                 if (!service->proxies)
3648                         method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3649
3650                 break;
3651         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
3652                 g_free(service->pac);
3653
3654                 if (url && strlen(url) > 0)
3655                         service->pac = g_strdup(url);
3656                 else
3657                         service->pac = NULL;
3658
3659                 /* if we are connected:
3660                    - if service->pac == NULL
3661                    - if __connman_ipconfig_get_proxy_autoconfig(
3662                    service->ipconfig) == NULL
3663                    --> We should start WPAD */
3664
3665                 break;
3666         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
3667                 goto error;
3668         }
3669
3670         if (servers_str)
3671                 g_string_free(servers_str, TRUE);
3672
3673         if (excludes_str)
3674                 g_string_free(excludes_str, TRUE);
3675
3676         service->proxy_config = method;
3677
3678         return 0;
3679
3680 error:
3681         if (servers_str)
3682                 g_string_free(servers_str, TRUE);
3683
3684         if (excludes_str)
3685                 g_string_free(excludes_str, TRUE);
3686
3687         return -EINVAL;
3688 }
3689
3690 int __connman_service_reset_ipconfig(struct connman_service *service,
3691                 enum connman_ipconfig_type type, DBusMessageIter *array,
3692                 enum connman_service_state *new_state)
3693 {
3694         struct connman_ipconfig *ipconfig, *new_ipconfig;
3695         enum connman_ipconfig_method old_method, new_method;
3696         enum connman_service_state state;
3697         int err = 0, index;
3698
3699         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3700                 ipconfig = service->ipconfig_ipv4;
3701                 state = service->state_ipv4;
3702                 new_method = CONNMAN_IPCONFIG_METHOD_DHCP;
3703         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3704                 ipconfig = service->ipconfig_ipv6;
3705                 state = service->state_ipv6;
3706                 new_method = CONNMAN_IPCONFIG_METHOD_AUTO;
3707         } else
3708                 return -EINVAL;
3709
3710         if (!ipconfig)
3711                 return -ENXIO;
3712
3713         old_method = __connman_ipconfig_get_method(ipconfig);
3714         index = __connman_ipconfig_get_index(ipconfig);
3715
3716         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3717                 new_ipconfig = create_ip4config(service, index,
3718                                 CONNMAN_IPCONFIG_METHOD_UNKNOWN);
3719         else
3720                 new_ipconfig = create_ip6config(service, index);
3721
3722         if (array) {
3723                 err = __connman_ipconfig_set_config(new_ipconfig, array);
3724                 if (err < 0) {
3725                         __connman_ipconfig_unref(new_ipconfig);
3726                         return err;
3727                 }
3728
3729                 new_method = __connman_ipconfig_get_method(new_ipconfig);
3730         }
3731
3732         if (is_connecting_state(service, state) ||
3733                                         is_connected_state(service, state))
3734                 __connman_network_clear_ipconfig(service->network, ipconfig);
3735
3736         __connman_ipconfig_unref(ipconfig);
3737
3738         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3739                 service->ipconfig_ipv4 = new_ipconfig;
3740         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3741                 service->ipconfig_ipv6 = new_ipconfig;
3742
3743         if (is_connecting_state(service, state) ||
3744                                         is_connected_state(service, state))
3745                 __connman_ipconfig_enable(new_ipconfig);
3746
3747         if (new_state && new_method != old_method) {
3748                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3749                         *new_state = service->state_ipv4;
3750                 else
3751                         *new_state = service->state_ipv6;
3752
3753                 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
3754         }
3755
3756         DBG("err %d ipconfig %p type %d method %d state %s", err,
3757                 new_ipconfig, type, new_method,
3758                 !new_state  ? "-" : state2string(*new_state));
3759
3760         return err;
3761 }
3762
3763 static DBusMessage *set_property(DBusConnection *conn,
3764                                         DBusMessage *msg, void *user_data)
3765 {
3766         struct connman_service *service = user_data;
3767         DBusMessageIter iter, value;
3768         const char *name;
3769         int type;
3770
3771         DBG("service %p", service);
3772
3773         if (!dbus_message_iter_init(msg, &iter))
3774                 return __connman_error_invalid_arguments(msg);
3775
3776         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
3777                 return __connman_error_invalid_arguments(msg);
3778
3779         if (service->type == CONNMAN_SERVICE_TYPE_WIFI && is_connected(service)) {
3780                 uid_t uid;
3781                 if (connman_dbus_get_connection_unix_user_sync(conn,
3782                                                 dbus_message_get_sender(msg),
3783                                                 &uid) < 0) {
3784                         DBG("Can not get unix user id!");
3785                         return __connman_error_permission_denied(msg);
3786                 }
3787
3788                 if (!connman_service_is_user_allowed(service, uid)) {
3789                         DBG("Not allow this user to operate this wifi service now!");
3790                         return __connman_error_permission_denied(msg);
3791                 }
3792         }
3793
3794         dbus_message_iter_get_basic(&iter, &name);
3795         dbus_message_iter_next(&iter);
3796
3797         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
3798                 return __connman_error_invalid_arguments(msg);
3799
3800         dbus_message_iter_recurse(&iter, &value);
3801
3802         type = dbus_message_iter_get_arg_type(&value);
3803
3804         if (g_str_equal(name, "AutoConnect")) {
3805                 dbus_bool_t autoconnect;
3806
3807                 if (type != DBUS_TYPE_BOOLEAN)
3808                         return __connman_error_invalid_arguments(msg);
3809
3810                 if (!service->favorite)
3811                         return __connman_error_invalid_service(msg);
3812
3813                 dbus_message_iter_get_basic(&value, &autoconnect);
3814
3815                 if (service->autoconnect == autoconnect)
3816                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3817
3818                 service->autoconnect = autoconnect;
3819
3820                 autoconnect_changed(service);
3821
3822                 if (autoconnect)
3823                         __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
3824
3825                 service_save(service);
3826         } else if (g_str_equal(name, "Nameservers.Configuration")) {
3827                 DBusMessageIter entry;
3828                 GString *str;
3829                 int index;
3830                 const char *gw;
3831
3832                 if (__connman_provider_is_immutable(service->provider) ||
3833                                 service->immutable)
3834                         return __connman_error_not_supported(msg);
3835
3836                 if (type != DBUS_TYPE_ARRAY)
3837                         return __connman_error_invalid_arguments(msg);
3838
3839                 str = g_string_new(NULL);
3840                 if (!str)
3841                         return __connman_error_invalid_arguments(msg);
3842
3843                 index = __connman_service_get_index(service);
3844                 gw = __connman_ipconfig_get_gateway_from_index(index,
3845                         CONNMAN_IPCONFIG_TYPE_ALL);
3846
3847                 if (gw && strlen(gw))
3848                         __connman_service_nameserver_del_routes(service,
3849                                                 CONNMAN_IPCONFIG_TYPE_ALL);
3850
3851                 dbus_message_iter_recurse(&value, &entry);
3852
3853                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3854                         const char *val;
3855                         dbus_message_iter_get_basic(&entry, &val);
3856                         dbus_message_iter_next(&entry);
3857 #if defined TIZEN_TV_EXT
3858                         /* First unpack the DNS Config Method */
3859                         if(g_strcmp0(val, "manual") == 0) {
3860                                 service->dns_config_method =
3861                                         CONNMAN_DNSCONFIG_METHOD_MANUAL;
3862                                 continue;
3863                         } else if(g_strcmp0(val, "dhcp") == 0) {
3864                                 service->dns_config_method =
3865                                         CONNMAN_DNSCONFIG_METHOD_DHCP;
3866                                 continue;
3867                         }
3868 #endif
3869                         if (connman_inet_check_ipaddress(val) > 0) {
3870                                 if (str->len > 0)
3871                                         g_string_append_printf(str, " %s", val);
3872                                 else
3873                                         g_string_append(str, val);
3874                         }
3875                 }
3876
3877                 nameserver_remove_all(service);
3878                 g_strfreev(service->nameservers_config);
3879
3880                 if (str->len > 0) {
3881                         service->nameservers_config =
3882                                 g_strsplit_set(str->str, " ", 0);
3883                 } else {
3884                         service->nameservers_config = NULL;
3885                 }
3886
3887                 g_string_free(str, TRUE);
3888
3889                 if (gw && strlen(gw))
3890                         __connman_service_nameserver_add_routes(service, gw);
3891
3892                 nameserver_add_all(service);
3893                 dns_configuration_changed(service);
3894
3895                 if (__connman_service_is_connected_state(service,
3896                                                 CONNMAN_IPCONFIG_TYPE_IPV4))
3897                         __connman_wispr_start(service,
3898                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
3899
3900                 if (__connman_service_is_connected_state(service,
3901                                                 CONNMAN_IPCONFIG_TYPE_IPV6))
3902                         __connman_wispr_start(service,
3903                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
3904
3905                 service_save(service);
3906         } else if (g_str_equal(name, "Timeservers.Configuration")) {
3907                 DBusMessageIter entry;
3908                 GSList *list = NULL;
3909                 int count = 0;
3910
3911                 if (service->immutable)
3912                         return __connman_error_not_supported(msg);
3913
3914                 if (type != DBUS_TYPE_ARRAY)
3915                         return __connman_error_invalid_arguments(msg);
3916
3917                 dbus_message_iter_recurse(&value, &entry);
3918
3919                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3920                         const char *val;
3921                         GSList *new_head;
3922
3923                         dbus_message_iter_get_basic(&entry, &val);
3924
3925                         new_head = __connman_timeserver_add_list(list, val);
3926                         if (list != new_head) {
3927                                 count++;
3928                                 list = new_head;
3929                         }
3930
3931                         dbus_message_iter_next(&entry);
3932                 }
3933
3934                 g_strfreev(service->timeservers_config);
3935                 service->timeservers_config = NULL;
3936
3937                 if (list) {
3938                         service->timeservers_config = g_new0(char *, count+1);
3939
3940                         while (list) {
3941                                 count--;
3942                                 service->timeservers_config[count] = list->data;
3943                                 list = g_slist_delete_link(list, list);
3944                         };
3945                 }
3946
3947                 service_save(service);
3948                 timeservers_configuration_changed(service);
3949
3950                 if (service == __connman_service_get_default())
3951                         __connman_timeserver_sync(service);
3952
3953         } else if (g_str_equal(name, "Domains.Configuration")) {
3954                 DBusMessageIter entry;
3955                 GString *str;
3956
3957                 if (service->immutable)
3958                         return __connman_error_not_supported(msg);
3959
3960                 if (type != DBUS_TYPE_ARRAY)
3961                         return __connman_error_invalid_arguments(msg);
3962
3963                 str = g_string_new(NULL);
3964                 if (!str)
3965                         return __connman_error_invalid_arguments(msg);
3966
3967                 dbus_message_iter_recurse(&value, &entry);
3968
3969                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3970                         const char *val;
3971                         dbus_message_iter_get_basic(&entry, &val);
3972                         dbus_message_iter_next(&entry);
3973                         if (str->len > 0)
3974                                 g_string_append_printf(str, " %s", val);
3975                         else
3976                                 g_string_append(str, val);
3977                 }
3978
3979                 searchdomain_remove_all(service);
3980                 g_strfreev(service->domains);
3981
3982                 if (str->len > 0)
3983                         service->domains = g_strsplit_set(str->str, " ", 0);
3984                 else
3985                         service->domains = NULL;
3986
3987                 g_string_free(str, TRUE);
3988
3989                 searchdomain_add_all(service);
3990                 domain_configuration_changed(service);
3991                 domain_changed(service);
3992
3993                 service_save(service);
3994         } else if (g_str_equal(name, "Proxy.Configuration")) {
3995                 int err;
3996
3997                 if (service->immutable)
3998                         return __connman_error_not_supported(msg);
3999
4000                 if (type != DBUS_TYPE_ARRAY)
4001                         return __connman_error_invalid_arguments(msg);
4002
4003                 err = update_proxy_configuration(service, &value);
4004
4005                 if (err < 0)
4006                         return __connman_error_failed(msg, -err);
4007
4008                 proxy_configuration_changed(service);
4009
4010                 __connman_notifier_proxy_changed(service);
4011
4012                 service_save(service);
4013         } else if (g_str_equal(name, "IPv4.Configuration") ||
4014                         g_str_equal(name, "IPv6.Configuration")) {
4015
4016                 enum connman_service_state state =
4017                                                 CONNMAN_SERVICE_STATE_UNKNOWN;
4018                 enum connman_ipconfig_type type =
4019                         CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4020                 int err = 0;
4021
4022                 if (service->type == CONNMAN_SERVICE_TYPE_VPN ||
4023                                 service->immutable)
4024                         return __connman_error_not_supported(msg);
4025
4026                 DBG("%s", name);
4027
4028                 if (!service->ipconfig_ipv4 &&
4029                                         !service->ipconfig_ipv6)
4030                         return __connman_error_invalid_property(msg);
4031
4032                 if (g_str_equal(name, "IPv4.Configuration"))
4033                         type = CONNMAN_IPCONFIG_TYPE_IPV4;
4034                 else
4035                         type = CONNMAN_IPCONFIG_TYPE_IPV6;
4036
4037                 err = __connman_service_reset_ipconfig(service, type, &value,
4038                                                                 &state);
4039
4040                 if (err < 0) {
4041                         if (is_connected_state(service, state) ||
4042                                         is_connecting_state(service, state)) {
4043                                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4044                                         __connman_network_enable_ipconfig(service->network,
4045                                                         service->ipconfig_ipv4);
4046                                 else
4047                                         __connman_network_enable_ipconfig(service->network,
4048                                                         service->ipconfig_ipv6);
4049                         }
4050
4051                         return __connman_error_failed(msg, -err);
4052                 }
4053
4054                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4055                         ipv4_configuration_changed(service);
4056                 else
4057                         ipv6_configuration_changed(service);
4058
4059                 if (is_connecting(service) || is_connected(service)) {
4060                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4061                                 __connman_network_enable_ipconfig(service->network,
4062                                                                 service->ipconfig_ipv4);
4063                         else
4064                                 __connman_network_enable_ipconfig(service->network,
4065                                                                 service->ipconfig_ipv6);
4066                 }
4067
4068                 service_save(service);
4069         } else
4070                 return __connman_error_invalid_property(msg);
4071
4072         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
4073 }
4074
4075 static void set_error(struct connman_service *service,
4076                                         enum connman_service_error error)
4077 {
4078         const char *str;
4079
4080         if (service->error == error)
4081                 return;
4082
4083         service->error = error;
4084
4085         if (!service->path)
4086                 return;
4087
4088 #if !defined TIZEN_EXT
4089         if (!allow_property_changed(service))
4090                 return;
4091 #endif
4092
4093         str = error2string(service->error);
4094
4095         if (!str)
4096                 str = "";
4097
4098         connman_dbus_property_changed_basic(service->path,
4099                                 CONNMAN_SERVICE_INTERFACE, "Error",
4100                                 DBUS_TYPE_STRING, &str);
4101 }
4102
4103 static void set_idle(struct connman_service *service)
4104 {
4105         service->state = service->state_ipv4 = service->state_ipv6 =
4106                                                 CONNMAN_SERVICE_STATE_IDLE;
4107         set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
4108         state_changed(service);
4109 }
4110
4111 static DBusMessage *clear_property(DBusConnection *conn,
4112                                         DBusMessage *msg, void *user_data)
4113 {
4114         struct connman_service *service = user_data;
4115         const char *name;
4116
4117         DBG("service %p", service);
4118
4119         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
4120                                                         DBUS_TYPE_INVALID);
4121
4122         if (g_str_equal(name, "Error")) {
4123                 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
4124
4125                 g_get_current_time(&service->modified);
4126                 service_save(service);
4127         } else
4128                 return __connman_error_invalid_property(msg);
4129
4130         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
4131 }
4132
4133 static bool is_ipconfig_usable(struct connman_service *service)
4134 {
4135         if (!__connman_ipconfig_is_usable(service->ipconfig_ipv4) &&
4136                         !__connman_ipconfig_is_usable(service->ipconfig_ipv6))
4137                 return false;
4138
4139         return true;
4140 }
4141
4142 static bool is_ignore(struct connman_service *service)
4143 {
4144         if (!service->autoconnect)
4145                 return true;
4146
4147         if (service->roaming)
4148                 return true;
4149
4150         if (service->ignore)
4151                 return true;
4152
4153         if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4154                 return true;
4155
4156         if (!is_ipconfig_usable(service))
4157                 return true;
4158
4159         return false;
4160 }
4161
4162 static void disconnect_on_last_session(enum connman_service_type type)
4163 {
4164         GList *list;
4165
4166         for (list = service_list; list; list = list->next) {
4167                 struct connman_service *service = list->data;
4168
4169                 if (service->type != type)
4170                         continue;
4171
4172                 if (service->connect_reason != CONNMAN_SERVICE_CONNECT_REASON_SESSION)
4173                          continue;
4174
4175                 __connman_service_disconnect(service);
4176                 return;
4177         }
4178 }
4179
4180 static int active_sessions[MAX_CONNMAN_SERVICE_TYPES] = {};
4181 static int active_count = 0;
4182
4183 void __connman_service_set_active_session(bool enable, GSList *list)
4184 {
4185         if (!list)
4186                 return;
4187
4188         if (enable)
4189                 active_count++;
4190         else
4191                 active_count--;
4192
4193         while (list != NULL) {
4194                 enum connman_service_type type = GPOINTER_TO_INT(list->data);
4195
4196                 switch (type) {
4197                 case CONNMAN_SERVICE_TYPE_ETHERNET:
4198                 case CONNMAN_SERVICE_TYPE_WIFI:
4199                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4200                 case CONNMAN_SERVICE_TYPE_CELLULAR:
4201                 case CONNMAN_SERVICE_TYPE_GADGET:
4202                         if (enable)
4203                                 active_sessions[type]++;
4204                         else
4205                                 active_sessions[type]--;
4206                         break;
4207
4208                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4209                 case CONNMAN_SERVICE_TYPE_SYSTEM:
4210                 case CONNMAN_SERVICE_TYPE_GPS:
4211                 case CONNMAN_SERVICE_TYPE_VPN:
4212                 case CONNMAN_SERVICE_TYPE_P2P:
4213                         break;
4214                 }
4215
4216                 if (active_sessions[type] == 0)
4217                         disconnect_on_last_session(type);
4218
4219                 list = g_slist_next(list);
4220         }
4221
4222         DBG("eth %d wifi %d bt %d cellular %d gadget %d sessions %d",
4223                         active_sessions[CONNMAN_SERVICE_TYPE_ETHERNET],
4224                         active_sessions[CONNMAN_SERVICE_TYPE_WIFI],
4225                         active_sessions[CONNMAN_SERVICE_TYPE_BLUETOOTH],
4226                         active_sessions[CONNMAN_SERVICE_TYPE_CELLULAR],
4227                         active_sessions[CONNMAN_SERVICE_TYPE_GADGET],
4228                         active_count);
4229 }
4230
4231 struct preferred_tech_data {
4232         GList *preferred_list;
4233         enum connman_service_type type;
4234 };
4235
4236 static void preferred_tech_add_by_type(gpointer data, gpointer user_data)
4237 {
4238         struct connman_service *service = data;
4239         struct preferred_tech_data *tech_data = user_data;
4240
4241         if (service->type == tech_data->type) {
4242                 tech_data->preferred_list =
4243                         g_list_append(tech_data->preferred_list, service);
4244
4245                 DBG("type %d service %p %s", tech_data->type, service,
4246                                 service->name);
4247         }
4248 }
4249
4250 static GList *preferred_tech_list_get(void)
4251 {
4252         unsigned int *tech_array;
4253         struct preferred_tech_data tech_data = { 0, };
4254         int i;
4255
4256         tech_array = connman_setting_get_uint_list("PreferredTechnologies");
4257         if (!tech_array)
4258                 return NULL;
4259
4260         if (connman_setting_get_bool("SingleConnectedTechnology")) {
4261                 GList *list;
4262                 for (list = service_list; list; list = list->next) {
4263                         struct connman_service *service = list->data;
4264
4265                         if (!is_connected(service))
4266                                 break;
4267
4268                         if (service->connect_reason ==
4269                                         CONNMAN_SERVICE_CONNECT_REASON_USER) {
4270                                 DBG("service %p name %s is user connected",
4271                                                 service, service->name);
4272 #if defined TIZEN_EXT
4273                                 /* We can connect to a favorite service like
4274                                  * wifi even we have a userconnect for cellular
4275                                  * because we have refount for cellular service
4276                                  */
4277                                 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
4278                                         break;
4279
4280                                 if (service->type == CONNMAN_SERVICE_TYPE_BLUETOOTH)
4281                                         break;
4282 #endif
4283                                 return NULL;
4284                         }
4285                 }
4286         }
4287
4288         for (i = 0; tech_array[i] != 0; i += 1) {
4289                 tech_data.type = tech_array[i];
4290                 g_list_foreach(service_list, preferred_tech_add_by_type,
4291                                 &tech_data);
4292         }
4293
4294         return tech_data.preferred_list;
4295 }
4296
4297 static bool auto_connect_service(GList *services,
4298                                 enum connman_service_connect_reason reason,
4299                                 bool preferred)
4300 {
4301         struct connman_service *service = NULL;
4302         bool ignore[MAX_CONNMAN_SERVICE_TYPES] = { };
4303         bool autoconnecting = false;
4304         GList *list;
4305
4306         DBG("preferred %d sessions %d reason %s", preferred, active_count,
4307                 reason2string(reason));
4308
4309         ignore[CONNMAN_SERVICE_TYPE_VPN] = true;
4310
4311         for (list = services; list; list = list->next) {
4312                 service = list->data;
4313
4314                 if (ignore[service->type]) {
4315                         DBG("service %p type %s ignore", service,
4316                                 __connman_service_type2string(service->type));
4317                         continue;
4318                 }
4319
4320 #if defined TIZEN_EXT
4321                 DBG("service %p %s %s %s, favorite(%d), ignore(%d), hidden(%d, %d)",
4322                                 service, service->name,
4323                                 state2string(service->state),
4324                                 __connman_service_type2string(service->type),
4325                                 service->favorite, is_ignore(service),
4326                                 service->hidden, service->hidden_service);
4327
4328                 /* Tizen takes Wi-Fi as the highest priority into consideration. */
4329                 if (service->type != CONNMAN_SERVICE_TYPE_WIFI)
4330                         if (is_connecting(service) == TRUE || is_connected(service) == TRUE)
4331                                 continue;
4332 #endif
4333
4334                 if (service->pending ||
4335                                 is_connecting(service) ||
4336                                 is_connected(service)) {
4337                         if (!active_count)
4338                                 return true;
4339
4340                         ignore[service->type] = true;
4341                         autoconnecting = true;
4342
4343                         DBG("service %p type %s busy", service,
4344                                 __connman_service_type2string(service->type));
4345
4346                         continue;
4347                 }
4348
4349                 if (!service->favorite) {
4350                         if (preferred)
4351                                continue;
4352
4353                         return autoconnecting;
4354                 }
4355
4356                 if (is_ignore(service) || service->state !=
4357                                 CONNMAN_SERVICE_STATE_IDLE)
4358                         continue;
4359
4360                 if (autoconnecting && !active_sessions[service->type]) {
4361                         DBG("service %p type %s has no users", service,
4362                                 __connman_service_type2string(service->type));
4363                         continue;
4364                 }
4365
4366                 if (!is_service_owner_user_login(service)) {
4367                         DBG("favorite user not login, wifi auto connect denied");
4368                         continue;
4369                 }
4370
4371                 DBG("service %p %s %s", service, service->name,
4372                         (preferred) ? "preferred" : reason2string(reason));
4373
4374                 __connman_service_connect(service, reason);
4375
4376                 if (!active_count)
4377                         return true;
4378
4379                 ignore[service->type] = true;
4380         }
4381
4382         return autoconnecting;
4383 }
4384
4385 static gboolean run_auto_connect(gpointer data)
4386 {
4387         enum connman_service_connect_reason reason = GPOINTER_TO_UINT(data);
4388         bool autoconnecting = false;
4389         GList *preferred_tech;
4390
4391         autoconnect_timeout = 0;
4392
4393         DBG("");
4394
4395         preferred_tech = preferred_tech_list_get();
4396         if (preferred_tech) {
4397                 autoconnecting = auto_connect_service(preferred_tech, reason,
4398                                                         true);
4399                 g_list_free(preferred_tech);
4400         }
4401
4402         if (!autoconnecting || active_count)
4403                 auto_connect_service(service_list, reason, false);
4404
4405         return FALSE;
4406 }
4407
4408 void __connman_service_auto_connect(enum connman_service_connect_reason reason)
4409 {
4410         DBG("");
4411
4412         if (autoconnect_timeout != 0)
4413                 return;
4414
4415         if (!__connman_session_policy_autoconnect(reason))
4416                 return;
4417
4418 #if defined TIZEN_EXT
4419         /* Adding Timeout of 500ms before trying to auto connect.
4420          * This is done because of below scenario
4421          * 1. Device is connected to AP1
4422          * 2. WPS Connection request is initiated for AP2
4423          * 3. Immediately WPS Connection is Cancelled
4424          * When WPS Connection Connection is initiated for AP2 then
4425          * sometimes there is a scenario where connman gets in ASSOCIATED
4426          * state with AP1 due to autoconnect and subsequently the connection
4427          * initiated by AP1 fails and connman service for AP1 comes in
4428          * FAILURE state due to this when connection with AP2 is cancelled
4429          * then autoconnect with AP1 doesn't works because its autoconnection
4430          * is ignored as its last state was FAILURE rather than IDLE */
4431         autoconnect_timeout = g_timeout_add(500, run_auto_connect,
4432 #else
4433         autoconnect_timeout = g_timeout_add_seconds(0, run_auto_connect,
4434 #endif
4435                                                 GUINT_TO_POINTER(reason));
4436 }
4437
4438 static gboolean run_vpn_auto_connect(gpointer data) {
4439         GList *list;
4440         bool need_split = false;
4441
4442         vpn_autoconnect_timeout = 0;
4443
4444         for (list = service_list; list; list = list->next) {
4445                 struct connman_service *service = list->data;
4446                 int res;
4447
4448                 if (service->type != CONNMAN_SERVICE_TYPE_VPN)
4449                         continue;
4450
4451                 if (is_connected(service) || is_connecting(service)) {
4452                         if (!service->do_split_routing)
4453                                 need_split = true;
4454                         continue;
4455                 }
4456
4457                 if (is_ignore(service) || !service->favorite)
4458                         continue;
4459
4460                 if (need_split && !service->do_split_routing) {
4461                         DBG("service %p no split routing", service);
4462                         continue;
4463                 }
4464
4465                 DBG("service %p %s %s", service, service->name,
4466                                 service->do_split_routing ?
4467                                 "split routing" : "");
4468
4469                 res = __connman_service_connect(service,
4470                                 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
4471                 if (res < 0 && res != -EINPROGRESS)
4472                         continue;
4473
4474                 if (!service->do_split_routing)
4475                         need_split = true;
4476         }
4477
4478         return FALSE;
4479 }
4480
4481 static void vpn_auto_connect(void)
4482 {
4483         if (vpn_autoconnect_timeout)
4484                 return;
4485
4486         vpn_autoconnect_timeout =
4487                 g_timeout_add_seconds(0, run_vpn_auto_connect, NULL);
4488 }
4489
4490 static void remove_timeout(struct connman_service *service)
4491 {
4492         if (service->timeout > 0) {
4493                 g_source_remove(service->timeout);
4494                 service->timeout = 0;
4495         }
4496 }
4497
4498 static void reply_pending(struct connman_service *service, int error)
4499 {
4500         remove_timeout(service);
4501
4502         if (service->pending) {
4503                 connman_dbus_reply_pending(service->pending, error, NULL);
4504                 service->pending = NULL;
4505         }
4506
4507         if (service->provider_pending) {
4508                 connman_dbus_reply_pending(service->provider_pending,
4509                                                 error, service->path);
4510                 service->provider_pending = NULL;
4511         }
4512 }
4513
4514 bool
4515 __connman_service_is_provider_pending(struct connman_service *service)
4516 {
4517         if (!service)
4518                 return false;
4519
4520         if (service->provider_pending)
4521                 return true;
4522
4523         return false;
4524 }
4525
4526 void __connman_service_set_provider_pending(struct connman_service *service,
4527                                                         DBusMessage *msg)
4528 {
4529         if (service->provider_pending) {
4530                 DBG("service %p provider pending msg %p already exists",
4531                         service, service->provider_pending);
4532                 return;
4533         }
4534
4535         service->provider_pending = msg;
4536         return;
4537 }
4538
4539 static void check_pending_msg(struct connman_service *service)
4540 {
4541         if (!service->pending)
4542                 return;
4543
4544         DBG("service %p pending msg %p already exists", service,
4545                                                 service->pending);
4546         dbus_message_unref(service->pending);
4547 }
4548
4549 void __connman_service_set_hidden_data(struct connman_service *service,
4550                                                         gpointer user_data)
4551 {
4552         DBusMessage *pending = user_data;
4553
4554         DBG("service %p pending %p", service, pending);
4555
4556         if (!pending)
4557                 return;
4558
4559         check_pending_msg(service);
4560
4561         service->pending = pending;
4562 }
4563
4564 void __connman_service_return_error(struct connman_service *service,
4565                                 int error, gpointer user_data)
4566 {
4567         DBG("service %p error %d user_data %p", service, error, user_data);
4568
4569         __connman_service_set_hidden_data(service, user_data);
4570
4571         reply_pending(service, error);
4572 }
4573
4574 static gboolean connect_timeout(gpointer user_data)
4575 {
4576         struct connman_service *service = user_data;
4577         bool autoconnect = false;
4578
4579         DBG("service %p", service);
4580
4581         service->timeout = 0;
4582
4583         if (service->network)
4584                 __connman_network_disconnect(service->network);
4585         else if (service->provider)
4586                 connman_provider_disconnect(service->provider);
4587
4588         __connman_ipconfig_disable(service->ipconfig_ipv4);
4589         __connman_ipconfig_disable(service->ipconfig_ipv6);
4590
4591         __connman_stats_service_unregister(service);
4592
4593         if (service->pending) {
4594                 DBusMessage *reply;
4595
4596                 reply = __connman_error_operation_timeout(service->pending);
4597                 if (reply)
4598                         g_dbus_send_message(connection, reply);
4599
4600                 dbus_message_unref(service->pending);
4601                 service->pending = NULL;
4602         } else
4603                 autoconnect = true;
4604
4605         __connman_service_ipconfig_indicate_state(service,
4606                                         CONNMAN_SERVICE_STATE_FAILURE,
4607                                         CONNMAN_IPCONFIG_TYPE_IPV4);
4608         __connman_service_ipconfig_indicate_state(service,
4609                                         CONNMAN_SERVICE_STATE_FAILURE,
4610                                         CONNMAN_IPCONFIG_TYPE_IPV6);
4611
4612         if (autoconnect &&
4613                         service->connect_reason !=
4614                                 CONNMAN_SERVICE_CONNECT_REASON_USER)
4615                 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
4616
4617         return FALSE;
4618 }
4619
4620 static DBusMessage *connect_service(DBusConnection *conn,
4621                                         DBusMessage *msg, void *user_data)
4622 {
4623         struct connman_service *service = user_data;
4624 #if defined TIZEN_EXT
4625         int err = 0;
4626 #else
4627         int index, err = 0;
4628         GList *list;
4629 #endif
4630
4631         DBG("service %p", service);
4632
4633 #if defined TIZEN_EXT
4634         /*
4635          * Description: TIZEN implements system global connection management.
4636          */
4637         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
4638                 connman_service_user_pdn_connection_ref(service);
4639 #endif
4640
4641         if (service->pending)
4642                 return __connman_error_in_progress(msg);
4643
4644         if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
4645                 uid_t uid;
4646                 if (connman_dbus_get_connection_unix_user_sync(conn,
4647                                                 dbus_message_get_sender(msg),
4648                                                 &uid) < 0) {
4649                         DBG("Can not get unix user id!");
4650                         return __connman_error_permission_denied(msg);
4651                 }
4652
4653                 if (!__connman_service_is_user_allowed(CONNMAN_SERVICE_TYPE_WIFI, uid)) {
4654                         DBG("Not allow this user to connect this wifi service now!");
4655                         return __connman_error_permission_denied(msg);
4656                 }
4657
4658                 if (uid != USER_ROOT && uid != service->user.favorite_user)
4659                         service->request_passphrase_input = true;
4660
4661                 service->user.current_user = uid;
4662
4663                 if (!service->passphrase && uid == service->user.favorite_user) {
4664                         DBG("Now load this favorite user's passphrase.");
4665                         service_load_passphrase(service);
4666                 }
4667         }
4668
4669 #if !defined TIZEN_EXT
4670         index = __connman_service_get_index(service);
4671
4672         for (list = service_list; list; list = list->next) {
4673                 struct connman_service *temp = list->data;
4674
4675 #if defined TIZEN_EXT
4676                 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
4677                         break;
4678 #endif
4679                 if (!is_connecting(temp) && !is_connected(temp))
4680                         break;
4681
4682                 if (service == temp)
4683                         continue;
4684
4685                 if (service->type != temp->type)
4686                         continue;
4687
4688                 if (__connman_service_get_index(temp) == index &&
4689                                 __connman_service_disconnect(temp) == -EINPROGRESS)
4690                         err = -EINPROGRESS;
4691
4692         }
4693         if (err == -EINPROGRESS)
4694                 return __connman_error_operation_timeout(msg);
4695 #endif
4696
4697         service->ignore = false;
4698
4699         service->pending = dbus_message_ref(msg);
4700
4701         err = __connman_service_connect(service,
4702                         CONNMAN_SERVICE_CONNECT_REASON_USER);
4703
4704         if (err == -EINPROGRESS)
4705                 return NULL;
4706
4707         if (service->pending) {
4708                 dbus_message_unref(service->pending);
4709                 service->pending = NULL;
4710         }
4711
4712         if (err < 0)
4713                 return __connman_error_failed(msg, -err);
4714
4715         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
4716 }
4717
4718 static DBusMessage *disconnect_service(DBusConnection *conn,
4719                                         DBusMessage *msg, void *user_data)
4720 {
4721         struct connman_service *service = user_data;
4722         int err;
4723
4724         DBG("service %p", service);
4725
4726 #if defined TIZEN_EXT
4727         /*
4728          * Description: TIZEN implements system global connection management.
4729          */
4730         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
4731                 if (connman_service_user_pdn_connection_unref_and_test(service) != TRUE)
4732                         return __connman_error_failed(msg, EISCONN);
4733
4734                 if (is_connected(service) == TRUE &&
4735                                 service == connman_service_get_default_connection())
4736                         return __connman_error_failed(msg, EISCONN);
4737         }
4738 #endif
4739
4740         if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
4741                 uid_t uid;
4742                 if (connman_dbus_get_connection_unix_user_sync(conn,
4743                                                 dbus_message_get_sender(msg),
4744                                                 &uid) < 0) {
4745                         DBG("Can not get unix user id!");
4746                         return __connman_error_permission_denied(msg);
4747                 }
4748
4749                 if (!connman_service_is_user_allowed(service, uid)) {
4750                         DBG("Not allow this user to disconnect this wifi service now!");
4751                         return __connman_error_permission_denied(msg);
4752                 }
4753         }
4754
4755         service->ignore = true;
4756
4757         err = __connman_service_disconnect(service);
4758         if (err < 0 && err != -EINPROGRESS)
4759                 return __connman_error_failed(msg, -err);
4760
4761         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
4762 }
4763
4764 #if defined TIZEN_EXT
4765 static void __connman_service_cleanup_network_8021x(struct connman_service *service)
4766 {
4767         if (service == NULL)
4768                 return;
4769
4770         DBG("service %p ", service);
4771
4772         connman_network_set_string(service->network, "WiFi.EAP", NULL);
4773         connman_network_set_string(service->network, "WiFi.Identity", NULL);
4774         connman_network_set_string(service->network, "WiFi.CACertFile", NULL);
4775         connman_network_set_string(service->network, "WiFi.ClientCertFile", NULL);
4776         connman_network_set_string(service->network, "WiFi.PrivateKeyFile", NULL);
4777         connman_network_set_string(service->network, "WiFi.PrivateKeyPassphrase", NULL);
4778         connman_network_set_string(service->network, "WiFi.Phase2", NULL);
4779 }
4780 #endif
4781
4782 bool __connman_service_remove(struct connman_service *service)
4783 {
4784         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET ||
4785                         service->type == CONNMAN_SERVICE_TYPE_GADGET)
4786                 return false;
4787
4788         if (service->immutable || service->hidden ||
4789                         __connman_provider_is_immutable(service->provider))
4790                 return false;
4791
4792 #if !defined TIZEN_EXT
4793         if (!service->favorite && service->state !=
4794                                                 CONNMAN_SERVICE_STATE_FAILURE)
4795                 return false;
4796 #endif
4797
4798         __connman_service_disconnect(service);
4799
4800         g_free(service->passphrase);
4801         service->passphrase = NULL;
4802
4803         g_free(service->identity);
4804         service->identity = NULL;
4805
4806         g_free(service->agent_identity);
4807         service->agent_identity = NULL;
4808
4809         g_free(service->eap);
4810         service->eap = NULL;
4811
4812 #if defined TIZEN_EXT
4813         g_free(service->ca_cert_file);
4814         service->ca_cert_file = NULL;
4815
4816         g_free(service->client_cert_file);
4817         service->client_cert_file = NULL;
4818
4819         g_free(service->private_key_file);
4820         service->private_key_file = NULL;
4821
4822         g_free(service->private_key_passphrase);
4823         service->private_key_passphrase = NULL;
4824
4825         g_free(service->phase2);
4826         service->phase2 = NULL;
4827
4828         __connman_service_cleanup_network_8021x(service);
4829
4830         __connman_ipconfig_set_method(service->ipconfig_ipv4, CONNMAN_IPCONFIG_METHOD_DHCP);
4831         __connman_ipconfig_set_method(service->ipconfig_ipv6, CONNMAN_IPCONFIG_METHOD_AUTO);
4832         connman_service_set_proxy(service, NULL, false);
4833
4834         __connman_service_nameserver_clear(service);
4835
4836         g_strfreev(service->nameservers_config);
4837         service->nameservers_config = NULL;
4838
4839 #endif
4840
4841 #if defined TIZEN_EXT
4842         if (service->security != CONNMAN_SERVICE_SECURITY_8021X)
4843 #endif
4844         set_idle(service);
4845
4846         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4847
4848         service->user.favorite_user = USER_NONE;
4849
4850         __connman_service_set_favorite(service, false);
4851
4852         __connman_ipconfig_ipv6_reset_privacy(service->ipconfig_ipv6);
4853
4854 #if defined TIZEN_EXT
4855         __connman_storage_remove_service(service->identifier);
4856 #else
4857         service_save(service);
4858 #endif
4859
4860         return true;
4861 }
4862
4863 static DBusMessage *remove_service(DBusConnection *conn,
4864                                         DBusMessage *msg, void *user_data)
4865 {
4866         struct connman_service *service = user_data;
4867
4868         DBG("service %p", service);
4869
4870         if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
4871                 uid_t uid;
4872                 if (connman_dbus_get_connection_unix_user_sync(conn,
4873                                                 dbus_message_get_sender(msg),
4874                                                 &uid) < 0) {
4875                         DBG("Can not get unix user id!");
4876                         return __connman_error_permission_denied(msg);
4877                 }
4878
4879 #if !defined TIZEN_EXT
4880                 if (!connman_service_is_user_allowed(service, uid)) {
4881                         DBG("Not allow this user to remove this wifi service now!");
4882                         return __connman_error_permission_denied(msg);
4883                 }
4884 #endif
4885         }
4886
4887         if (!__connman_service_remove(service))
4888                 return __connman_error_not_supported(msg);
4889
4890         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
4891 }
4892
4893 static bool check_suitable_state(enum connman_service_state a,
4894                                         enum connman_service_state b)
4895 {
4896         /*
4897          * Special check so that "ready" service can be moved before
4898          * "online" one.
4899          */
4900         if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
4901                         b == CONNMAN_SERVICE_STATE_READY) ||
4902                 (b == CONNMAN_SERVICE_STATE_ONLINE &&
4903                         a == CONNMAN_SERVICE_STATE_READY))
4904                 return true;
4905
4906         return a == b;
4907 }
4908
4909 static void downgrade_state(struct connman_service *service)
4910 {
4911         if (!service)
4912                 return;
4913
4914         DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
4915                                                 service->state_ipv6);
4916
4917         if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
4918                 __connman_service_ipconfig_indicate_state(service,
4919                                                 CONNMAN_SERVICE_STATE_READY,
4920                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4921
4922         if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
4923                 __connman_service_ipconfig_indicate_state(service,
4924                                                 CONNMAN_SERVICE_STATE_READY,
4925                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4926 }
4927
4928 static void apply_relevant_default_downgrade(struct connman_service *service)
4929 {
4930         struct connman_service *def_service;
4931
4932         def_service = __connman_service_get_default();
4933         if (!def_service)
4934                 return;
4935
4936         if (def_service == service &&
4937                         def_service->state == CONNMAN_SERVICE_STATE_ONLINE) {
4938                 def_service->state = CONNMAN_SERVICE_STATE_READY;
4939                 __connman_notifier_leave_online(def_service->type);
4940                 state_changed(def_service);
4941         }
4942 }
4943
4944 static void switch_default_service(struct connman_service *default_service,
4945                 struct connman_service *downgrade_service)
4946 {
4947         struct connman_service *service;
4948         GList *src, *dst;
4949
4950         apply_relevant_default_downgrade(default_service);
4951         src = g_list_find(service_list, downgrade_service);
4952         dst = g_list_find(service_list, default_service);
4953
4954         /* Nothing to do */
4955         if (src == dst || src->next == dst)
4956                 return;
4957
4958         service = src->data;
4959         service_list = g_list_delete_link(service_list, src);
4960         service_list = g_list_insert_before(service_list, dst, service);
4961
4962         downgrade_state(downgrade_service);
4963 }
4964
4965 static DBusMessage *move_service(DBusConnection *conn,
4966                                         DBusMessage *msg, void *user_data,
4967                                                                 bool before)
4968 {
4969         struct connman_service *service = user_data;
4970         struct connman_service *target;
4971         const char *path;
4972         enum connman_ipconfig_method target4, target6;
4973         enum connman_ipconfig_method service4, service6;
4974
4975         DBG("service %p", service);
4976
4977         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
4978                                                         DBUS_TYPE_INVALID);
4979
4980         if (!service->favorite)
4981                 return __connman_error_not_supported(msg);
4982
4983         target = find_service(path);
4984         if (!target || !target->favorite || target == service)
4985                 return __connman_error_invalid_service(msg);
4986
4987         if (target->type == CONNMAN_SERVICE_TYPE_VPN) {
4988                 /*
4989                  * We only allow VPN route splitting if there are
4990                  * routes defined for a given VPN.
4991                  */
4992                 if (!__connman_provider_check_routes(target->provider)) {
4993                         connman_info("Cannot move service. "
4994                                 "No routes defined for provider %s",
4995                                 __connman_provider_get_ident(target->provider));
4996                         return __connman_error_invalid_service(msg);
4997                 }
4998
4999                 target->do_split_routing = true;
5000         } else
5001                 target->do_split_routing = false;
5002
5003         service->do_split_routing = false;
5004
5005         target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
5006         target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
5007         service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
5008         service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
5009
5010         DBG("target %s method %d/%d state %d/%d split %d", target->identifier,
5011                 target4, target6, target->state_ipv4, target->state_ipv6,
5012                 target->do_split_routing);
5013
5014         DBG("service %s method %d/%d state %d/%d", service->identifier,
5015                                 service4, service6,
5016                                 service->state_ipv4, service->state_ipv6);
5017
5018         /*
5019          * If method is OFF, then we do not need to check the corresponding
5020          * ipconfig state.
5021          */
5022         if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
5023                 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
5024                         if (!check_suitable_state(target->state_ipv6,
5025                                                         service->state_ipv6))
5026                                 return __connman_error_invalid_service(msg);
5027                 }
5028         }
5029
5030         if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
5031                 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
5032                         if (!check_suitable_state(target->state_ipv4,
5033                                                         service->state_ipv4))
5034                                 return __connman_error_invalid_service(msg);
5035                 }
5036         }
5037
5038         if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
5039                 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
5040                         if (!check_suitable_state(target->state_ipv6,
5041                                                         service->state_ipv6))
5042                                 return __connman_error_invalid_service(msg);
5043                 }
5044         }
5045
5046         if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
5047                 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
5048                         if (!check_suitable_state(target->state_ipv4,
5049                                                         service->state_ipv4))
5050                                 return __connman_error_invalid_service(msg);
5051                 }
5052         }
5053
5054         g_get_current_time(&service->modified);
5055         service_save(service);
5056         service_save(target);
5057
5058         /*
5059          * If the service which goes down is the default service and is
5060          * online, we downgrade directly its state to ready so:
5061          * the service which goes up, needs to recompute its state which
5062          * is triggered via downgrading it - if relevant - to state ready.
5063          */
5064         if (before)
5065                 switch_default_service(target, service);
5066         else
5067                 switch_default_service(service, target);
5068
5069         __connman_connection_update_gateway();
5070
5071         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
5072 }
5073
5074 static DBusMessage *move_before(DBusConnection *conn,
5075                                         DBusMessage *msg, void *user_data)
5076 {
5077         return move_service(conn, msg, user_data, true);
5078 }
5079
5080 static DBusMessage *move_after(DBusConnection *conn,
5081                                         DBusMessage *msg, void *user_data)
5082 {
5083         return move_service(conn, msg, user_data, false);
5084 }
5085
5086 static DBusMessage *reset_counters(DBusConnection *conn,
5087                                         DBusMessage *msg, void *user_data)
5088 {
5089         struct connman_service *service = user_data;
5090
5091         reset_stats(service);
5092
5093         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
5094 }
5095
5096 static DBusMessage *get_user_favorite(DBusConnection *conn,
5097                                         DBusMessage *msg, void *user_data)
5098 {
5099         DBusMessage *reply;
5100         uid_t uid = USER_NONE;
5101         dbus_bool_t user_favorite = false;
5102         struct connman_service *service = user_data;
5103
5104         connman_dbus_get_connection_unix_user_sync(conn,
5105                                         dbus_message_get_sender(msg),
5106                                         &uid);
5107         if (uid == USER_ROOT)
5108                 user_favorite = service->favorite;
5109         else if (uid != USER_NONE && uid == service->user.favorite_user) {
5110                 DBG("The service is favorite to this user!");
5111                 user_favorite = true;
5112         }
5113
5114         reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
5115         dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN,
5116                                 &user_favorite, DBUS_TYPE_INVALID);
5117         return reply;
5118 }
5119
5120 static struct _services_notify {
5121         int id;
5122         GHashTable *add;
5123         GHashTable *remove;
5124 } *services_notify;
5125
5126 static void service_append_added_foreach(gpointer data, gpointer user_data)
5127 {
5128         struct connman_service *service = data;
5129         DBusMessageIter *iter = user_data;
5130
5131         if (!service || !service->path) {
5132                 DBG("service %p or path is NULL", service);
5133                 return;
5134         }
5135
5136         if (g_hash_table_lookup(services_notify->add, service->path)) {
5137                 DBG("new %s", service->path);
5138
5139                 append_struct(service, iter);
5140                 g_hash_table_remove(services_notify->add, service->path);
5141         } else {
5142                 DBG("changed %s", service->path);
5143
5144                 append_struct_service(iter, NULL, service);
5145         }
5146 }
5147
5148 static void service_append_ordered(DBusMessageIter *iter, void *user_data)
5149 {
5150         g_list_foreach(service_list, service_append_added_foreach, iter);
5151 }
5152
5153 static void append_removed(gpointer key, gpointer value, gpointer user_data)
5154 {
5155         char *objpath = key;
5156         DBusMessageIter *iter = user_data;
5157
5158         DBG("removed %s", objpath);
5159         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath);
5160 }
5161
5162 static void service_append_removed(DBusMessageIter *iter, void *user_data)
5163 {
5164         g_hash_table_foreach(services_notify->remove, append_removed, iter);
5165 }
5166
5167 static gboolean service_send_changed(gpointer data)
5168 {
5169         DBusMessage *signal;
5170
5171         DBG("");
5172
5173         services_notify->id = 0;
5174
5175         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
5176                         CONNMAN_MANAGER_INTERFACE, "ServicesChanged");
5177         if (!signal)
5178                 return FALSE;
5179
5180         __connman_dbus_append_objpath_dict_array(signal,
5181                                         service_append_ordered, NULL);
5182         __connman_dbus_append_objpath_array(signal,
5183                                         service_append_removed, NULL);
5184
5185         dbus_connection_send(connection, signal, NULL);
5186         dbus_message_unref(signal);
5187
5188         g_hash_table_remove_all(services_notify->remove);
5189         g_hash_table_remove_all(services_notify->add);
5190
5191         return FALSE;
5192 }
5193
5194 static void service_schedule_changed(void)
5195 {
5196         if (services_notify->id != 0)
5197                 return;
5198
5199         services_notify->id = g_timeout_add(100, service_send_changed, NULL);
5200 }
5201
5202 static void service_schedule_added(struct connman_service *service)
5203 {
5204         DBG("service %p", service);
5205
5206         g_hash_table_remove(services_notify->remove, service->path);
5207         g_hash_table_replace(services_notify->add, service->path, service);
5208
5209         service_schedule_changed();
5210 }
5211
5212 static void service_schedule_removed(struct connman_service *service)
5213 {
5214         if (!service || !service->path) {
5215                 DBG("service %p or path is NULL", service);
5216                 return;
5217         }
5218
5219         DBG("service %p %s", service, service->path);
5220
5221         g_hash_table_remove(services_notify->add, service->path);
5222         g_hash_table_replace(services_notify->remove, g_strdup(service->path),
5223                         NULL);
5224
5225         service_schedule_changed();
5226 }
5227
5228 static bool allow_property_changed(struct connman_service *service)
5229 {
5230 #if defined TIZEN_EXT
5231         if (service->path == NULL)
5232                 return FALSE;
5233 #endif
5234         if (g_hash_table_lookup_extended(services_notify->add, service->path,
5235                                         NULL, NULL)) {
5236                 DBG("no property updates for service %p", service);
5237                 return false;
5238         }
5239
5240         return true;
5241 }
5242
5243 static const GDBusMethodTable service_methods[] = {
5244         { GDBUS_DEPRECATED_METHOD("GetProperties",
5245                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
5246                         get_properties) },
5247         { GDBUS_METHOD("SetProperty",
5248                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
5249                         NULL, set_property) },
5250         { GDBUS_METHOD("ClearProperty",
5251                         GDBUS_ARGS({ "name", "s" }), NULL,
5252                         clear_property) },
5253         { GDBUS_ASYNC_METHOD("Connect", NULL, NULL,
5254                               connect_service) },
5255         { GDBUS_METHOD("Disconnect", NULL, NULL,
5256                         disconnect_service) },
5257         { GDBUS_METHOD("Remove", NULL, NULL, remove_service) },
5258         { GDBUS_METHOD("MoveBefore",
5259                         GDBUS_ARGS({ "service", "o" }), NULL,
5260                         move_before) },
5261         { GDBUS_METHOD("MoveAfter",
5262                         GDBUS_ARGS({ "service", "o" }), NULL,
5263                         move_after) },
5264         { GDBUS_METHOD("ResetCounters", NULL, NULL, reset_counters) },
5265         { GDBUS_METHOD("GetUserFavorite",
5266                         NULL, GDBUS_ARGS({ "value", "v" }),
5267                         get_user_favorite) },
5268         { },
5269 };
5270
5271 static const GDBusSignalTable service_signals[] = {
5272         { GDBUS_SIGNAL("PropertyChanged",
5273                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
5274         { },
5275 };
5276
5277 static void service_free(gpointer user_data)
5278 {
5279         struct connman_service *service = user_data;
5280         char *path = service->path;
5281
5282         DBG("service %p", service);
5283
5284         reply_pending(service, ENOENT);
5285
5286         __connman_notifier_service_remove(service);
5287         service_schedule_removed(service);
5288
5289         __connman_wispr_stop(service);
5290         stats_stop(service);
5291
5292         service->path = NULL;
5293
5294         if (path) {
5295                 __connman_connection_update_gateway();
5296
5297                 g_dbus_unregister_interface(connection, path,
5298                                                 CONNMAN_SERVICE_INTERFACE);
5299                 g_free(path);
5300         }
5301
5302         g_hash_table_destroy(service->counter_table);
5303
5304         if (service->network) {
5305                 __connman_network_disconnect(service->network);
5306                 connman_network_unref(service->network);
5307                 service->network = NULL;
5308         }
5309
5310         if (service->provider)
5311                 connman_provider_unref(service->provider);
5312
5313         if (service->ipconfig_ipv4) {
5314                 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
5315                 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
5316                 __connman_ipconfig_unref(service->ipconfig_ipv4);
5317                 service->ipconfig_ipv4 = NULL;
5318         }
5319
5320         if (service->ipconfig_ipv6) {
5321                 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
5322                 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
5323                 __connman_ipconfig_unref(service->ipconfig_ipv6);
5324                 service->ipconfig_ipv6 = NULL;
5325         }
5326
5327         g_strfreev(service->timeservers);
5328         g_strfreev(service->timeservers_config);
5329         g_strfreev(service->nameservers);
5330         g_strfreev(service->nameservers_config);
5331         g_strfreev(service->nameservers_auto);
5332         g_strfreev(service->domains);
5333         g_strfreev(service->proxies);
5334         g_strfreev(service->excludes);
5335
5336         g_free(service->hostname);
5337         g_free(service->domainname);
5338         g_free(service->pac);
5339         g_free(service->name);
5340         g_free(service->passphrase);
5341         g_free(service->identifier);
5342         g_free(service->eap);
5343         g_free(service->identity);
5344         g_free(service->agent_identity);
5345         g_free(service->ca_cert_file);
5346         g_free(service->client_cert_file);
5347         g_free(service->private_key_file);
5348         g_free(service->private_key_passphrase);
5349         g_free(service->phase2);
5350         g_free(service->config_file);
5351         g_free(service->config_entry);
5352
5353         if (service->stats.timer)
5354                 g_timer_destroy(service->stats.timer);
5355         if (service->stats_roaming.timer)
5356                 g_timer_destroy(service->stats_roaming.timer);
5357
5358         if (current_default == service)
5359                 current_default = NULL;
5360
5361         g_free(service);
5362 }
5363
5364 static void stats_init(struct connman_service *service)
5365 {
5366         /* home */
5367         service->stats.valid = false;
5368         service->stats.enabled = false;
5369         service->stats.timer = g_timer_new();
5370
5371         /* roaming */
5372         service->stats_roaming.valid = false;
5373         service->stats_roaming.enabled = false;
5374         service->stats_roaming.timer = g_timer_new();
5375 }
5376
5377 static void service_initialize(struct connman_service *service)
5378 {
5379         DBG("service %p", service);
5380
5381         service->refcount = 1;
5382
5383         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
5384
5385         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
5386         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
5387
5388         service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
5389         service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
5390         service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
5391
5392         service->favorite  = false;
5393         service->immutable = false;
5394         service->hidden = false;
5395
5396         service->ignore = false;
5397
5398         service->user.favorite_user = USER_NONE;
5399         service->user.current_user = USER_NONE;
5400
5401         service->request_passphrase_input = false;
5402
5403         service->connect_reason = CONNMAN_SERVICE_CONNECT_REASON_NONE;
5404
5405         service->order = 0;
5406
5407         stats_init(service);
5408
5409         service->provider = NULL;
5410
5411         service->wps = false;
5412 #if defined TIZEN_EXT
5413         service->storage_reload = false;
5414         /*
5415          * Description: TIZEN implements system global connection management.
5416          */
5417         service->user_pdn_connection_refcount = 0;
5418         __sync_synchronize();
5419 #endif
5420 }
5421
5422 /**
5423  * connman_service_create:
5424  *
5425  * Allocate a new service.
5426  *
5427  * Returns: a newly-allocated #connman_service structure
5428  */
5429 struct connman_service *connman_service_create(void)
5430 {
5431         GSList *list;
5432         struct connman_stats_counter *counters;
5433         const char *counter;
5434
5435         struct connman_service *service;
5436
5437         service = g_try_new0(struct connman_service, 1);
5438         if (!service)
5439                 return NULL;
5440
5441         DBG("service %p", service);
5442
5443         service->counter_table = g_hash_table_new_full(g_str_hash,
5444                                                 g_str_equal, NULL, g_free);
5445
5446         for (list = counter_list; list; list = list->next) {
5447                 counter = list->data;
5448
5449                 counters = g_try_new0(struct connman_stats_counter, 1);
5450                 if (!counters) {
5451                         g_hash_table_destroy(service->counter_table);
5452                         g_free(service);
5453                         return NULL;
5454                 }
5455
5456                 counters->append_all = true;
5457
5458                 g_hash_table_replace(service->counter_table, (gpointer)counter,
5459                                 counters);
5460         }
5461
5462         service_initialize(service);
5463
5464         return service;
5465 }
5466
5467 /**
5468  * connman_service_ref:
5469  * @service: service structure
5470  *
5471  * Increase reference counter of service
5472  */
5473 struct connman_service *
5474 connman_service_ref_debug(struct connman_service *service,
5475                         const char *file, int line, const char *caller)
5476 {
5477         DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
5478                 file, line, caller);
5479
5480         __sync_fetch_and_add(&service->refcount, 1);
5481
5482         return service;
5483 }
5484
5485 /**
5486  * connman_service_unref:
5487  * @service: service structure
5488  *
5489  * Decrease reference counter of service and release service if no
5490  * longer needed.
5491  */
5492 void connman_service_unref_debug(struct connman_service *service,
5493                         const char *file, int line, const char *caller)
5494 {
5495         DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
5496                 file, line, caller);
5497
5498         if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
5499                 return;
5500
5501         service_list = g_list_remove(service_list, service);
5502
5503         __connman_service_disconnect(service);
5504
5505         g_hash_table_remove(service_hash, service->identifier);
5506 }
5507
5508 static gint service_compare(gconstpointer a, gconstpointer b)
5509 {
5510         struct connman_service *service_a = (void *) a;
5511         struct connman_service *service_b = (void *) b;
5512         enum connman_service_state state_a, state_b;
5513         bool a_connected, b_connected;
5514         gint strength;
5515
5516         state_a = service_a->state;
5517         state_b = service_b->state;
5518         a_connected = is_connected(service_a);
5519         b_connected = is_connected(service_b);
5520
5521         if (a_connected && b_connected) {
5522                 if (service_a->order > service_b->order)
5523                         return -1;
5524
5525                 if (service_a->order < service_b->order)
5526                         return 1;
5527         }
5528
5529         if (state_a != state_b) {
5530                 if (a_connected && b_connected) {
5531                         /* We prefer online over ready state */
5532                         if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
5533                                 return -1;
5534
5535                         if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
5536                                 return 1;
5537                 }
5538
5539                 if (a_connected)
5540                         return -1;
5541                 if (b_connected)
5542                         return 1;
5543
5544                 if (is_connecting(service_a))
5545                         return -1;
5546                 if (is_connecting(service_b))
5547                         return 1;
5548         }
5549
5550         if (service_a->favorite && !service_b->favorite)
5551                 return -1;
5552
5553         if (!service_a->favorite && service_b->favorite)
5554                 return 1;
5555
5556         if (service_a->type != service_b->type) {
5557
5558                 if (service_a->type == CONNMAN_SERVICE_TYPE_ETHERNET)
5559                         return -1;
5560                 if (service_b->type == CONNMAN_SERVICE_TYPE_ETHERNET)
5561                         return 1;
5562
5563                 if (service_a->type == CONNMAN_SERVICE_TYPE_WIFI)
5564                         return -1;
5565                 if (service_b->type == CONNMAN_SERVICE_TYPE_WIFI)
5566                         return 1;
5567
5568                 if (service_a->type == CONNMAN_SERVICE_TYPE_CELLULAR)
5569                         return -1;
5570                 if (service_b->type == CONNMAN_SERVICE_TYPE_CELLULAR)
5571                         return 1;
5572
5573                 if (service_a->type == CONNMAN_SERVICE_TYPE_BLUETOOTH)
5574                         return -1;
5575                 if (service_b->type == CONNMAN_SERVICE_TYPE_BLUETOOTH)
5576                         return 1;
5577
5578                 if (service_a->type == CONNMAN_SERVICE_TYPE_VPN)
5579                         return -1;
5580                 if (service_b->type == CONNMAN_SERVICE_TYPE_VPN)
5581                         return 1;
5582
5583                 if (service_a->type == CONNMAN_SERVICE_TYPE_GADGET)
5584                         return -1;
5585                 if (service_b->type == CONNMAN_SERVICE_TYPE_GADGET)
5586                         return 1;
5587         }
5588
5589         strength = (gint) service_b->strength - (gint) service_a->strength;
5590         if (strength)
5591                 return strength;
5592
5593         return g_strcmp0(service_a->name, service_b->name);
5594 }
5595
5596 static void service_list_sort(void)
5597 {
5598         if (service_list && service_list->next) {
5599                 service_list = g_list_sort(service_list, service_compare);
5600                 service_schedule_changed();
5601         }
5602 }
5603
5604 /**
5605  * connman_service_get_type:
5606  * @service: service structure
5607  *
5608  * Get the type of service
5609  */
5610 enum connman_service_type connman_service_get_type(struct connman_service *service)
5611 {
5612         if (!service)
5613                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5614
5615         return service->type;
5616 }
5617
5618 /**
5619  * connman_service_get_interface:
5620  * @service: service structure
5621  *
5622  * Get network interface of service
5623  */
5624 char *connman_service_get_interface(struct connman_service *service)
5625 {
5626         int index;
5627
5628         if (!service)
5629                 return NULL;
5630
5631         index = __connman_service_get_index(service);
5632
5633         return connman_inet_ifname(index);
5634 }
5635
5636 /**
5637  * __connman_service_is_user_allowed:
5638  * @type: service type
5639  * @uid: user id
5640  *
5641  * Check a user is allowed to operate a type of service
5642  */
5643 bool __connman_service_is_user_allowed(enum connman_service_type type,
5644                                         uid_t uid)
5645 {
5646         GList *list;
5647         uid_t owner_user = USER_NONE;
5648
5649         for (list = service_list; list; list = list->next) {
5650                 struct connman_service *service = list->data;
5651
5652                 if (service->type != type)
5653                         continue;
5654
5655                 if (is_connected(service)) {
5656                         owner_user = service->user.favorite_user;
5657                         break;
5658                 }
5659         }
5660
5661         if (uid == USER_NONE ||
5662                         (uid != USER_ROOT &&
5663                         owner_user != USER_NONE &&
5664                         owner_user != uid))
5665                 return false;
5666
5667         return true;
5668 }
5669
5670 /**
5671  * connman_service_get_network:
5672  * @service: service structure
5673  *
5674  * Get the service network
5675  */
5676 struct connman_network *
5677 __connman_service_get_network(struct connman_service *service)
5678 {
5679         if (!service)
5680                 return NULL;
5681
5682         return service->network;
5683 }
5684
5685 struct connman_ipconfig *
5686 __connman_service_get_ip4config(struct connman_service *service)
5687 {
5688         if (!service)
5689                 return NULL;
5690
5691         return service->ipconfig_ipv4;
5692 }
5693
5694 struct connman_ipconfig *
5695 __connman_service_get_ip6config(struct connman_service *service)
5696 {
5697         if (!service)
5698                 return NULL;
5699
5700         return service->ipconfig_ipv6;
5701 }
5702
5703 struct connman_ipconfig *
5704 __connman_service_get_ipconfig(struct connman_service *service, int family)
5705 {
5706         if (family == AF_INET)
5707                 return __connman_service_get_ip4config(service);
5708         else if (family == AF_INET6)
5709                 return __connman_service_get_ip6config(service);
5710         else
5711                 return NULL;
5712
5713 }
5714
5715 bool __connman_service_is_connected_state(struct connman_service *service,
5716                                         enum connman_ipconfig_type type)
5717 {
5718         if (!service)
5719                 return false;
5720
5721         switch (type) {
5722         case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
5723                 break;
5724         case CONNMAN_IPCONFIG_TYPE_IPV4:
5725                 return is_connected_state(service, service->state_ipv4);
5726         case CONNMAN_IPCONFIG_TYPE_IPV6:
5727                 return is_connected_state(service, service->state_ipv6);
5728         case CONNMAN_IPCONFIG_TYPE_ALL:
5729                 return is_connected_state(service,
5730                                         CONNMAN_IPCONFIG_TYPE_IPV4) &&
5731                         is_connected_state(service,
5732                                         CONNMAN_IPCONFIG_TYPE_IPV6);
5733         }
5734
5735         return false;
5736 }
5737 enum connman_service_security __connman_service_get_security(
5738                                 struct connman_service *service)
5739 {
5740         if (!service)
5741                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5742
5743         return service->security;
5744 }
5745
5746 const char *__connman_service_get_phase2(struct connman_service *service)
5747 {
5748         if (!service)
5749                 return NULL;
5750
5751         return service->phase2;
5752 }
5753
5754 bool __connman_service_wps_enabled(struct connman_service *service)
5755 {
5756         if (!service)
5757                 return false;
5758
5759         return service->wps;
5760 }
5761
5762 void __connman_service_mark_dirty(void)
5763 {
5764         services_dirty = true;
5765 }
5766
5767 #if defined TIZEN_EXT
5768 /**
5769   * Returns profile count if there is any connected profiles
5770   * that use same interface
5771   */
5772 int __connman_service_get_connected_count_of_iface(
5773                                         struct connman_service *service)
5774 {
5775         GList *list;
5776         int count = 0;
5777         int index1 = 0;
5778         int index2 = 0;
5779
5780         DBG("");
5781
5782         index1 = __connman_service_get_index(service);
5783
5784         if (index1 <= 0)
5785                 return 0;
5786
5787         for (list = service_list; list; list = list->next) {
5788                 struct connman_service *service2 = list->data;
5789
5790                 if (service == service2)
5791                         continue;
5792
5793                 index2 = __connman_service_get_index(service2);
5794
5795                 if (is_connected(service2) && index2 > 0 && index1 == index2)
5796                         count++;
5797
5798                 index2 = 0;
5799         }
5800
5801         DBG("Interface index %d, count %d", index1, count);
5802
5803         return count;
5804 }
5805
5806 void __connman_service_set_storage_reload(struct connman_service *service,
5807                                         bool storage_reload)
5808 {
5809         if (service != NULL)
5810                 service->storage_reload = storage_reload;
5811 }
5812 #endif
5813
5814 /**
5815  * __connman_service_set_favorite_delayed:
5816  * @service: service structure
5817  * @favorite: favorite value
5818  * @delay_ordering: do not order service sequence
5819  *
5820  * Change the favorite setting of service
5821  */
5822 int __connman_service_set_favorite_delayed(struct connman_service *service,
5823                                         bool favorite,
5824                                         bool delay_ordering)
5825 {
5826 #if defined TIZEN_EXT
5827         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
5828                 return -EIO;
5829 #endif
5830         if (service->hidden)
5831                 return -EOPNOTSUPP;
5832
5833         if (service->favorite == favorite)
5834                 return -EALREADY;
5835
5836         service->favorite = favorite;
5837
5838         if (!delay_ordering)
5839                 __connman_service_get_order(service);
5840
5841         favorite_changed(service);
5842
5843         if (!delay_ordering) {
5844
5845                 service_list_sort();
5846
5847                 __connman_connection_update_gateway();
5848         }
5849
5850         return 0;
5851 }
5852
5853 /**
5854  * __connman_service_set_favorite:
5855  * @service: service structure
5856  * @favorite: favorite value
5857  *
5858  * Change the favorite setting of service
5859  */
5860 int __connman_service_set_favorite(struct connman_service *service,
5861                                                 bool favorite)
5862 {
5863         return __connman_service_set_favorite_delayed(service, favorite,
5864                                                         false);
5865 }
5866
5867 bool connman_service_get_favorite(struct connman_service *service)
5868 {
5869         return service->favorite;
5870 }
5871
5872 bool connman_service_get_autoconnect(struct connman_service *service)
5873 {
5874         return service->autoconnect;
5875 }
5876
5877 int __connman_service_set_immutable(struct connman_service *service,
5878                                                 bool immutable)
5879 {
5880         if (service->hidden)
5881                 return -EOPNOTSUPP;
5882
5883         if (service->immutable == immutable)
5884                 return 0;
5885
5886         service->immutable = immutable;
5887
5888         immutable_changed(service);
5889
5890         return 0;
5891 }
5892
5893 int __connman_service_set_ignore(struct connman_service *service,
5894                                                 bool ignore)
5895 {
5896         if (!service)
5897                 return -EINVAL;
5898
5899         service->ignore = ignore;
5900
5901         return 0;
5902 }
5903
5904 void __connman_service_set_string(struct connman_service *service,
5905                                   const char *key, const char *value)
5906 {
5907         if (service->hidden)
5908                 return;
5909         if (g_str_equal(key, "EAP")) {
5910                 g_free(service->eap);
5911                 service->eap = g_strdup(value);
5912         } else if (g_str_equal(key, "Identity")) {
5913                 g_free(service->identity);
5914                 service->identity = g_strdup(value);
5915         } else if (g_str_equal(key, "CACertFile")) {
5916                 g_free(service->ca_cert_file);
5917                 service->ca_cert_file = g_strdup(value);
5918         } else if (g_str_equal(key, "ClientCertFile")) {
5919                 g_free(service->client_cert_file);
5920                 service->client_cert_file = g_strdup(value);
5921         } else if (g_str_equal(key, "PrivateKeyFile")) {
5922                 g_free(service->private_key_file);
5923                 service->private_key_file = g_strdup(value);
5924         } else if (g_str_equal(key, "PrivateKeyPassphrase")) {
5925                 g_free(service->private_key_passphrase);
5926                 service->private_key_passphrase = g_strdup(value);
5927         } else if (g_str_equal(key, "Phase2")) {
5928                 g_free(service->phase2);
5929                 service->phase2 = g_strdup(value);
5930         } else if (g_str_equal(key, "Passphrase"))
5931                 __connman_service_set_passphrase(service, value);
5932 }
5933
5934 void __connman_service_set_search_domains(struct connman_service *service,
5935                                         char **domains)
5936 {
5937         searchdomain_remove_all(service);
5938
5939         if (service->domains)
5940                 g_strfreev(service->domains);
5941
5942         service->domains = g_strdupv(domains);
5943
5944         searchdomain_add_all(service);
5945 }
5946
5947 static void service_complete(struct connman_service *service)
5948 {
5949         reply_pending(service, EIO);
5950
5951         if (service->connect_reason != CONNMAN_SERVICE_CONNECT_REASON_USER)
5952                 __connman_service_auto_connect(service->connect_reason);
5953
5954         g_get_current_time(&service->modified);
5955         service_save(service);
5956 }
5957
5958 static void report_error_cb(void *user_context, bool retry,
5959                                                         void *user_data)
5960 {
5961         struct connman_service *service = user_context;
5962
5963         if (retry)
5964                 __connman_service_connect(service,
5965                                         CONNMAN_SERVICE_CONNECT_REASON_USER);
5966         else {
5967                 /* It is not relevant to stay on Failure state
5968                  * when failing is due to wrong user input */
5969                 __connman_service_clear_error(service);
5970 #if defined TIZEN_EXT
5971                 /* Reseting the state back in case of failure state */
5972                 service->state_ipv4 = service->state_ipv6 =
5973                                 CONNMAN_SERVICE_STATE_IDLE;
5974
5975                 if (service->error != CONNMAN_SERVICE_ERROR_AUTH_FAILED)
5976                         set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
5977 #endif
5978                 service_complete(service);
5979                 __connman_connection_update_gateway();
5980         }
5981 }
5982
5983 static int check_wpspin(struct connman_service *service, const char *wpspin)
5984 {
5985         int length;
5986         guint i;
5987
5988         if (!wpspin)
5989                 return 0;
5990
5991         length = strlen(wpspin);
5992
5993         /* If 0, it will mean user wants to use PBC method */
5994         if (length == 0) {
5995                 connman_network_set_string(service->network,
5996                                                         "WiFi.PinWPS", NULL);
5997                 return 0;
5998         }
5999
6000         /* A WPS PIN is always 8 chars length,
6001          * its content is in digit representation.
6002          */
6003         if (length != 8)
6004                 return -ENOKEY;
6005
6006         for (i = 0; i < 8; i++)
6007                 if (!isdigit((unsigned char) wpspin[i]))
6008                         return -ENOKEY;
6009
6010         connman_network_set_string(service->network, "WiFi.PinWPS", wpspin);
6011
6012         return 0;
6013 }
6014
6015 static void request_input_cb(struct connman_service *service,
6016                         bool values_received,
6017                         const char *name, int name_len,
6018                         const char *identity, const char *passphrase,
6019                         bool wps, const char *wpspin,
6020                         const char *error, void *user_data)
6021 {
6022         struct connman_device *device;
6023         const char *security;
6024         int err = 0;
6025
6026         DBG("RequestInput return, %p", service);
6027
6028         if (error) {
6029                 DBG("error: %s", error);
6030
6031                 if (g_strcmp0(error,
6032                                 "net.connman.Agent.Error.Canceled") == 0) {
6033                         err = -EINVAL;
6034
6035                         if (service->hidden)
6036                                 __connman_service_return_error(service,
6037                                                         ECANCELED, user_data);
6038                         goto done;
6039                 } else {
6040                         if (service->hidden)
6041                                 __connman_service_return_error(service,
6042                                                         ETIMEDOUT, user_data);
6043                 }
6044         }
6045
6046         if (service->hidden && name_len > 0 && name_len <= 32) {
6047                 device = connman_network_get_device(service->network);
6048                 security = connman_network_get_string(service->network,
6049                                                         "WiFi.Security");
6050                 err = __connman_device_request_hidden_scan(device,
6051                                                 name, name_len,
6052                                                 identity, passphrase,
6053                                                 security, user_data);
6054                 if (err < 0)
6055                         __connman_service_return_error(service, -err,
6056                                                         user_data);
6057         }
6058
6059         if (!values_received || service->hidden) {
6060                 err = -EINVAL;
6061                 goto done;
6062         }
6063
6064         if (wps && service->network) {
6065                 err = check_wpspin(service, wpspin);
6066                 if (err < 0)
6067                         goto done;
6068
6069                 connman_network_set_bool(service->network, "WiFi.UseWPS", wps);
6070         }
6071
6072         if (identity)
6073                 __connman_service_set_agent_identity(service, identity);
6074
6075         if (passphrase)
6076                 err = __connman_service_set_passphrase(service, passphrase);
6077
6078  done:
6079         if (err >= 0) {
6080                 /* We forget any previous error. */
6081                 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
6082
6083                 __connman_service_connect(service,
6084                                         CONNMAN_SERVICE_CONNECT_REASON_USER);
6085
6086         } else if (err == -ENOKEY) {
6087                 __connman_service_indicate_error(service,
6088                                         CONNMAN_SERVICE_ERROR_INVALID_KEY);
6089         } else {
6090                 /* It is not relevant to stay on Failure state
6091                  * when failing is due to wrong user input */
6092                 service->state = CONNMAN_SERVICE_STATE_IDLE;
6093
6094                 if (!service->hidden) {
6095                         /*
6096                          * If there was a real error when requesting
6097                          * hidden scan, then that error is returned already
6098                          * to the user somewhere above so do not try to
6099                          * do this again.
6100                          */
6101                         __connman_service_return_error(service, -err,
6102                                                         user_data);
6103                 }
6104
6105                 service_complete(service);
6106                 __connman_connection_update_gateway();
6107         }
6108 }
6109
6110 static void downgrade_connected_services(void)
6111 {
6112         struct connman_service *up_service;
6113         GList *list;
6114
6115         for (list = service_list; list; list = list->next) {
6116                 up_service = list->data;
6117
6118                 if (!is_connected(up_service))
6119                         continue;
6120
6121                 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
6122                         return;
6123
6124                 downgrade_state(up_service);
6125         }
6126 }
6127
6128 static int service_update_preferred_order(struct connman_service *default_service,
6129                 struct connman_service *new_service,
6130                 enum connman_service_state new_state)
6131 {
6132         unsigned int *tech_array;
6133         int i;
6134
6135         if (!default_service || default_service == new_service ||
6136                         default_service->state != new_state)
6137                 return 0;
6138
6139         tech_array = connman_setting_get_uint_list("PreferredTechnologies");
6140         if (tech_array) {
6141
6142                 for (i = 0; tech_array[i] != 0; i += 1) {
6143                         if (default_service->type == tech_array[i])
6144                                 return -EALREADY;
6145
6146                         if (new_service->type == tech_array[i]) {
6147                                 switch_default_service(default_service,
6148                                                 new_service);
6149                                 __connman_connection_update_gateway();
6150                                 return 0;
6151                         }
6152                 }
6153         }
6154
6155         return -EALREADY;
6156 }
6157
6158 #if defined TIZEN_EXT
6159 static gboolean __connman_service_can_drop(struct connman_service *service)
6160 {
6161         if (is_connected(service) == TRUE || is_connecting(service) == TRUE) {
6162                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
6163                         return TRUE;
6164                 else if (connman_service_is_no_ref_user_pdn_connection(service) == TRUE)
6165                         return TRUE;
6166         }
6167         return FALSE;
6168 }
6169
6170 static struct connman_device *default_connecting_device = NULL;
6171
6172 static void __connman_service_disconnect_default(struct connman_service *service)
6173 {
6174         struct connman_device *default_device = NULL;
6175
6176         if (default_connecting_device == NULL)
6177                 return;
6178
6179         default_device = connman_network_get_device(
6180                         __connman_service_get_network(service));
6181
6182         DBG("Disconnecting service %p %s", service, service->path);
6183         DBG("Disconnecting device %p %p %s",
6184                         default_connecting_device,
6185                         default_device,
6186                         connman_device_get_string(default_device, "Name"));
6187
6188         if (default_connecting_device == default_device)
6189                 default_connecting_device = NULL;
6190 }
6191
6192 static void __connman_service_connect_default(struct connman_service *current)
6193 {
6194         int err;
6195         GList *list;
6196         bool default_internet;
6197         struct connman_service *service;
6198         struct connman_service *default_service = NULL;
6199         struct connman_device *default_device = NULL;
6200
6201         if (current->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
6202                 switch (current->state) {
6203                 case CONNMAN_SERVICE_STATE_UNKNOWN:
6204                 case CONNMAN_SERVICE_STATE_ASSOCIATION:
6205                 case CONNMAN_SERVICE_STATE_CONFIGURATION:
6206                         return;
6207                 default:
6208                         break;
6209                 }
6210
6211                 if (default_connecting_device &&
6212                                 __connman_service_is_internet_profile(current) == TRUE) {
6213                         if (current->network == NULL)
6214                                 return;
6215
6216                         default_device = connman_network_get_device(current->network);
6217                         if (default_connecting_device == default_device) {
6218                                 DBG("Cellular service[%s]  %p %s",
6219                                                 state2string(current->state), current, current->path);
6220                                 DBG("Cellular device %p %p %s",
6221                                                 default_connecting_device, default_device,
6222                                                 connman_device_get_string(default_device, "Name"));
6223
6224                                 default_connecting_device = NULL;
6225                         }
6226                 }
6227
6228                 return;
6229         } else if (is_connected(current) == TRUE || is_connecting(current) == TRUE)
6230                 return;
6231
6232         /* Always-on: keep default cellular connection as possible */
6233         for (list = service_list; list; list = list->next) {
6234                 service = list->data;
6235
6236                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR ||
6237                                 __connman_service_is_internet_profile(service) != TRUE ||
6238                                 service->network == NULL) {
6239                         continue;
6240                 }
6241
6242                 default_internet =
6243                                 connman_network_get_bool(service->network, "DefaultInternet");
6244
6245                 DBG("service: %p %s %s %s (default: %d)", service, service->name,
6246                                 __connman_service_type2string(service->type),
6247                                 state2string(service->state), default_internet);
6248
6249                 if (default_internet) {
6250                         default_service = service;
6251                         if (is_connected(default_service) == TRUE ||
6252                                         is_connecting(default_service) == TRUE)
6253                                 return;
6254
6255                         default_device = connman_network_get_device(default_service->network);
6256                         if (default_connecting_device == default_device) {
6257                                 DBG("Device is connecting (%p)", default_connecting_device);
6258                                 return;
6259                         }
6260
6261                         default_connecting_device = default_device;
6262                         default_service->connect_reason = CONNMAN_SERVICE_CONNECT_REASON_USER;
6263
6264                         err = __connman_network_connect(default_service->network);
6265                         DBG("Connecting default service %p %s [%d]",
6266                                         default_service, default_service->path, err);
6267                         DBG("Connecting device %p %s", default_connecting_device,
6268                                         connman_device_get_string(default_connecting_device, "Name"));
6269                         if (err < 0 && err != -EINPROGRESS) {
6270                                 default_connecting_device = NULL;
6271                         } else
6272                                 break;
6273                 }
6274         }
6275 }
6276 #endif
6277
6278 static void single_connected_tech(struct connman_service *allowed)
6279 {
6280         struct connman_service *service;
6281         GSList *services = NULL, *list;
6282         GList *iter;
6283
6284         DBG("keeping %p %s", allowed, allowed->path);
6285
6286 #if defined TIZEN_EXT
6287         if (!allowed || allowed->type == CONNMAN_SERVICE_TYPE_CELLULAR)
6288                 return;
6289 #endif
6290
6291         for (iter = service_list; iter; iter = iter->next) {
6292                 service = iter->data;
6293
6294 #if defined TIZEN_EXT
6295                 if (service != allowed && service->type != allowed->type &&
6296                                 __connman_service_can_drop(service) == TRUE)
6297 #else
6298                 if (!is_connected(service))
6299                         break;
6300
6301                 if (service == allowed)
6302                         continue;
6303 #endif
6304                 services = g_slist_prepend(services, service);
6305         }
6306
6307         for (list = services; list; list = list->next) {
6308                 service = list->data;
6309
6310                 DBG("disconnecting %p %s", service, service->path);
6311 #if defined TIZEN_EXT
6312                 __connman_service_disconnect_default(service);
6313 #endif
6314                 __connman_service_disconnect(service);
6315         }
6316
6317         g_slist_free(services);
6318 }
6319
6320 #if defined TIZEN_EXT
6321 static void set_priority_connected_service(void)
6322 {
6323         struct connman_service *service;
6324         GList *list;
6325
6326         for (list = service_list; list; list = list->next) {
6327                 service = list->data;
6328
6329                 if (is_connected(service) == FALSE)
6330                         service->order = 5;
6331                 else
6332                         service->order = 6;
6333         }
6334 }
6335 #endif
6336
6337 static const char *get_dbus_sender(struct connman_service *service)
6338 {
6339         if (!service->pending)
6340                 return NULL;
6341
6342         return dbus_message_get_sender(service->pending);
6343 }
6344
6345 static int service_indicate_state(struct connman_service *service)
6346 {
6347         enum connman_service_state old_state, new_state;
6348         struct connman_service *def_service;
6349         enum connman_ipconfig_method method;
6350         int result;
6351
6352         if (!service)
6353                 return -EINVAL;
6354
6355         old_state = service->state;
6356         new_state = combine_state(service->state_ipv4, service->state_ipv6);
6357
6358         DBG("service %p old %s - new %s/%s => %s",
6359                                         service,
6360                                         state2string(old_state),
6361                                         state2string(service->state_ipv4),
6362                                         state2string(service->state_ipv6),
6363                                         state2string(new_state));
6364
6365         if (old_state == new_state)
6366                 return -EALREADY;
6367
6368         def_service = __connman_service_get_default();
6369
6370         if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
6371                 result = service_update_preferred_order(def_service,
6372                                 service, new_state);
6373                 if (result == -EALREADY)
6374                         return result;
6375         }
6376
6377         if (old_state == CONNMAN_SERVICE_STATE_ONLINE)
6378                 __connman_notifier_leave_online(service->type);
6379
6380         if (is_connected_state(service, old_state) &&
6381                         !is_connected_state(service, new_state))
6382                 searchdomain_remove_all(service);
6383
6384         service->state = new_state;
6385         state_changed(service);
6386
6387         switch(new_state) {
6388         case CONNMAN_SERVICE_STATE_UNKNOWN:
6389
6390                 break;
6391
6392         case CONNMAN_SERVICE_STATE_IDLE:
6393                 if (old_state != CONNMAN_SERVICE_STATE_DISCONNECT)
6394                         __connman_service_disconnect(service);
6395
6396                 break;
6397
6398         case CONNMAN_SERVICE_STATE_ASSOCIATION:
6399
6400                 break;
6401
6402         case CONNMAN_SERVICE_STATE_CONFIGURATION:
6403                 if (!service->new_service &&
6404                                 __connman_stats_service_register(service) == 0) {
6405                         /*
6406                          * For new services the statistics are updated after
6407                          * we have successfully connected.
6408                          */
6409                         __connman_stats_get(service, false,
6410                                                 &service->stats.data);
6411                         __connman_stats_get(service, true,
6412                                                 &service->stats_roaming.data);
6413                 }
6414
6415                 break;
6416
6417         case CONNMAN_SERVICE_STATE_READY:
6418                 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
6419
6420                 if (service->new_service &&
6421                                 __connman_stats_service_register(service) == 0) {
6422                         /*
6423                          * This is normally done after configuring state
6424                          * but for new service do this after we have connected
6425                          * successfully.
6426                          */
6427                         __connman_stats_get(service, false,
6428                                                 &service->stats.data);
6429                         __connman_stats_get(service, true,
6430                                                 &service->stats_roaming.data);
6431                 }
6432
6433                 service->new_service = false;
6434
6435                 default_changed();
6436
6437                 def_service = __connman_service_get_default();
6438
6439                 service_update_preferred_order(def_service, service, new_state);
6440
6441                 __connman_service_set_favorite(service, true);
6442
6443                 reply_pending(service, 0);
6444
6445                 g_get_current_time(&service->modified);
6446                 service_save(service);
6447
6448                 searchdomain_add_all(service);
6449                 dns_changed(service);
6450                 domain_changed(service);
6451                 proxy_changed(service);
6452
6453                 if (old_state != CONNMAN_SERVICE_STATE_ONLINE)
6454                         __connman_notifier_connect(service->type);
6455
6456                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
6457                         connman_network_get_bool(service->network,
6458                                                 "WiFi.UseWPS")) {
6459                         const char *pass;
6460
6461                         pass = connman_network_get_string(service->network,
6462                                                         "WiFi.Passphrase");
6463
6464                         __connman_service_set_passphrase(service, pass);
6465
6466                         connman_network_set_bool(service->network,
6467                                                         "WiFi.UseWPS", false);
6468                 }
6469
6470                 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
6471                 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
6472                         __connman_ipconfig_disable_ipv6(
6473                                                 service->ipconfig_ipv6);
6474
6475                 if (connman_setting_get_bool("SingleConnectedTechnology"))
6476                         single_connected_tech(service);
6477                 else if (service->type != CONNMAN_SERVICE_TYPE_VPN)
6478                         vpn_auto_connect();
6479
6480 #if defined TIZEN_EXT
6481                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
6482                         set_priority_connected_service();
6483 #endif
6484
6485                 break;
6486
6487         case CONNMAN_SERVICE_STATE_ONLINE:
6488
6489                 break;
6490
6491         case CONNMAN_SERVICE_STATE_DISCONNECT:
6492                 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
6493
6494                 reply_pending(service, ECONNABORTED);
6495
6496                 def_service = __connman_service_get_default();
6497                 service->disconnect_reason = connman_network_get_disconnect_reason(service->network);
6498
6499                 if (!__connman_notifier_is_connected() &&
6500                         def_service &&
6501                                 def_service->provider)
6502                         connman_provider_disconnect(def_service->provider);
6503
6504                 default_changed();
6505
6506                 __connman_wispr_stop(service);
6507
6508                 __connman_wpad_stop(service);
6509
6510 #if defined TIZEN_EXT
6511                 /**
6512                   * Skip the functions if there is any connected profiles
6513                   * that use same interface
6514                   */
6515                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR ||
6516                         __connman_service_get_connected_count_of_iface(
6517                                                         service) <= 0) {
6518 #endif
6519                 dns_changed(service);
6520                 domain_changed(service);
6521                 proxy_changed(service);
6522 #if defined TIZEN_EXT
6523                 }
6524 #endif
6525
6526                 /*
6527                  * Previous services which are connected and which states
6528                  * are set to online should reset relevantly ipconfig_state
6529                  * to ready so wispr/portal will be rerun on those
6530                  */
6531                 downgrade_connected_services();
6532
6533                 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
6534                 break;
6535
6536         case CONNMAN_SERVICE_STATE_FAILURE:
6537 #if defined TIZEN_EXT
6538                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
6539                         service->order = 5;
6540                 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
6541 #endif
6542                 if (service->connect_reason == CONNMAN_SERVICE_CONNECT_REASON_USER &&
6543                         connman_agent_report_error(service, service->path,
6544                                         error2string(service->error),
6545                                         report_error_cb,
6546                                         get_dbus_sender(service),
6547                                         NULL) == -EINPROGRESS)
6548                         return 0;
6549                 service_complete(service);
6550
6551                 break;
6552         }
6553
6554         service_list_sort();
6555
6556 #if defined TIZEN_EXT
6557         __connman_service_connect_default(service);
6558 #endif
6559
6560         __connman_connection_update_gateway();
6561
6562         if ((old_state == CONNMAN_SERVICE_STATE_ONLINE &&
6563                         new_state != CONNMAN_SERVICE_STATE_READY) ||
6564                 (old_state == CONNMAN_SERVICE_STATE_READY &&
6565                         new_state != CONNMAN_SERVICE_STATE_ONLINE)) {
6566                 __connman_notifier_disconnect(service->type);
6567         }
6568
6569         if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
6570                 __connman_notifier_enter_online(service->type);
6571                 default_changed();
6572         }
6573
6574         if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
6575                 service->connect_reason == CONNMAN_SERVICE_CONNECT_REASON_USER &&
6576                 (new_state == CONNMAN_SERVICE_STATE_READY ||
6577                 new_state == CONNMAN_SERVICE_STATE_ONLINE)) {
6578                 if (service->user.favorite_user != service->user.current_user) {
6579                         DBG("Now set service favorite user id from %d to %d",
6580                         service->user.favorite_user, service->user.current_user);
6581
6582                         service->user.favorite_user = service->user.current_user;
6583
6584                         service_save(service);
6585                 }
6586         }
6587
6588         return 0;
6589 }
6590
6591 int __connman_service_indicate_error(struct connman_service *service,
6592                                         enum connman_service_error error)
6593 {
6594         DBG("service %p error %d", service, error);
6595
6596         if (!service)
6597                 return -EINVAL;
6598
6599         if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
6600                 return -EALREADY;
6601
6602         set_error(service, error);
6603
6604 /* default internet service: fix not cleared if pdp activation*/
6605 #if defined TIZEN_EXT
6606                 /*
6607                  * If connection failed for default service(DefaultInternet),
6608                  * default_connecting_device should be cleared.
6609                  */
6610                 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
6611                                 service->error == CONNMAN_SERVICE_ERROR_CONNECT_FAILED)
6612                         __connman_service_disconnect_default(service);
6613
6614                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
6615                                 service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY) {
6616                         g_free(service->passphrase);
6617                         service->passphrase = NULL;
6618                 }
6619 #endif
6620
6621         __connman_service_ipconfig_indicate_state(service,
6622                                                 CONNMAN_SERVICE_STATE_FAILURE,
6623                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
6624         __connman_service_ipconfig_indicate_state(service,
6625                                                 CONNMAN_SERVICE_STATE_FAILURE,
6626                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
6627         return 0;
6628 }
6629
6630 int __connman_service_clear_error(struct connman_service *service)
6631 {
6632         DBusMessage *pending, *provider_pending;
6633
6634         DBG("service %p", service);
6635
6636         if (!service)
6637                 return -EINVAL;
6638
6639         if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
6640                 return -EINVAL;
6641
6642         pending = service->pending;
6643         service->pending = NULL;
6644         provider_pending = service->provider_pending;
6645         service->provider_pending = NULL;
6646
6647         __connman_service_ipconfig_indicate_state(service,
6648                                                 CONNMAN_SERVICE_STATE_IDLE,
6649                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
6650
6651         __connman_service_ipconfig_indicate_state(service,
6652                                                 CONNMAN_SERVICE_STATE_IDLE,
6653                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
6654
6655         service->pending = pending;
6656         service->provider_pending = provider_pending;
6657
6658         return 0;
6659 }
6660
6661 int __connman_service_indicate_default(struct connman_service *service)
6662 {
6663         DBG("service %p state %s", service, state2string(service->state));
6664
6665         if (!is_connected(service)) {
6666                 /*
6667                  * If service is not yet fully connected, then we must not
6668                  * change the default yet. The default gw will be changed
6669                  * after the service state is in ready.
6670                  */
6671                 return -EINPROGRESS;
6672         }
6673
6674         default_changed();
6675
6676         return 0;
6677 }
6678
6679 enum connman_service_state __connman_service_ipconfig_get_state(
6680                                         struct connman_service *service,
6681                                         enum connman_ipconfig_type type)
6682 {
6683         if (!service)
6684                 return CONNMAN_SERVICE_STATE_UNKNOWN;
6685
6686         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
6687                 return service->state_ipv4;
6688
6689         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
6690                 return service->state_ipv6;
6691
6692         return CONNMAN_SERVICE_STATE_UNKNOWN;
6693 }
6694
6695 static void check_proxy_setup(struct connman_service *service)
6696 {
6697         /*
6698          * We start WPAD if we haven't got a PAC URL from DHCP and
6699          * if our proxy manual configuration is either empty or set
6700          * to AUTO with an empty URL.
6701          */
6702
6703         if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
6704                 goto done;
6705
6706         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
6707                 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
6708                         service->pac))
6709                 goto done;
6710
6711         if (__connman_wpad_start(service) < 0) {
6712                 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
6713                 __connman_notifier_proxy_changed(service);
6714                 goto done;
6715         }
6716
6717         return;
6718
6719 done:
6720         __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
6721 }
6722
6723 #if defined TIZEN_EXT
6724 void connman_check_proxy_setup_and_wispr_start(struct connman_service *service){
6725
6726         DBG("check the proxy and start wispr");
6727         check_proxy_setup(service);
6728         return;
6729 }
6730 #endif
6731
6732 /*
6733  * How many networks are connected at the same time. If more than 1,
6734  * then set the rp_filter setting properly (loose mode routing) so that network
6735  * connectivity works ok. This is only done for IPv4 networks as IPv6
6736  * does not have rp_filter knob.
6737  */
6738 static int connected_networks_count;
6739 static int original_rp_filter;
6740
6741 static void service_rp_filter(struct connman_service *service,
6742                                 bool connected)
6743 {
6744         enum connman_ipconfig_method method;
6745
6746         method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
6747
6748         switch (method) {
6749         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
6750         case CONNMAN_IPCONFIG_METHOD_OFF:
6751         case CONNMAN_IPCONFIG_METHOD_AUTO:
6752                 return;
6753         case CONNMAN_IPCONFIG_METHOD_FIXED:
6754         case CONNMAN_IPCONFIG_METHOD_MANUAL:
6755         case CONNMAN_IPCONFIG_METHOD_DHCP:
6756                 break;
6757         }
6758
6759         if (connected) {
6760                 if (connected_networks_count == 1) {
6761                         int filter_value;
6762                         filter_value = __connman_ipconfig_set_rp_filter();
6763                         if (filter_value < 0)
6764                                 return;
6765
6766                         original_rp_filter = filter_value;
6767                 }
6768                 connected_networks_count++;
6769
6770         } else {
6771                 if (connected_networks_count == 2)
6772                         __connman_ipconfig_unset_rp_filter(original_rp_filter);
6773
6774                 connected_networks_count--;
6775                 if (connected_networks_count < 0)
6776                         connected_networks_count = 0;
6777         }
6778
6779         DBG("%s %s ipconfig %p method %d count %d filter %d",
6780                 connected ? "connected" : "disconnected", service->identifier,
6781                 service->ipconfig_ipv4, method,
6782                 connected_networks_count, original_rp_filter);
6783 }
6784
6785 static gboolean redo_wispr(gpointer user_data)
6786 {
6787         struct connman_service *service = user_data;
6788         int refcount = service->refcount - 1;
6789
6790         connman_service_unref(service);
6791         if (refcount == 0) {
6792                 DBG("Service %p already removed", service);
6793                 return FALSE;
6794         }
6795
6796         __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
6797
6798         return FALSE;
6799 }
6800
6801 int __connman_service_online_check_failed(struct connman_service *service,
6802                                         enum connman_ipconfig_type type)
6803 {
6804         DBG("service %p type %d count %d", service, type,
6805                                                 service->online_check_count);
6806
6807         /* currently we only retry IPv6 stuff */
6808         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 ||
6809                         service->online_check_count != 1) {
6810                 connman_warn("Online check failed for %p %s", service,
6811                         service->name);
6812                 return 0;
6813         }
6814
6815         service->online_check_count = 0;
6816
6817         /*
6818          * We set the timeout to 1 sec so that we have a chance to get
6819          * necessary IPv6 router advertisement messages that might have
6820          * DNS data etc.
6821          */
6822         g_timeout_add_seconds(1, redo_wispr, connman_service_ref(service));
6823
6824         return EAGAIN;
6825 }
6826
6827 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
6828                                         enum connman_service_state new_state,
6829                                         enum connman_ipconfig_type type)
6830 {
6831         struct connman_ipconfig *ipconfig = NULL;
6832         enum connman_service_state old_state;
6833         enum connman_ipconfig_method method;
6834
6835         if (!service)
6836                 return -EINVAL;
6837
6838         switch (type) {
6839         case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
6840         case CONNMAN_IPCONFIG_TYPE_ALL:
6841                 return -EINVAL;
6842
6843         case CONNMAN_IPCONFIG_TYPE_IPV4:
6844                 old_state = service->state_ipv4;
6845                 ipconfig = service->ipconfig_ipv4;
6846
6847                 break;
6848
6849         case CONNMAN_IPCONFIG_TYPE_IPV6:
6850                 old_state = service->state_ipv6;
6851                 ipconfig = service->ipconfig_ipv6;
6852
6853                 break;
6854         }
6855
6856         if (!ipconfig)
6857                 return -EINVAL;
6858
6859         /* Any change? */
6860         if (old_state == new_state)
6861                 return -EALREADY;
6862
6863 #if defined TIZEN_EXT
6864         __sync_synchronize();
6865         if (service->user_pdn_connection_refcount > 0 &&
6866                         service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
6867                 if (new_state == CONNMAN_SERVICE_STATE_FAILURE ||
6868                                 new_state == CONNMAN_SERVICE_STATE_DISCONNECT ||
6869                                 new_state == CONNMAN_SERVICE_STATE_IDLE) {
6870                         service->user_pdn_connection_refcount = 0;
6871                         __sync_synchronize();
6872                 }
6873 #endif
6874
6875         DBG("service %p (%s) old state %d (%s) new state %d (%s) type %d (%s)",
6876                 service, service ? service->identifier : NULL,
6877                 old_state, state2string(old_state),
6878                 new_state, state2string(new_state),
6879                 type, __connman_ipconfig_type2string(type));
6880
6881         switch (new_state) {
6882         case CONNMAN_SERVICE_STATE_UNKNOWN:
6883         case CONNMAN_SERVICE_STATE_IDLE:
6884         case CONNMAN_SERVICE_STATE_ASSOCIATION:
6885                 break;
6886         case CONNMAN_SERVICE_STATE_CONFIGURATION:
6887                 __connman_ipconfig_enable(ipconfig);
6888                 break;
6889         case CONNMAN_SERVICE_STATE_READY:
6890 #if defined TIZEN_EXT
6891                 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
6892                                 __connman_service_is_internet_profile(service) != TRUE) {
6893                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
6894                                 service_rp_filter(service, TRUE);
6895
6896                         break;
6897                 }
6898 #endif
6899                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
6900 #if !defined TIZEN_EXT
6901                         check_proxy_setup(service);
6902 #endif
6903                         service_rp_filter(service, true);
6904                 } else {
6905                         service->online_check_count = 1;
6906                         __connman_wispr_start(service, type);
6907                 }
6908                 break;
6909         case CONNMAN_SERVICE_STATE_ONLINE:
6910                 break;
6911         case CONNMAN_SERVICE_STATE_DISCONNECT:
6912                 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
6913                         return -EINVAL;
6914
6915                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
6916                         service_rp_filter(service, false);
6917
6918                 break;
6919         case CONNMAN_SERVICE_STATE_FAILURE:
6920                 break;
6921         }
6922
6923         /* Keep that state, but if the ipconfig method is OFF, then we set
6924            the state to IDLE so that it will not affect the combined state
6925            in the future.
6926          */
6927         method = __connman_ipconfig_get_method(ipconfig);
6928         switch (method) {
6929         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
6930         case CONNMAN_IPCONFIG_METHOD_OFF:
6931                 new_state = CONNMAN_SERVICE_STATE_IDLE;
6932                 break;
6933
6934         case CONNMAN_IPCONFIG_METHOD_FIXED:
6935         case CONNMAN_IPCONFIG_METHOD_MANUAL:
6936         case CONNMAN_IPCONFIG_METHOD_DHCP:
6937         case CONNMAN_IPCONFIG_METHOD_AUTO:
6938                 break;
6939
6940         }
6941
6942         if (is_connected_state(service, old_state) &&
6943                         !is_connected_state(service, new_state))
6944                 nameserver_remove_all(service);
6945
6946         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
6947                 service->state_ipv4 = new_state;
6948         else
6949                 service->state_ipv6 = new_state;
6950
6951         if (!is_connected_state(service, old_state) &&
6952                         is_connected_state(service, new_state))
6953                 nameserver_add_all(service);
6954
6955 #if defined TIZEN_EXT
6956         int ret = service_indicate_state(service);
6957         /*Sent the Ready changed signal again in case IPv4 IP set
6958           after IPv6 IP set*/
6959
6960         if(ret == -EALREADY && type == CONNMAN_IPCONFIG_TYPE_IPV4
6961                         && new_state == CONNMAN_SERVICE_STATE_READY) {
6962                 DBG("Notify IPv4 state new/old %d/%d", new_state,old_state);
6963                 state_changed(service);
6964         }
6965
6966         return ret;
6967 #endif
6968         return service_indicate_state(service);
6969 }
6970
6971 static bool prepare_network(struct connman_service *service)
6972 {
6973         enum connman_network_type type;
6974         unsigned int ssid_len;
6975
6976         type = connman_network_get_type(service->network);
6977
6978         switch (type) {
6979         case CONNMAN_NETWORK_TYPE_UNKNOWN:
6980         case CONNMAN_NETWORK_TYPE_VENDOR:
6981                 return false;
6982         case CONNMAN_NETWORK_TYPE_WIFI:
6983                 if (!connman_network_get_blob(service->network, "WiFi.SSID",
6984                                                 &ssid_len))
6985                         return false;
6986
6987                 if (service->passphrase)
6988                         connman_network_set_string(service->network,
6989                                 "WiFi.Passphrase", service->passphrase);
6990                 break;
6991         case CONNMAN_NETWORK_TYPE_ETHERNET:
6992         case CONNMAN_NETWORK_TYPE_GADGET:
6993         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
6994         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
6995         case CONNMAN_NETWORK_TYPE_CELLULAR:
6996                 break;
6997         }
6998
6999         return true;
7000 }
7001
7002 static void prepare_8021x(struct connman_service *service)
7003 {
7004         if (service->eap)
7005                 connman_network_set_string(service->network, "WiFi.EAP",
7006                                                                 service->eap);
7007
7008         if (service->identity)
7009                 connman_network_set_string(service->network, "WiFi.Identity",
7010                                                         service->identity);
7011
7012         if (service->ca_cert_file)
7013                 connman_network_set_string(service->network, "WiFi.CACertFile",
7014                                                         service->ca_cert_file);
7015
7016         if (service->client_cert_file)
7017                 connman_network_set_string(service->network,
7018                                                 "WiFi.ClientCertFile",
7019                                                 service->client_cert_file);
7020
7021         if (service->private_key_file)
7022                 connman_network_set_string(service->network,
7023                                                 "WiFi.PrivateKeyFile",
7024                                                 service->private_key_file);
7025
7026         if (service->private_key_passphrase)
7027                 connman_network_set_string(service->network,
7028                                         "WiFi.PrivateKeyPassphrase",
7029                                         service->private_key_passphrase);
7030
7031         if (service->phase2)
7032                 connman_network_set_string(service->network, "WiFi.Phase2",
7033                                                         service->phase2);
7034 }
7035
7036 static int service_connect(struct connman_service *service)
7037 {
7038         int err;
7039
7040         if (service->hidden)
7041                 return -EPERM;
7042
7043 #if defined TIZEN_EXT
7044         GList *list;
7045         int index;
7046
7047         index = __connman_service_get_index(service);
7048
7049         for (list = service_list; list; list = list->next) {
7050                 struct connman_service *temp = list->data;
7051
7052                 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
7053                         break;
7054
7055                 if (!is_connecting(temp) && !is_connected(temp))
7056                         break;
7057
7058                 if (service == temp)
7059                         continue;
7060
7061                 if (service->type != temp->type)
7062                         continue;
7063
7064                 if (__connman_service_get_index(temp) == index &&
7065                                 __connman_service_disconnect(temp) == -EINPROGRESS)
7066                         return -EINPROGRESS;
7067         }
7068 #endif
7069
7070         switch (service->type) {
7071         case CONNMAN_SERVICE_TYPE_UNKNOWN:
7072         case CONNMAN_SERVICE_TYPE_SYSTEM:
7073         case CONNMAN_SERVICE_TYPE_GPS:
7074         case CONNMAN_SERVICE_TYPE_P2P:
7075                 return -EINVAL;
7076         case CONNMAN_SERVICE_TYPE_ETHERNET:
7077         case CONNMAN_SERVICE_TYPE_GADGET:
7078         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
7079         case CONNMAN_SERVICE_TYPE_CELLULAR:
7080         case CONNMAN_SERVICE_TYPE_VPN:
7081                 break;
7082         case CONNMAN_SERVICE_TYPE_WIFI:
7083                 switch (service->security) {
7084                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
7085                 case CONNMAN_SERVICE_SECURITY_NONE:
7086                         break;
7087                 case CONNMAN_SERVICE_SECURITY_WEP:
7088                 case CONNMAN_SERVICE_SECURITY_PSK:
7089                 case CONNMAN_SERVICE_SECURITY_WPA:
7090                 case CONNMAN_SERVICE_SECURITY_RSN:
7091                         if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
7092                                 return -ENOKEY;
7093
7094                         if (service->request_passphrase_input) {
7095                                 DBG("Now try to connect other user's favorite service");
7096                                 service->request_passphrase_input = false;
7097                                 return -ENOKEY;
7098                         } else if (!service->passphrase) {
7099                                 if (!service->network)
7100                                         return -EOPNOTSUPP;
7101
7102                                 if (!service->wps ||
7103                                         !connman_network_get_bool(service->network, "WiFi.UseWPS"))
7104                                         return -ENOKEY;
7105                         }
7106                         break;
7107
7108                 case CONNMAN_SERVICE_SECURITY_8021X:
7109                         if (!service->eap)
7110                                 return -EINVAL;
7111
7112 #if defined TIZEN_EXT
7113                         /*
7114                          * never request credentials if using EAP-TLS, EAP-SIM
7115                          * or EAP-AKA (EAP-TLS, EAP-SIM and EAP-AKA networks
7116                          * need to be fully provisioned)
7117                          */
7118                         if (g_str_equal(service->eap, "tls") ||
7119                                 g_str_equal(service->eap, "sim") ||
7120                                 g_str_equal(service->eap, "aka"))
7121                                 break;
7122 #else
7123                         /*
7124                          * never request credentials if using EAP-TLS
7125                          * (EAP-TLS networks need to be fully provisioned)
7126                          */
7127                         if (g_str_equal(service->eap, "tls"))
7128                                 break;
7129 #endif
7130                         /*
7131                          * Return -ENOKEY if either identity or passphrase is
7132                          * missing. Agent provided credentials can be used as
7133                          * fallback if needed.
7134                          */
7135                         if (((!service->identity &&
7136                                         !service->agent_identity) ||
7137                                         !service->passphrase) ||
7138                                         service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
7139                                 return -ENOKEY;
7140
7141                         break;
7142                 }
7143                 break;
7144         }
7145
7146         if (service->network) {
7147                 if (!prepare_network(service))
7148                         return -EINVAL;
7149
7150                 switch (service->security) {
7151                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
7152                 case CONNMAN_SERVICE_SECURITY_NONE:
7153                 case CONNMAN_SERVICE_SECURITY_WEP:
7154                 case CONNMAN_SERVICE_SECURITY_PSK:
7155                 case CONNMAN_SERVICE_SECURITY_WPA:
7156                 case CONNMAN_SERVICE_SECURITY_RSN:
7157                         break;
7158                 case CONNMAN_SERVICE_SECURITY_8021X:
7159                         prepare_8021x(service);
7160                         break;
7161                 }
7162
7163                 if (__connman_stats_service_register(service) == 0) {
7164                         __connman_stats_get(service, false,
7165                                                 &service->stats.data);
7166                         __connman_stats_get(service, true,
7167                                                 &service->stats_roaming.data);
7168                 }
7169
7170                 if (service->ipconfig_ipv4)
7171                         __connman_ipconfig_enable(service->ipconfig_ipv4);
7172                 if (service->ipconfig_ipv6)
7173                         __connman_ipconfig_enable(service->ipconfig_ipv6);
7174
7175                 err = __connman_network_connect(service->network);
7176         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
7177                                         service->provider)
7178                 err = __connman_provider_connect(service->provider);
7179         else
7180                 return -EOPNOTSUPP;
7181
7182         if (err < 0) {
7183                 if (err != -EINPROGRESS) {
7184                         __connman_ipconfig_disable(service->ipconfig_ipv4);
7185                         __connman_ipconfig_disable(service->ipconfig_ipv6);
7186                         __connman_stats_service_unregister(service);
7187                 }
7188         }
7189
7190         return err;
7191 }
7192
7193 int __connman_service_connect(struct connman_service *service,
7194                         enum connman_service_connect_reason reason)
7195 {
7196         int err;
7197
7198         DBG("service %p state %s connect reason %s -> %s",
7199                 service, state2string(service->state),
7200                 reason2string(service->connect_reason),
7201                 reason2string(reason));
7202
7203         if (is_connected(service))
7204                 return -EISCONN;
7205
7206         if (is_connecting(service))
7207                 return -EALREADY;
7208
7209         switch (service->type) {
7210         case CONNMAN_SERVICE_TYPE_UNKNOWN:
7211         case CONNMAN_SERVICE_TYPE_SYSTEM:
7212         case CONNMAN_SERVICE_TYPE_GPS:
7213         case CONNMAN_SERVICE_TYPE_P2P:
7214                 return -EINVAL;
7215
7216         case CONNMAN_SERVICE_TYPE_ETHERNET:
7217         case CONNMAN_SERVICE_TYPE_GADGET:
7218         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
7219         case CONNMAN_SERVICE_TYPE_CELLULAR:
7220         case CONNMAN_SERVICE_TYPE_VPN:
7221         case CONNMAN_SERVICE_TYPE_WIFI:
7222                 break;
7223         }
7224
7225         if (!is_ipconfig_usable(service))
7226                 return -ENOLINK;
7227
7228         __connman_service_clear_error(service);
7229
7230         err = service_connect(service);
7231
7232         service->connect_reason = reason;
7233         if (err >= 0)
7234                 return 0;
7235
7236         if (err == -EINPROGRESS) {
7237                 if (service->timeout == 0)
7238                         service->timeout = g_timeout_add_seconds(
7239                                 CONNECT_TIMEOUT, connect_timeout, service);
7240
7241                 return -EINPROGRESS;
7242         }
7243
7244         if (service->network)
7245                 __connman_network_disconnect(service->network);
7246         else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
7247                                 service->provider)
7248                         connman_provider_disconnect(service->provider);
7249
7250         if (service->connect_reason == CONNMAN_SERVICE_CONNECT_REASON_USER) {
7251                 if (err == -ENOKEY || err == -EPERM) {
7252                         DBusMessage *pending = NULL;
7253
7254                         /*
7255                          * We steal the reply here. The idea is that the
7256                          * connecting client will see the connection status
7257                          * after the real hidden network is connected or
7258                          * connection failed.
7259                          */
7260                         if (service->hidden) {
7261                                 pending = service->pending;
7262                                 service->pending = NULL;
7263                         }
7264
7265                         err = __connman_agent_request_passphrase_input(service,
7266                                         request_input_cb,
7267                                         get_dbus_sender(service),
7268                                         pending);
7269                         if (service->hidden && err != -EINPROGRESS)
7270                                 service->pending = pending;
7271
7272                         return err;
7273                 }
7274                 reply_pending(service, -err);
7275         }
7276
7277         return err;
7278 }
7279
7280 int __connman_service_disconnect(struct connman_service *service)
7281 {
7282         int err;
7283
7284         DBG("service %p", service);
7285
7286         service->connect_reason = CONNMAN_SERVICE_CONNECT_REASON_NONE;
7287         service->proxy = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
7288
7289         connman_agent_cancel(service);
7290
7291         reply_pending(service, ECONNABORTED);
7292
7293         if (service->network) {
7294                 err = __connman_network_disconnect(service->network);
7295         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
7296                                         service->provider)
7297                 err = connman_provider_disconnect(service->provider);
7298         else
7299                 return -EOPNOTSUPP;
7300
7301         if (err < 0 && err != -EINPROGRESS)
7302                 return err;
7303
7304         __connman_6to4_remove(service->ipconfig_ipv4);
7305
7306         if (service->ipconfig_ipv4)
7307                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
7308                                                         NULL);
7309         else
7310                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
7311                                                         NULL);
7312
7313 #if defined TIZEN_EXT
7314         /**
7315           * Skip the functions If there is any connected profiles
7316           * that use same interface
7317           */
7318         if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR ||
7319                 __connman_service_get_connected_count_of_iface(service) <= 0) {
7320 #endif
7321         __connman_ipconfig_address_remove(service->ipconfig_ipv4);
7322         settings_changed(service, service->ipconfig_ipv4);
7323
7324         __connman_ipconfig_address_remove(service->ipconfig_ipv6);
7325         settings_changed(service, service->ipconfig_ipv6);
7326
7327         __connman_ipconfig_disable(service->ipconfig_ipv4);
7328         __connman_ipconfig_disable(service->ipconfig_ipv6);
7329 #if defined TIZEN_EXT
7330         }
7331 #endif
7332
7333         __connman_stats_service_unregister(service);
7334
7335         return err;
7336 }
7337
7338 int __connman_service_disconnect_all(void)
7339 {
7340         struct connman_service *service;
7341         GSList *services = NULL, *list;
7342         GList *iter;
7343
7344         DBG("");
7345
7346         for (iter = service_list; iter; iter = iter->next) {
7347                 service = iter->data;
7348
7349                 if (!is_connected(service))
7350                         break;
7351
7352                 services = g_slist_prepend(services, service);
7353         }
7354
7355         for (list = services; list; list = list->next) {
7356                 struct connman_service *service = list->data;
7357
7358                 service->ignore = true;
7359
7360                 __connman_service_disconnect(service);
7361         }
7362
7363         g_slist_free(services);
7364
7365         return 0;
7366 }
7367
7368 /**
7369  * lookup_by_identifier:
7370  * @identifier: service identifier
7371  *
7372  * Look up a service by identifier (reference count will not be increased)
7373  */
7374 static struct connman_service *lookup_by_identifier(const char *identifier)
7375 {
7376         return g_hash_table_lookup(service_hash, identifier);
7377 }
7378
7379 struct provision_user_data {
7380         const char *ident;
7381         int ret;
7382 };
7383
7384 static void provision_changed(gpointer value, gpointer user_data)
7385 {
7386         struct connman_service *service = value;
7387         struct provision_user_data *data = user_data;
7388         const char *path = data->ident;
7389         int ret;
7390
7391         ret = __connman_config_provision_service_ident(service, path,
7392                         service->config_file, service->config_entry);
7393         if (ret > 0)
7394                 data->ret = ret;
7395 }
7396
7397 int __connman_service_provision_changed(const char *ident)
7398 {
7399         struct provision_user_data data = {
7400                 .ident = ident,
7401                 .ret = 0
7402         };
7403
7404         g_list_foreach(service_list, provision_changed, (void *)&data);
7405
7406         /*
7407          * Because the provision_changed() might have set some services
7408          * as favorite, we must sort the sequence now.
7409          */
7410         if (services_dirty) {
7411                 services_dirty = false;
7412
7413                 service_list_sort();
7414
7415                 __connman_connection_update_gateway();
7416         }
7417
7418         return data.ret;
7419 }
7420
7421 void __connman_service_set_config(struct connman_service *service,
7422                                 const char *file_id, const char *entry)
7423 {
7424         if (!service)
7425                 return;
7426
7427         g_free(service->config_file);
7428         service->config_file = g_strdup(file_id);
7429
7430         g_free(service->config_entry);
7431         service->config_entry = g_strdup(entry);
7432 }
7433
7434 /**
7435  * __connman_service_get:
7436  * @identifier: service identifier
7437  *
7438  * Look up a service by identifier or create a new one if not found
7439  */
7440 static struct connman_service *service_get(const char *identifier)
7441 {
7442         struct connman_service *service;
7443
7444         service = g_hash_table_lookup(service_hash, identifier);
7445         if (service) {
7446                 connman_service_ref(service);
7447                 return service;
7448         }
7449
7450         service = connman_service_create();
7451         if (!service)
7452                 return NULL;
7453
7454         DBG("service %p", service);
7455
7456         service->identifier = g_strdup(identifier);
7457
7458         service_list = g_list_insert_sorted(service_list, service,
7459                                                 service_compare);
7460
7461         g_hash_table_insert(service_hash, service->identifier, service);
7462
7463         return service;
7464 }
7465
7466 static int service_register(struct connman_service *service)
7467 {
7468         DBG("service %p", service);
7469
7470         if (service->path)
7471                 return -EALREADY;
7472
7473         service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
7474                                                 service->identifier);
7475
7476         DBG("path %s", service->path);
7477
7478         if (__connman_config_provision_service(service) < 0)
7479                 service_load(service);
7480
7481         g_dbus_register_interface(connection, service->path,
7482                                         CONNMAN_SERVICE_INTERFACE,
7483                                         service_methods, service_signals,
7484                                                         NULL, service, NULL);
7485
7486         service_list_sort();
7487
7488         __connman_connection_update_gateway();
7489
7490         return 0;
7491 }
7492
7493 static void service_up(struct connman_ipconfig *ipconfig,
7494                 const char *ifname)
7495 {
7496         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7497
7498         DBG("%s up", ifname);
7499
7500         link_changed(service);
7501
7502         service->stats.valid = false;
7503         service->stats_roaming.valid = false;
7504 }
7505
7506 static void service_down(struct connman_ipconfig *ipconfig,
7507                         const char *ifname)
7508 {
7509         DBG("%s down", ifname);
7510 }
7511
7512 static void service_lower_up(struct connman_ipconfig *ipconfig,
7513                         const char *ifname)
7514 {
7515         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7516
7517         DBG("%s lower up", ifname);
7518
7519         stats_start(service);
7520 }
7521
7522 static void service_lower_down(struct connman_ipconfig *ipconfig,
7523                         const char *ifname)
7524 {
7525         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7526
7527         DBG("%s lower down", ifname);
7528
7529         if (!is_idle_state(service, service->state_ipv4))
7530                 __connman_ipconfig_disable(service->ipconfig_ipv4);
7531
7532         if (!is_idle_state(service, service->state_ipv6))
7533                 __connman_ipconfig_disable(service->ipconfig_ipv6);
7534
7535         stats_stop(service);
7536         service_save(service);
7537 }
7538
7539 static void service_ip_bound(struct connman_ipconfig *ipconfig,
7540                         const char *ifname)
7541 {
7542         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7543         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
7544         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
7545
7546         DBG("%s ip bound", ifname);
7547
7548         type = __connman_ipconfig_get_config_type(ipconfig);
7549         method = __connman_ipconfig_get_method(ipconfig);
7550
7551         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
7552                                                         type, method);
7553
7554         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
7555                         method == CONNMAN_IPCONFIG_METHOD_AUTO)
7556                 __connman_service_ipconfig_indicate_state(service,
7557                                                 CONNMAN_SERVICE_STATE_READY,
7558                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
7559
7560         settings_changed(service, ipconfig);
7561 }
7562
7563 static void service_ip_release(struct connman_ipconfig *ipconfig,
7564                         const char *ifname)
7565 {
7566         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7567         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
7568         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
7569
7570         DBG("%s ip release", ifname);
7571
7572         type = __connman_ipconfig_get_config_type(ipconfig);
7573         method = __connman_ipconfig_get_method(ipconfig);
7574
7575         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
7576                                                         type, method);
7577
7578         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
7579                         method == CONNMAN_IPCONFIG_METHOD_OFF)
7580                 __connman_service_ipconfig_indicate_state(service,
7581                                         CONNMAN_SERVICE_STATE_DISCONNECT,
7582                                         CONNMAN_IPCONFIG_TYPE_IPV6);
7583
7584         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
7585                         method == CONNMAN_IPCONFIG_METHOD_OFF)
7586                 __connman_service_ipconfig_indicate_state(service,
7587                                         CONNMAN_SERVICE_STATE_DISCONNECT,
7588                                         CONNMAN_IPCONFIG_TYPE_IPV4);
7589
7590         settings_changed(service, ipconfig);
7591 }
7592
7593 static void service_route_changed(struct connman_ipconfig *ipconfig,
7594                                 const char *ifname)
7595 {
7596         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7597
7598         DBG("%s route changed", ifname);
7599
7600         settings_changed(service, ipconfig);
7601 }
7602
7603 static const struct connman_ipconfig_ops service_ops = {
7604         .up             = service_up,
7605         .down           = service_down,
7606         .lower_up       = service_lower_up,
7607         .lower_down     = service_lower_down,
7608         .ip_bound       = service_ip_bound,
7609         .ip_release     = service_ip_release,
7610         .route_set      = service_route_changed,
7611         .route_unset    = service_route_changed,
7612 };
7613
7614 static struct connman_ipconfig *create_ip4config(struct connman_service *service,
7615                 int index, enum connman_ipconfig_method method)
7616 {
7617         struct connman_ipconfig *ipconfig_ipv4;
7618
7619         ipconfig_ipv4 = __connman_ipconfig_create(index,
7620                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
7621         if (!ipconfig_ipv4)
7622                 return NULL;
7623
7624         __connman_ipconfig_set_method(ipconfig_ipv4, method);
7625
7626         __connman_ipconfig_set_data(ipconfig_ipv4, service);
7627
7628         __connman_ipconfig_set_ops(ipconfig_ipv4, &service_ops);
7629
7630         return ipconfig_ipv4;
7631 }
7632
7633 static struct connman_ipconfig *create_ip6config(struct connman_service *service,
7634                 int index)
7635 {
7636         struct connman_ipconfig *ipconfig_ipv6;
7637
7638         ipconfig_ipv6 = __connman_ipconfig_create(index,
7639                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
7640         if (!ipconfig_ipv6)
7641                 return NULL;
7642
7643         __connman_ipconfig_set_data(ipconfig_ipv6, service);
7644
7645         __connman_ipconfig_set_ops(ipconfig_ipv6, &service_ops);
7646
7647         return ipconfig_ipv6;
7648 }
7649
7650 void __connman_service_read_ip4config(struct connman_service *service)
7651 {
7652         GKeyFile *keyfile;
7653
7654         if (!service->ipconfig_ipv4)
7655                 return;
7656
7657         keyfile = connman_storage_load_service(service->identifier);
7658         if (!keyfile)
7659                 return;
7660
7661         __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
7662                                 service->identifier, "IPv4.");
7663
7664         g_key_file_free(keyfile);
7665 }
7666
7667 void connman_service_create_ip4config(struct connman_service *service,
7668                                         int index)
7669 {
7670         DBG("ipv4 %p", service->ipconfig_ipv4);
7671
7672         if (service->ipconfig_ipv4)
7673                 return;
7674
7675         service->ipconfig_ipv4 = create_ip4config(service, index,
7676                         CONNMAN_IPCONFIG_METHOD_DHCP);
7677         __connman_service_read_ip4config(service);
7678 }
7679
7680 void __connman_service_read_ip6config(struct connman_service *service)
7681 {
7682         GKeyFile *keyfile;
7683
7684         if (!service->ipconfig_ipv6)
7685                 return;
7686
7687         keyfile = connman_storage_load_service(service->identifier);
7688         if (!keyfile)
7689                 return;
7690
7691         __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
7692                                 service->identifier, "IPv6.");
7693
7694         g_key_file_free(keyfile);
7695 }
7696
7697 void connman_service_create_ip6config(struct connman_service *service,
7698                                                                 int index)
7699 {
7700         DBG("ipv6 %p", service->ipconfig_ipv6);
7701
7702         if (service->ipconfig_ipv6)
7703                 return;
7704
7705         service->ipconfig_ipv6 = create_ip6config(service, index);
7706
7707         __connman_service_read_ip6config(service);
7708 }
7709
7710 /**
7711  * connman_service_lookup_from_network:
7712  * @network: network structure
7713  *
7714  * Look up a service by network (reference count will not be increased)
7715  */
7716 struct connman_service *connman_service_lookup_from_network(struct connman_network *network)
7717 {
7718         struct connman_service *service;
7719         const char *ident, *group;
7720         char *name;
7721
7722         if (!network)
7723                 return NULL;
7724
7725         ident = __connman_network_get_ident(network);
7726         if (!ident)
7727                 return NULL;
7728
7729         group = connman_network_get_group(network);
7730         if (!group)
7731                 return NULL;
7732
7733         name = g_strdup_printf("%s_%s_%s",
7734                         __connman_network_get_type(network), ident, group);
7735         service = lookup_by_identifier(name);
7736         g_free(name);
7737
7738         return service;
7739 }
7740
7741 struct connman_service *__connman_service_lookup_from_index(int index)
7742 {
7743         struct connman_service *service;
7744         GList *list;
7745
7746         for (list = service_list; list; list = list->next) {
7747                 service = list->data;
7748
7749                 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
7750                                                         == index)
7751                         return service;
7752
7753                 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
7754                                                         == index)
7755                         return service;
7756         }
7757
7758         return NULL;
7759 }
7760
7761 struct connman_service *__connman_service_lookup_from_ident(const char *identifier)
7762 {
7763         return lookup_by_identifier(identifier);
7764 }
7765
7766 const char *__connman_service_get_ident(struct connman_service *service)
7767 {
7768         return service->identifier;
7769 }
7770
7771 const char *__connman_service_get_path(struct connman_service *service)
7772 {
7773         return service->path;
7774 }
7775
7776 const char *__connman_service_get_name(struct connman_service *service)
7777 {
7778         return service->name;
7779 }
7780
7781 enum connman_service_state __connman_service_get_state(struct connman_service *service)
7782 {
7783         return service->state;
7784 }
7785
7786 unsigned int __connman_service_get_order(struct connman_service *service)
7787 {
7788         unsigned int order = 0;
7789
7790         if (!service)
7791                 return 0;
7792
7793         service->order = 0;
7794
7795         if (!service->favorite)
7796                 return 0;
7797
7798 #if defined TIZEN_EXT
7799         if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
7800                         service->do_split_routing == FALSE)
7801                 order = 10;
7802         else if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
7803                 if (service->order < 5)
7804                         order = 5;
7805         } else if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
7806                 order = 4;
7807         else if (service->type == CONNMAN_SERVICE_TYPE_BLUETOOTH)
7808                 order = 3;
7809         else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
7810                         __connman_service_is_internet_profile(service) == TRUE)
7811                 order = 1;
7812         else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
7813                         __connman_service_is_tethering_profile(service) == TRUE)
7814                 order = 0;
7815         else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
7816                 order = 0;
7817         else
7818                 order = 2;
7819 #else
7820         if (service == service_list->data)
7821                 order = 1;
7822
7823         if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
7824                         !service->do_split_routing) {
7825                 service->order = 10;
7826                 order = 10;
7827         }
7828 #endif
7829         DBG("service %p name %s order %d split %d", service, service->name,
7830                 order, service->do_split_routing);
7831
7832         return order;
7833 }
7834
7835 void __connman_service_update_ordering(void)
7836 {
7837         if (service_list && service_list->next)
7838                 service_list = g_list_sort(service_list, service_compare);
7839 }
7840
7841 static enum connman_service_type convert_network_type(struct connman_network *network)
7842 {
7843         enum connman_network_type type = connman_network_get_type(network);
7844
7845         switch (type) {
7846         case CONNMAN_NETWORK_TYPE_UNKNOWN:
7847         case CONNMAN_NETWORK_TYPE_VENDOR:
7848                 break;
7849         case CONNMAN_NETWORK_TYPE_ETHERNET:
7850                 return CONNMAN_SERVICE_TYPE_ETHERNET;
7851         case CONNMAN_NETWORK_TYPE_WIFI:
7852                 return CONNMAN_SERVICE_TYPE_WIFI;
7853         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
7854         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
7855                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
7856         case CONNMAN_NETWORK_TYPE_CELLULAR:
7857                 return CONNMAN_SERVICE_TYPE_CELLULAR;
7858         case CONNMAN_NETWORK_TYPE_GADGET:
7859                 return CONNMAN_SERVICE_TYPE_GADGET;
7860         }
7861
7862         return CONNMAN_SERVICE_TYPE_UNKNOWN;
7863 }
7864
7865 static enum connman_service_security convert_wifi_security(const char *security)
7866 {
7867         if (!security)
7868                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
7869         else if (g_str_equal(security, "none"))
7870                 return CONNMAN_SERVICE_SECURITY_NONE;
7871         else if (g_str_equal(security, "wep"))
7872                 return CONNMAN_SERVICE_SECURITY_WEP;
7873         else if (g_str_equal(security, "psk"))
7874                 return CONNMAN_SERVICE_SECURITY_PSK;
7875         else if (g_str_equal(security, "ieee8021x"))
7876                 return CONNMAN_SERVICE_SECURITY_8021X;
7877         else if (g_str_equal(security, "wpa"))
7878                 return CONNMAN_SERVICE_SECURITY_WPA;
7879         else if (g_str_equal(security, "rsn"))
7880                 return CONNMAN_SERVICE_SECURITY_RSN;
7881 #if defined TIZEN_EXT
7882         else if (g_str_equal(security, "ft_psk") == TRUE)
7883                 return CONNMAN_SERVICE_SECURITY_PSK;
7884         else if (g_str_equal(security, "ft_ieee8021x") == TRUE)
7885                 return CONNMAN_SERVICE_SECURITY_8021X;
7886 #endif
7887         else
7888                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
7889 }
7890
7891 static void update_from_network(struct connman_service *service,
7892                                         struct connman_network *network)
7893 {
7894         uint8_t strength = service->strength;
7895         const char *str;
7896
7897         DBG("service %p network %p", service, network);
7898
7899         if (is_connected(service))
7900                 return;
7901
7902         if (is_connecting(service))
7903                 return;
7904
7905         str = connman_network_get_string(network, "Name");
7906         if (str) {
7907                 g_free(service->name);
7908                 service->name = g_strdup(str);
7909                 service->hidden = false;
7910         } else {
7911                 g_free(service->name);
7912                 service->name = NULL;
7913                 service->hidden = true;
7914         }
7915
7916         service->strength = connman_network_get_strength(network);
7917         service->roaming = connman_network_get_bool(network, "Roaming");
7918
7919         if (service->strength == 0) {
7920                 /*
7921                  * Filter out 0-values; it's unclear what they mean
7922                  * and they cause anomalous sorting of the priority list.
7923                  */
7924                 service->strength = strength;
7925         }
7926
7927         str = connman_network_get_string(network, "WiFi.Security");
7928         service->security = convert_wifi_security(str);
7929
7930         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
7931                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
7932
7933         if (service->strength > strength && service->network) {
7934                 connman_network_unref(service->network);
7935                 service->network = connman_network_ref(network);
7936
7937                 strength_changed(service);
7938         }
7939
7940         if (!service->network)
7941                 service->network = connman_network_ref(network);
7942
7943         service_list_sort();
7944 }
7945
7946 /**
7947  * __connman_service_create_from_network:
7948  * @network: network structure
7949  *
7950  * Look up service by network and if not found, create one
7951  */
7952 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
7953 {
7954         struct connman_service *service;
7955         struct connman_device *device;
7956         const char *ident, *group;
7957         char *name;
7958         unsigned int *auto_connect_types;
7959         int i, index;
7960
7961         DBG("network %p", network);
7962
7963         if (!network)
7964                 return NULL;
7965
7966         ident = __connman_network_get_ident(network);
7967         if (!ident)
7968                 return NULL;
7969
7970         group = connman_network_get_group(network);
7971         if (!group)
7972                 return NULL;
7973
7974         name = g_strdup_printf("%s_%s_%s",
7975                         __connman_network_get_type(network), ident, group);
7976         service = service_get(name);
7977         g_free(name);
7978
7979         if (!service)
7980                 return NULL;
7981
7982         if (__connman_network_get_weakness(network))
7983                 return service;
7984
7985         if (service->path) {
7986                 update_from_network(service, network);
7987                 __connman_connection_update_gateway();
7988                 return service;
7989         }
7990
7991         service->type = convert_network_type(network);
7992
7993         auto_connect_types = connman_setting_get_uint_list("DefaultAutoConnectTechnologies");
7994         service->autoconnect = false;
7995         for (i = 0; auto_connect_types &&
7996                      auto_connect_types[i] != 0; i++) {
7997                 if (service->type == auto_connect_types[i]) {
7998                         service->autoconnect = true;
7999                         break;
8000                 }
8001         }
8002
8003         switch (service->type) {
8004         case CONNMAN_SERVICE_TYPE_UNKNOWN:
8005         case CONNMAN_SERVICE_TYPE_SYSTEM:
8006         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
8007         case CONNMAN_SERVICE_TYPE_GPS:
8008         case CONNMAN_SERVICE_TYPE_VPN:
8009         case CONNMAN_SERVICE_TYPE_GADGET:
8010         case CONNMAN_SERVICE_TYPE_WIFI:
8011         case CONNMAN_SERVICE_TYPE_CELLULAR:
8012         case CONNMAN_SERVICE_TYPE_P2P:
8013                 break;
8014         case CONNMAN_SERVICE_TYPE_ETHERNET:
8015                 service->favorite = true;
8016                 break;
8017         }
8018
8019         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
8020         service->state = combine_state(service->state_ipv4, service->state_ipv6);
8021
8022         update_from_network(service, network);
8023
8024         index = connman_network_get_index(network);
8025
8026         if (!service->ipconfig_ipv4)
8027                 service->ipconfig_ipv4 = create_ip4config(service, index,
8028                                 CONNMAN_IPCONFIG_METHOD_DHCP);
8029
8030         if (!service->ipconfig_ipv6)
8031                 service->ipconfig_ipv6 = create_ip6config(service, index);
8032
8033         service_register(service);
8034
8035         if (service->favorite) {
8036                 device = connman_network_get_device(service->network);
8037                 if (device && !connman_device_get_scanning(device)) {
8038
8039                         switch (service->type) {
8040                         case CONNMAN_SERVICE_TYPE_UNKNOWN:
8041                         case CONNMAN_SERVICE_TYPE_SYSTEM:
8042                         case CONNMAN_SERVICE_TYPE_P2P:
8043                                 break;
8044
8045                         case CONNMAN_SERVICE_TYPE_GADGET:
8046                         case CONNMAN_SERVICE_TYPE_ETHERNET:
8047                                 if (service->autoconnect) {
8048                                         __connman_service_connect(service,
8049                                                 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
8050                                         break;
8051                                 }
8052
8053                                 /* fall through */
8054                         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
8055                         case CONNMAN_SERVICE_TYPE_GPS:
8056                         case CONNMAN_SERVICE_TYPE_VPN:
8057                         case CONNMAN_SERVICE_TYPE_WIFI:
8058                         case CONNMAN_SERVICE_TYPE_CELLULAR:
8059                                 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
8060                                 break;
8061                         }
8062                 }
8063
8064 #if defined TIZEN_EXT
8065                 /* TIZEN synchronizes below information when the service creates */
8066                 if (service->eap != NULL)
8067                         connman_network_set_string(service->network, "WiFi.EAP",
8068                                                                 service->eap);
8069                 if (service->identity != NULL)
8070                         connman_network_set_string(service->network, "WiFi.Identity",
8071                                                                 service->identity);
8072                 if (service->phase2 != NULL)
8073                         connman_network_set_string(service->network, "WiFi.Phase2",
8074                                                                 service->phase2);
8075 #endif
8076         }
8077
8078         __connman_notifier_service_add(service, service->name);
8079         service_schedule_added(service);
8080
8081         return service;
8082 }
8083
8084 void __connman_service_update_from_network(struct connman_network *network)
8085 {
8086         bool need_sort = false;
8087         struct connman_service *service;
8088         uint8_t strength;
8089         bool roaming;
8090         const char *name;
8091         bool stats_enable;
8092
8093         service = connman_service_lookup_from_network(network);
8094         if (!service)
8095                 return;
8096
8097         if (!service->network)
8098                 return;
8099
8100 #if defined TIZEN_EXT
8101         if (service->storage_reload) {
8102                 service_load(service);
8103                 __connman_service_set_storage_reload(service, false);
8104         }
8105 #endif
8106
8107         name = connman_network_get_string(service->network, "Name");
8108         if (g_strcmp0(service->name, name) != 0) {
8109                 g_free(service->name);
8110                 service->name = g_strdup(name);
8111
8112                 if (allow_property_changed(service))
8113                         connman_dbus_property_changed_basic(service->path,
8114                                         CONNMAN_SERVICE_INTERFACE, "Name",
8115                                         DBUS_TYPE_STRING, &service->name);
8116         }
8117
8118         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
8119                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
8120
8121         strength = connman_network_get_strength(service->network);
8122         if (strength == service->strength)
8123                 goto roaming;
8124
8125         service->strength = strength;
8126         need_sort = true;
8127
8128         strength_changed(service);
8129
8130 roaming:
8131         roaming = connman_network_get_bool(service->network, "Roaming");
8132         if (roaming == service->roaming)
8133                 goto sorting;
8134
8135         stats_enable = stats_enabled(service);
8136         if (stats_enable)
8137                 stats_stop(service);
8138
8139         service->roaming = roaming;
8140         need_sort = true;
8141
8142         if (stats_enable)
8143                 stats_start(service);
8144
8145         roaming_changed(service);
8146
8147 sorting:
8148         if (need_sort) {
8149                 service_list_sort();
8150         }
8151 }
8152
8153 void __connman_service_remove_from_network(struct connman_network *network)
8154 {
8155         struct connman_service *service;
8156
8157         service = connman_service_lookup_from_network(network);
8158
8159         DBG("network %p service %p", network, service);
8160
8161         if (!service)
8162                 return;
8163
8164         service->ignore = true;
8165
8166         __connman_connection_gateway_remove(service,
8167                                         CONNMAN_IPCONFIG_TYPE_ALL);
8168
8169         connman_service_unref(service);
8170 }
8171
8172 /**
8173  * __connman_service_create_from_provider:
8174  * @provider: provider structure
8175  *
8176  * Look up service by provider and if not found, create one
8177  */
8178 struct connman_service *
8179 __connman_service_create_from_provider(struct connman_provider *provider)
8180 {
8181         struct connman_service *service;
8182         const char *ident, *str;
8183         char *name;
8184         int index = connman_provider_get_index(provider);
8185
8186         DBG("provider %p", provider);
8187
8188         ident = __connman_provider_get_ident(provider);
8189         if (!ident)
8190                 return NULL;
8191
8192         name = g_strdup_printf("vpn_%s", ident);
8193         service = service_get(name);
8194         g_free(name);
8195
8196         if (!service)
8197                 return NULL;
8198
8199         service->type = CONNMAN_SERVICE_TYPE_VPN;
8200         service->provider = connman_provider_ref(provider);
8201         service->autoconnect = false;
8202         service->favorite = true;
8203
8204         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
8205         service->state = combine_state(service->state_ipv4, service->state_ipv6);
8206
8207         str = connman_provider_get_string(provider, "Name");
8208         if (str) {
8209                 g_free(service->name);
8210                 service->name = g_strdup(str);
8211                 service->hidden = false;
8212         } else {
8213                 g_free(service->name);
8214                 service->name = NULL;
8215                 service->hidden = true;
8216         }
8217
8218         service->strength = 0;
8219
8220         if (!service->ipconfig_ipv4)
8221                 service->ipconfig_ipv4 = create_ip4config(service, index,
8222                                 CONNMAN_IPCONFIG_METHOD_MANUAL);
8223
8224         if (!service->ipconfig_ipv6)
8225                 service->ipconfig_ipv6 = create_ip6config(service, index);
8226
8227         service_register(service);
8228
8229         __connman_notifier_service_add(service, service->name);
8230         service_schedule_added(service);
8231
8232         return service;
8233 }
8234
8235 static void remove_unprovisioned_services(void)
8236 {
8237         gchar **services;
8238         GKeyFile *keyfile, *configkeyfile;
8239         char *file, *section;
8240         int i = 0;
8241
8242         services = connman_storage_get_services();
8243         if (!services)
8244                 return;
8245
8246         for (; services[i]; i++) {
8247                 file = section = NULL;
8248                 keyfile = configkeyfile = NULL;
8249
8250                 keyfile = connman_storage_load_service(services[i]);
8251                 if (!keyfile)
8252                         continue;
8253
8254                 file = g_key_file_get_string(keyfile, services[i],
8255                                         "Config.file", NULL);
8256                 if (!file)
8257                         goto next;
8258
8259                 section = g_key_file_get_string(keyfile, services[i],
8260                                         "Config.ident", NULL);
8261                 if (!section)
8262                         goto next;
8263
8264                 configkeyfile = __connman_storage_load_config(file);
8265                 if (!configkeyfile) {
8266                         /*
8267                          * Config file is missing, remove the provisioned
8268                          * service.
8269                          */
8270                         __connman_storage_remove_service(services[i]);
8271                         goto next;
8272                 }
8273
8274                 if (!g_key_file_has_group(configkeyfile, section))
8275                         /*
8276                          * Config section is missing, remove the provisioned
8277                          * service.
8278                          */
8279                         __connman_storage_remove_service(services[i]);
8280
8281         next:
8282                 if (keyfile)
8283                         g_key_file_free(keyfile);
8284
8285                 if (configkeyfile)
8286                         g_key_file_free(configkeyfile);
8287
8288                 g_free(section);
8289                 g_free(file);
8290         }
8291
8292         g_strfreev(services);
8293 }
8294
8295 static int agent_probe(struct connman_agent *agent)
8296 {
8297         DBG("agent %p", agent);
8298         return 0;
8299 }
8300
8301 static void agent_remove(struct connman_agent *agent)
8302 {
8303         DBG("agent %p", agent);
8304 }
8305
8306 static void *agent_context_ref(void *context)
8307 {
8308         struct connman_service *service = context;
8309
8310         return (void *)connman_service_ref(service);
8311 }
8312
8313 static void agent_context_unref(void *context)
8314 {
8315         struct connman_service *service = context;
8316
8317         connman_service_unref(service);
8318 }
8319
8320 static struct connman_agent_driver agent_driver = {
8321         .name           = "service",
8322         .interface      = CONNMAN_AGENT_INTERFACE,
8323         .probe          = agent_probe,
8324         .remove         = agent_remove,
8325         .context_ref    = agent_context_ref,
8326         .context_unref  = agent_context_unref,
8327 };
8328
8329 int __connman_service_init(void)
8330 {
8331         int err;
8332
8333         DBG("");
8334
8335         err = connman_agent_driver_register(&agent_driver);
8336         if (err < 0) {
8337                 connman_error("Cannot register agent driver for %s",
8338                                                 agent_driver.name);
8339                 return err;
8340         }
8341
8342         connection = connman_dbus_get_connection();
8343
8344         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
8345                                                         NULL, service_free);
8346
8347         services_notify = g_new0(struct _services_notify, 1);
8348         services_notify->remove = g_hash_table_new_full(g_str_hash,
8349                         g_str_equal, g_free, NULL);
8350         services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
8351
8352         remove_unprovisioned_services();
8353
8354         return 0;
8355 }
8356
8357 void __connman_service_cleanup(void)
8358 {
8359         DBG("");
8360
8361         if (vpn_autoconnect_timeout) {
8362                 g_source_remove(vpn_autoconnect_timeout);
8363                 vpn_autoconnect_timeout = 0;
8364         }
8365
8366         if (autoconnect_timeout != 0) {
8367                 g_source_remove(autoconnect_timeout);
8368                 autoconnect_timeout = 0;
8369         }
8370
8371         connman_agent_driver_unregister(&agent_driver);
8372
8373         g_list_free(service_list);
8374         service_list = NULL;
8375
8376         g_hash_table_destroy(service_hash);
8377         service_hash = NULL;
8378
8379         g_slist_free(counter_list);
8380         counter_list = NULL;
8381
8382         if (services_notify->id != 0) {
8383                 g_source_remove(services_notify->id);
8384                 service_send_changed(NULL);
8385                 g_hash_table_destroy(services_notify->remove);
8386                 g_hash_table_destroy(services_notify->add);
8387         }
8388         g_free(services_notify);
8389
8390         dbus_connection_unref(connection);
8391 }