Merge "Removed cap for mac override and change the label for connman dir" into tizen
[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                                 __connman_network_enable_ipconfig(service->network,
4044                                                         service->ipconfig_ipv4);
4045                                 __connman_network_enable_ipconfig(service->network,
4046                                                         service->ipconfig_ipv6);
4047                         }
4048
4049                         return __connman_error_failed(msg, -err);
4050                 }
4051
4052                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4053                         ipv4_configuration_changed(service);
4054                 else
4055                         ipv6_configuration_changed(service);
4056
4057                 if (is_connecting(service) || is_connected(service)) {
4058                         __connman_network_enable_ipconfig(service->network,
4059                                                         service->ipconfig_ipv4);
4060                         __connman_network_enable_ipconfig(service->network,
4061                                                         service->ipconfig_ipv6);
4062                 }
4063
4064                 service_save(service);
4065         } else
4066                 return __connman_error_invalid_property(msg);
4067
4068         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
4069 }
4070
4071 static void set_error(struct connman_service *service,
4072                                         enum connman_service_error error)
4073 {
4074         const char *str;
4075
4076         if (service->error == error)
4077                 return;
4078
4079         service->error = error;
4080
4081         if (!service->path)
4082                 return;
4083
4084         if (!allow_property_changed(service))
4085                 return;
4086
4087         str = error2string(service->error);
4088
4089         if (!str)
4090                 str = "";
4091
4092         connman_dbus_property_changed_basic(service->path,
4093                                 CONNMAN_SERVICE_INTERFACE, "Error",
4094                                 DBUS_TYPE_STRING, &str);
4095 }
4096
4097 static void set_idle(struct connman_service *service)
4098 {
4099         service->state = service->state_ipv4 = service->state_ipv6 =
4100                                                 CONNMAN_SERVICE_STATE_IDLE;
4101         set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
4102         state_changed(service);
4103 }
4104
4105 static DBusMessage *clear_property(DBusConnection *conn,
4106                                         DBusMessage *msg, void *user_data)
4107 {
4108         struct connman_service *service = user_data;
4109         const char *name;
4110
4111         DBG("service %p", service);
4112
4113         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
4114                                                         DBUS_TYPE_INVALID);
4115
4116         if (g_str_equal(name, "Error")) {
4117                 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
4118
4119                 g_get_current_time(&service->modified);
4120                 service_save(service);
4121         } else
4122                 return __connman_error_invalid_property(msg);
4123
4124         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
4125 }
4126
4127 static bool is_ipconfig_usable(struct connman_service *service)
4128 {
4129         if (!__connman_ipconfig_is_usable(service->ipconfig_ipv4) &&
4130                         !__connman_ipconfig_is_usable(service->ipconfig_ipv6))
4131                 return false;
4132
4133         return true;
4134 }
4135
4136 static bool is_ignore(struct connman_service *service)
4137 {
4138         if (!service->autoconnect)
4139                 return true;
4140
4141         if (service->roaming)
4142                 return true;
4143
4144         if (service->ignore)
4145                 return true;
4146
4147         if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4148                 return true;
4149
4150         if (!is_ipconfig_usable(service))
4151                 return true;
4152
4153         return false;
4154 }
4155
4156 static void disconnect_on_last_session(enum connman_service_type type)
4157 {
4158         GList *list;
4159
4160         for (list = service_list; list; list = list->next) {
4161                 struct connman_service *service = list->data;
4162
4163                 if (service->type != type)
4164                         continue;
4165
4166                 if (service->connect_reason != CONNMAN_SERVICE_CONNECT_REASON_SESSION)
4167                          continue;
4168
4169                 __connman_service_disconnect(service);
4170                 return;
4171         }
4172 }
4173
4174 static int active_sessions[MAX_CONNMAN_SERVICE_TYPES] = {};
4175 static int active_count = 0;
4176
4177 void __connman_service_set_active_session(bool enable, GSList *list)
4178 {
4179         if (!list)
4180                 return;
4181
4182         if (enable)
4183                 active_count++;
4184         else
4185                 active_count--;
4186
4187         while (list != NULL) {
4188                 enum connman_service_type type = GPOINTER_TO_INT(list->data);
4189
4190                 switch (type) {
4191                 case CONNMAN_SERVICE_TYPE_ETHERNET:
4192                 case CONNMAN_SERVICE_TYPE_WIFI:
4193                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4194                 case CONNMAN_SERVICE_TYPE_CELLULAR:
4195                 case CONNMAN_SERVICE_TYPE_GADGET:
4196                         if (enable)
4197                                 active_sessions[type]++;
4198                         else
4199                                 active_sessions[type]--;
4200                         break;
4201
4202                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4203                 case CONNMAN_SERVICE_TYPE_SYSTEM:
4204                 case CONNMAN_SERVICE_TYPE_GPS:
4205                 case CONNMAN_SERVICE_TYPE_VPN:
4206                 case CONNMAN_SERVICE_TYPE_P2P:
4207                         break;
4208                 }
4209
4210                 if (active_sessions[type] == 0)
4211                         disconnect_on_last_session(type);
4212
4213                 list = g_slist_next(list);
4214         }
4215
4216         DBG("eth %d wifi %d bt %d cellular %d gadget %d sessions %d",
4217                         active_sessions[CONNMAN_SERVICE_TYPE_ETHERNET],
4218                         active_sessions[CONNMAN_SERVICE_TYPE_WIFI],
4219                         active_sessions[CONNMAN_SERVICE_TYPE_BLUETOOTH],
4220                         active_sessions[CONNMAN_SERVICE_TYPE_CELLULAR],
4221                         active_sessions[CONNMAN_SERVICE_TYPE_GADGET],
4222                         active_count);
4223 }
4224
4225 struct preferred_tech_data {
4226         GList *preferred_list;
4227         enum connman_service_type type;
4228 };
4229
4230 static void preferred_tech_add_by_type(gpointer data, gpointer user_data)
4231 {
4232         struct connman_service *service = data;
4233         struct preferred_tech_data *tech_data = user_data;
4234
4235         if (service->type == tech_data->type) {
4236                 tech_data->preferred_list =
4237                         g_list_append(tech_data->preferred_list, service);
4238
4239                 DBG("type %d service %p %s", tech_data->type, service,
4240                                 service->name);
4241         }
4242 }
4243
4244 static GList *preferred_tech_list_get(void)
4245 {
4246         unsigned int *tech_array;
4247         struct preferred_tech_data tech_data = { 0, };
4248         int i;
4249
4250         tech_array = connman_setting_get_uint_list("PreferredTechnologies");
4251         if (!tech_array)
4252                 return NULL;
4253
4254         if (connman_setting_get_bool("SingleConnectedTechnology")) {
4255                 GList *list;
4256                 for (list = service_list; list; list = list->next) {
4257                         struct connman_service *service = list->data;
4258
4259                         if (!is_connected(service))
4260                                 break;
4261
4262                         if (service->connect_reason ==
4263                                         CONNMAN_SERVICE_CONNECT_REASON_USER) {
4264                                 DBG("service %p name %s is user connected",
4265                                                 service, service->name);
4266 #if defined TIZEN_EXT
4267                                 /* We can connect to a favorite service like
4268                                  * wifi even we have a userconnect for cellular
4269                                  * because we have refount for cellular service
4270                                  */
4271                                 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
4272                                         break;
4273
4274                                 if (service->type == CONNMAN_SERVICE_TYPE_BLUETOOTH)
4275                                         break;
4276 #endif
4277                                 return NULL;
4278                         }
4279                 }
4280         }
4281
4282         for (i = 0; tech_array[i] != 0; i += 1) {
4283                 tech_data.type = tech_array[i];
4284                 g_list_foreach(service_list, preferred_tech_add_by_type,
4285                                 &tech_data);
4286         }
4287
4288         return tech_data.preferred_list;
4289 }
4290
4291 static bool auto_connect_service(GList *services,
4292                                 enum connman_service_connect_reason reason,
4293                                 bool preferred)
4294 {
4295         struct connman_service *service = NULL;
4296         bool ignore[MAX_CONNMAN_SERVICE_TYPES] = { };
4297         bool autoconnecting = false;
4298         GList *list;
4299
4300         DBG("preferred %d sessions %d reason %s", preferred, active_count,
4301                 reason2string(reason));
4302
4303         ignore[CONNMAN_SERVICE_TYPE_VPN] = true;
4304
4305         for (list = services; list; list = list->next) {
4306                 service = list->data;
4307
4308                 if (ignore[service->type]) {
4309                         DBG("service %p type %s ignore", service,
4310                                 __connman_service_type2string(service->type));
4311                         continue;
4312                 }
4313
4314 #if defined TIZEN_EXT
4315                 DBG("service %p %s %s %s, favorite(%d), ignore(%d), hidden(%d, %d)",
4316                                 service, service->name,
4317                                 state2string(service->state),
4318                                 __connman_service_type2string(service->type),
4319                                 service->favorite, is_ignore(service),
4320                                 service->hidden, service->hidden_service);
4321
4322                 /* Tizen takes Wi-Fi as the highest priority into consideration. */
4323                 if (service->type != CONNMAN_SERVICE_TYPE_WIFI)
4324                         if (is_connecting(service) == TRUE || is_connected(service) == TRUE)
4325                                 continue;
4326 #endif
4327
4328                 if (service->pending ||
4329                                 is_connecting(service) ||
4330                                 is_connected(service)) {
4331                         if (!active_count)
4332                                 return true;
4333
4334                         ignore[service->type] = true;
4335                         autoconnecting = true;
4336
4337                         DBG("service %p type %s busy", service,
4338                                 __connman_service_type2string(service->type));
4339
4340                         continue;
4341                 }
4342
4343                 if (!service->favorite) {
4344                         if (preferred)
4345                                continue;
4346
4347                         return autoconnecting;
4348                 }
4349
4350                 if (is_ignore(service) || service->state !=
4351                                 CONNMAN_SERVICE_STATE_IDLE)
4352                         continue;
4353
4354                 if (autoconnecting && !active_sessions[service->type]) {
4355                         DBG("service %p type %s has no users", service,
4356                                 __connman_service_type2string(service->type));
4357                         continue;
4358                 }
4359
4360                 if (!is_service_owner_user_login(service)) {
4361                         DBG("favorite user not login, wifi auto connect denied");
4362                         continue;
4363                 }
4364
4365                 DBG("service %p %s %s", service, service->name,
4366                         (preferred) ? "preferred" : reason2string(reason));
4367
4368                 __connman_service_connect(service, reason);
4369
4370                 if (!active_count)
4371                         return true;
4372
4373                 ignore[service->type] = true;
4374         }
4375
4376         return autoconnecting;
4377 }
4378
4379 static gboolean run_auto_connect(gpointer data)
4380 {
4381         enum connman_service_connect_reason reason = GPOINTER_TO_UINT(data);
4382         bool autoconnecting = false;
4383         GList *preferred_tech;
4384
4385         autoconnect_timeout = 0;
4386
4387         DBG("");
4388
4389         preferred_tech = preferred_tech_list_get();
4390         if (preferred_tech) {
4391                 autoconnecting = auto_connect_service(preferred_tech, reason,
4392                                                         true);
4393                 g_list_free(preferred_tech);
4394         }
4395
4396         if (!autoconnecting || active_count)
4397                 auto_connect_service(service_list, reason, false);
4398
4399         return FALSE;
4400 }
4401
4402 void __connman_service_auto_connect(enum connman_service_connect_reason reason)
4403 {
4404         DBG("");
4405
4406         if (autoconnect_timeout != 0)
4407                 return;
4408
4409         if (!__connman_session_policy_autoconnect(reason))
4410                 return;
4411
4412 #if defined TIZEN_EXT
4413         /* Adding Timeout of 500ms before trying to auto connect.
4414          * This is done because of below scenario
4415          * 1. Device is connected to AP1
4416          * 2. WPS Connection request is initiated for AP2
4417          * 3. Immediately WPS Connection is Cancelled
4418          * When WPS Connection Connection is initiated for AP2 then
4419          * sometimes there is a scenario where connman gets in ASSOCIATED
4420          * state with AP1 due to autoconnect and subsequently the connection
4421          * initiated by AP1 fails and connman service for AP1 comes in
4422          * FAILURE state due to this when connection with AP2 is cancelled
4423          * then autoconnect with AP1 doesn't works because its autoconnection
4424          * is ignored as its last state was FAILURE rather than IDLE */
4425         autoconnect_timeout = g_timeout_add(500, run_auto_connect,
4426 #else
4427         autoconnect_timeout = g_timeout_add_seconds(0, run_auto_connect,
4428 #endif
4429                                                 GUINT_TO_POINTER(reason));
4430 }
4431
4432 static gboolean run_vpn_auto_connect(gpointer data) {
4433         GList *list;
4434         bool need_split = false;
4435
4436         vpn_autoconnect_timeout = 0;
4437
4438         for (list = service_list; list; list = list->next) {
4439                 struct connman_service *service = list->data;
4440                 int res;
4441
4442                 if (service->type != CONNMAN_SERVICE_TYPE_VPN)
4443                         continue;
4444
4445                 if (is_connected(service) || is_connecting(service)) {
4446                         if (!service->do_split_routing)
4447                                 need_split = true;
4448                         continue;
4449                 }
4450
4451                 if (is_ignore(service) || !service->favorite)
4452                         continue;
4453
4454                 if (need_split && !service->do_split_routing) {
4455                         DBG("service %p no split routing", service);
4456                         continue;
4457                 }
4458
4459                 DBG("service %p %s %s", service, service->name,
4460                                 service->do_split_routing ?
4461                                 "split routing" : "");
4462
4463                 res = __connman_service_connect(service,
4464                                 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
4465                 if (res < 0 && res != -EINPROGRESS)
4466                         continue;
4467
4468                 if (!service->do_split_routing)
4469                         need_split = true;
4470         }
4471
4472         return FALSE;
4473 }
4474
4475 static void vpn_auto_connect(void)
4476 {
4477         if (vpn_autoconnect_timeout)
4478                 return;
4479
4480         vpn_autoconnect_timeout =
4481                 g_timeout_add_seconds(0, run_vpn_auto_connect, NULL);
4482 }
4483
4484 static void remove_timeout(struct connman_service *service)
4485 {
4486         if (service->timeout > 0) {
4487                 g_source_remove(service->timeout);
4488                 service->timeout = 0;
4489         }
4490 }
4491
4492 static void reply_pending(struct connman_service *service, int error)
4493 {
4494         remove_timeout(service);
4495
4496         if (service->pending) {
4497                 connman_dbus_reply_pending(service->pending, error, NULL);
4498                 service->pending = NULL;
4499         }
4500
4501         if (service->provider_pending) {
4502                 connman_dbus_reply_pending(service->provider_pending,
4503                                                 error, service->path);
4504                 service->provider_pending = NULL;
4505         }
4506 }
4507
4508 bool
4509 __connman_service_is_provider_pending(struct connman_service *service)
4510 {
4511         if (!service)
4512                 return false;
4513
4514         if (service->provider_pending)
4515                 return true;
4516
4517         return false;
4518 }
4519
4520 void __connman_service_set_provider_pending(struct connman_service *service,
4521                                                         DBusMessage *msg)
4522 {
4523         if (service->provider_pending) {
4524                 DBG("service %p provider pending msg %p already exists",
4525                         service, service->provider_pending);
4526                 return;
4527         }
4528
4529         service->provider_pending = msg;
4530         return;
4531 }
4532
4533 static void check_pending_msg(struct connman_service *service)
4534 {
4535         if (!service->pending)
4536                 return;
4537
4538         DBG("service %p pending msg %p already exists", service,
4539                                                 service->pending);
4540         dbus_message_unref(service->pending);
4541 }
4542
4543 void __connman_service_set_hidden_data(struct connman_service *service,
4544                                                         gpointer user_data)
4545 {
4546         DBusMessage *pending = user_data;
4547
4548         DBG("service %p pending %p", service, pending);
4549
4550         if (!pending)
4551                 return;
4552
4553         check_pending_msg(service);
4554
4555         service->pending = pending;
4556 }
4557
4558 void __connman_service_return_error(struct connman_service *service,
4559                                 int error, gpointer user_data)
4560 {
4561         DBG("service %p error %d user_data %p", service, error, user_data);
4562
4563         __connman_service_set_hidden_data(service, user_data);
4564
4565         reply_pending(service, error);
4566 }
4567
4568 static gboolean connect_timeout(gpointer user_data)
4569 {
4570         struct connman_service *service = user_data;
4571         bool autoconnect = false;
4572
4573         DBG("service %p", service);
4574
4575         service->timeout = 0;
4576
4577         if (service->network)
4578                 __connman_network_disconnect(service->network);
4579         else if (service->provider)
4580                 connman_provider_disconnect(service->provider);
4581
4582         __connman_ipconfig_disable(service->ipconfig_ipv4);
4583         __connman_ipconfig_disable(service->ipconfig_ipv6);
4584
4585         __connman_stats_service_unregister(service);
4586
4587         if (service->pending) {
4588                 DBusMessage *reply;
4589
4590                 reply = __connman_error_operation_timeout(service->pending);
4591                 if (reply)
4592                         g_dbus_send_message(connection, reply);
4593
4594                 dbus_message_unref(service->pending);
4595                 service->pending = NULL;
4596         } else
4597                 autoconnect = true;
4598
4599         __connman_service_ipconfig_indicate_state(service,
4600                                         CONNMAN_SERVICE_STATE_FAILURE,
4601                                         CONNMAN_IPCONFIG_TYPE_IPV4);
4602         __connman_service_ipconfig_indicate_state(service,
4603                                         CONNMAN_SERVICE_STATE_FAILURE,
4604                                         CONNMAN_IPCONFIG_TYPE_IPV6);
4605
4606         if (autoconnect &&
4607                         service->connect_reason !=
4608                                 CONNMAN_SERVICE_CONNECT_REASON_USER)
4609                 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
4610
4611         return FALSE;
4612 }
4613
4614 static DBusMessage *connect_service(DBusConnection *conn,
4615                                         DBusMessage *msg, void *user_data)
4616 {
4617         struct connman_service *service = user_data;
4618         int index, err = 0;
4619         GList *list;
4620
4621         DBG("service %p", service);
4622
4623 #if defined TIZEN_EXT
4624         /*
4625          * Description: TIZEN implements system global connection management.
4626          */
4627         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
4628                 connman_service_user_pdn_connection_ref(service);
4629 #endif
4630
4631         if (service->pending)
4632                 return __connman_error_in_progress(msg);
4633
4634         if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
4635                 uid_t uid;
4636                 if (connman_dbus_get_connection_unix_user_sync(conn,
4637                                                 dbus_message_get_sender(msg),
4638                                                 &uid) < 0) {
4639                         DBG("Can not get unix user id!");
4640                         return __connman_error_permission_denied(msg);
4641                 }
4642
4643                 if (!__connman_service_is_user_allowed(CONNMAN_SERVICE_TYPE_WIFI, uid)) {
4644                         DBG("Not allow this user to connect this wifi service now!");
4645                         return __connman_error_permission_denied(msg);
4646                 }
4647
4648                 if (uid != USER_ROOT && uid != service->user.favorite_user)
4649                         service->request_passphrase_input = true;
4650
4651                 service->user.current_user = uid;
4652
4653                 if (!service->passphrase && uid == service->user.favorite_user) {
4654                         DBG("Now load this favorite user's passphrase.");
4655                         service_load_passphrase(service);
4656                 }
4657         }
4658
4659         index = __connman_service_get_index(service);
4660
4661         for (list = service_list; list; list = list->next) {
4662                 struct connman_service *temp = list->data;
4663
4664 #if defined TIZEN_EXT
4665                 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
4666                         break;
4667 #endif
4668                 if (!is_connecting(temp) && !is_connected(temp))
4669                         break;
4670
4671                 if (service == temp)
4672                         continue;
4673
4674                 if (service->type != temp->type)
4675                         continue;
4676
4677                 if (__connman_service_get_index(temp) == index &&
4678                                 __connman_service_disconnect(temp) == -EINPROGRESS)
4679                         err = -EINPROGRESS;
4680
4681         }
4682         if (err == -EINPROGRESS)
4683                 return __connman_error_operation_timeout(msg);
4684
4685         service->ignore = false;
4686
4687         service->pending = dbus_message_ref(msg);
4688
4689         err = __connman_service_connect(service,
4690                         CONNMAN_SERVICE_CONNECT_REASON_USER);
4691
4692         if (err == -EINPROGRESS)
4693                 return NULL;
4694
4695         if (service->pending) {
4696                 dbus_message_unref(service->pending);
4697                 service->pending = NULL;
4698         }
4699
4700         if (err < 0)
4701                 return __connman_error_failed(msg, -err);
4702
4703         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
4704 }
4705
4706 static DBusMessage *disconnect_service(DBusConnection *conn,
4707                                         DBusMessage *msg, void *user_data)
4708 {
4709         struct connman_service *service = user_data;
4710         int err;
4711
4712         DBG("service %p", service);
4713
4714 #if defined TIZEN_EXT
4715         /*
4716          * Description: TIZEN implements system global connection management.
4717          */
4718         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
4719                 if (connman_service_user_pdn_connection_unref_and_test(service) != TRUE)
4720                         return __connman_error_failed(msg, EISCONN);
4721
4722                 if (is_connected(service) == TRUE &&
4723                                 service == connman_service_get_default_connection())
4724                         return __connman_error_failed(msg, EISCONN);
4725         }
4726 #endif
4727
4728         if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
4729                 uid_t uid;
4730                 if (connman_dbus_get_connection_unix_user_sync(conn,
4731                                                 dbus_message_get_sender(msg),
4732                                                 &uid) < 0) {
4733                         DBG("Can not get unix user id!");
4734                         return __connman_error_permission_denied(msg);
4735                 }
4736
4737                 if (!connman_service_is_user_allowed(service, uid)) {
4738                         DBG("Not allow this user to disconnect this wifi service now!");
4739                         return __connman_error_permission_denied(msg);
4740                 }
4741         }
4742
4743         service->ignore = true;
4744
4745         err = __connman_service_disconnect(service);
4746         if (err < 0 && err != -EINPROGRESS)
4747                 return __connman_error_failed(msg, -err);
4748
4749         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
4750 }
4751
4752 #if defined TIZEN_EXT
4753 static void __connman_service_cleanup_network_8021x(struct connman_service *service)
4754 {
4755         if (service == NULL)
4756                 return;
4757
4758         DBG("service %p ", service);
4759
4760         connman_network_set_string(service->network, "WiFi.EAP", NULL);
4761         connman_network_set_string(service->network, "WiFi.Identity", NULL);
4762         connman_network_set_string(service->network, "WiFi.CACertFile", NULL);
4763         connman_network_set_string(service->network, "WiFi.ClientCertFile", NULL);
4764         connman_network_set_string(service->network, "WiFi.PrivateKeyFile", NULL);
4765         connman_network_set_string(service->network, "WiFi.PrivateKeyPassphrase", NULL);
4766         connman_network_set_string(service->network, "WiFi.Phase2", NULL);
4767 }
4768 #endif
4769
4770 bool __connman_service_remove(struct connman_service *service)
4771 {
4772         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET ||
4773                         service->type == CONNMAN_SERVICE_TYPE_GADGET)
4774                 return false;
4775
4776         if (service->immutable || service->hidden ||
4777                         __connman_provider_is_immutable(service->provider))
4778                 return false;
4779
4780         if (!service->favorite && service->state !=
4781                                                 CONNMAN_SERVICE_STATE_FAILURE)
4782                 return false;
4783
4784         __connman_service_disconnect(service);
4785
4786         g_free(service->passphrase);
4787         service->passphrase = NULL;
4788
4789         g_free(service->identity);
4790         service->identity = NULL;
4791
4792         g_free(service->agent_identity);
4793         service->agent_identity = NULL;
4794
4795         g_free(service->eap);
4796         service->eap = NULL;
4797
4798 #if defined TIZEN_EXT
4799         g_free(service->ca_cert_file);
4800         service->ca_cert_file = NULL;
4801
4802         g_free(service->client_cert_file);
4803         service->client_cert_file = NULL;
4804
4805         g_free(service->private_key_file);
4806         service->private_key_file = NULL;
4807
4808         g_free(service->private_key_passphrase);
4809         service->private_key_passphrase = NULL;
4810
4811         g_free(service->phase2);
4812         service->phase2 = NULL;
4813
4814         __connman_service_cleanup_network_8021x(service);
4815
4816         __connman_ipconfig_set_method(service->ipconfig_ipv4, CONNMAN_IPCONFIG_METHOD_DHCP);
4817         __connman_ipconfig_set_method(service->ipconfig_ipv6, CONNMAN_IPCONFIG_METHOD_AUTO);
4818         connman_service_set_proxy(service, NULL, false);
4819
4820         __connman_service_nameserver_clear(service);
4821
4822         g_strfreev(service->nameservers_config);
4823         service->nameservers_config = NULL;
4824
4825 #endif
4826
4827 #if defined TIZEN_EXT
4828         if (service->security != CONNMAN_SERVICE_SECURITY_8021X)
4829 #endif
4830         set_idle(service);
4831
4832         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4833
4834         service->user.favorite_user = USER_NONE;
4835
4836         __connman_service_set_favorite(service, false);
4837
4838         __connman_ipconfig_ipv6_reset_privacy(service->ipconfig_ipv6);
4839
4840 #if defined TIZEN_EXT
4841         __connman_storage_remove_service(service->identifier);
4842 #else
4843         service_save(service);
4844 #endif
4845
4846         return true;
4847 }
4848
4849 static DBusMessage *remove_service(DBusConnection *conn,
4850                                         DBusMessage *msg, void *user_data)
4851 {
4852         struct connman_service *service = user_data;
4853
4854         DBG("service %p", service);
4855
4856         if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
4857                 uid_t uid;
4858                 if (connman_dbus_get_connection_unix_user_sync(conn,
4859                                                 dbus_message_get_sender(msg),
4860                                                 &uid) < 0) {
4861                         DBG("Can not get unix user id!");
4862                         return __connman_error_permission_denied(msg);
4863                 }
4864
4865 #if !defined TIZEN_EXT
4866                 if (!connman_service_is_user_allowed(service, uid)) {
4867                         DBG("Not allow this user to remove this wifi service now!");
4868                         return __connman_error_permission_denied(msg);
4869                 }
4870 #endif
4871         }
4872
4873         if (!__connman_service_remove(service))
4874                 return __connman_error_not_supported(msg);
4875
4876         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
4877 }
4878
4879 static bool check_suitable_state(enum connman_service_state a,
4880                                         enum connman_service_state b)
4881 {
4882         /*
4883          * Special check so that "ready" service can be moved before
4884          * "online" one.
4885          */
4886         if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
4887                         b == CONNMAN_SERVICE_STATE_READY) ||
4888                 (b == CONNMAN_SERVICE_STATE_ONLINE &&
4889                         a == CONNMAN_SERVICE_STATE_READY))
4890                 return true;
4891
4892         return a == b;
4893 }
4894
4895 static void downgrade_state(struct connman_service *service)
4896 {
4897         if (!service)
4898                 return;
4899
4900         DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
4901                                                 service->state_ipv6);
4902
4903         if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
4904                 __connman_service_ipconfig_indicate_state(service,
4905                                                 CONNMAN_SERVICE_STATE_READY,
4906                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4907
4908         if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
4909                 __connman_service_ipconfig_indicate_state(service,
4910                                                 CONNMAN_SERVICE_STATE_READY,
4911                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4912 }
4913
4914 static void apply_relevant_default_downgrade(struct connman_service *service)
4915 {
4916         struct connman_service *def_service;
4917
4918         def_service = __connman_service_get_default();
4919         if (!def_service)
4920                 return;
4921
4922         if (def_service == service &&
4923                         def_service->state == CONNMAN_SERVICE_STATE_ONLINE) {
4924                 def_service->state = CONNMAN_SERVICE_STATE_READY;
4925                 __connman_notifier_leave_online(def_service->type);
4926                 state_changed(def_service);
4927         }
4928 }
4929
4930 static void switch_default_service(struct connman_service *default_service,
4931                 struct connman_service *downgrade_service)
4932 {
4933         struct connman_service *service;
4934         GList *src, *dst;
4935
4936         apply_relevant_default_downgrade(default_service);
4937         src = g_list_find(service_list, downgrade_service);
4938         dst = g_list_find(service_list, default_service);
4939
4940         /* Nothing to do */
4941         if (src == dst || src->next == dst)
4942                 return;
4943
4944         service = src->data;
4945         service_list = g_list_delete_link(service_list, src);
4946         service_list = g_list_insert_before(service_list, dst, service);
4947
4948         downgrade_state(downgrade_service);
4949 }
4950
4951 static DBusMessage *move_service(DBusConnection *conn,
4952                                         DBusMessage *msg, void *user_data,
4953                                                                 bool before)
4954 {
4955         struct connman_service *service = user_data;
4956         struct connman_service *target;
4957         const char *path;
4958         enum connman_ipconfig_method target4, target6;
4959         enum connman_ipconfig_method service4, service6;
4960
4961         DBG("service %p", service);
4962
4963         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
4964                                                         DBUS_TYPE_INVALID);
4965
4966         if (!service->favorite)
4967                 return __connman_error_not_supported(msg);
4968
4969         target = find_service(path);
4970         if (!target || !target->favorite || target == service)
4971                 return __connman_error_invalid_service(msg);
4972
4973         if (target->type == CONNMAN_SERVICE_TYPE_VPN) {
4974                 /*
4975                  * We only allow VPN route splitting if there are
4976                  * routes defined for a given VPN.
4977                  */
4978                 if (!__connman_provider_check_routes(target->provider)) {
4979                         connman_info("Cannot move service. "
4980                                 "No routes defined for provider %s",
4981                                 __connman_provider_get_ident(target->provider));
4982                         return __connman_error_invalid_service(msg);
4983                 }
4984
4985                 target->do_split_routing = true;
4986         } else
4987                 target->do_split_routing = false;
4988
4989         service->do_split_routing = false;
4990
4991         target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
4992         target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
4993         service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4994         service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4995
4996         DBG("target %s method %d/%d state %d/%d split %d", target->identifier,
4997                 target4, target6, target->state_ipv4, target->state_ipv6,
4998                 target->do_split_routing);
4999
5000         DBG("service %s method %d/%d state %d/%d", service->identifier,
5001                                 service4, service6,
5002                                 service->state_ipv4, service->state_ipv6);
5003
5004         /*
5005          * If method is OFF, then we do not need to check the corresponding
5006          * ipconfig state.
5007          */
5008         if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
5009                 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
5010                         if (!check_suitable_state(target->state_ipv6,
5011                                                         service->state_ipv6))
5012                                 return __connman_error_invalid_service(msg);
5013                 }
5014         }
5015
5016         if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
5017                 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
5018                         if (!check_suitable_state(target->state_ipv4,
5019                                                         service->state_ipv4))
5020                                 return __connman_error_invalid_service(msg);
5021                 }
5022         }
5023
5024         if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
5025                 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
5026                         if (!check_suitable_state(target->state_ipv6,
5027                                                         service->state_ipv6))
5028                                 return __connman_error_invalid_service(msg);
5029                 }
5030         }
5031
5032         if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
5033                 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
5034                         if (!check_suitable_state(target->state_ipv4,
5035                                                         service->state_ipv4))
5036                                 return __connman_error_invalid_service(msg);
5037                 }
5038         }
5039
5040         g_get_current_time(&service->modified);
5041         service_save(service);
5042         service_save(target);
5043
5044         /*
5045          * If the service which goes down is the default service and is
5046          * online, we downgrade directly its state to ready so:
5047          * the service which goes up, needs to recompute its state which
5048          * is triggered via downgrading it - if relevant - to state ready.
5049          */
5050         if (before)
5051                 switch_default_service(target, service);
5052         else
5053                 switch_default_service(service, target);
5054
5055         __connman_connection_update_gateway();
5056
5057         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
5058 }
5059
5060 static DBusMessage *move_before(DBusConnection *conn,
5061                                         DBusMessage *msg, void *user_data)
5062 {
5063         return move_service(conn, msg, user_data, true);
5064 }
5065
5066 static DBusMessage *move_after(DBusConnection *conn,
5067                                         DBusMessage *msg, void *user_data)
5068 {
5069         return move_service(conn, msg, user_data, false);
5070 }
5071
5072 static DBusMessage *reset_counters(DBusConnection *conn,
5073                                         DBusMessage *msg, void *user_data)
5074 {
5075         struct connman_service *service = user_data;
5076
5077         reset_stats(service);
5078
5079         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
5080 }
5081
5082 static DBusMessage *get_user_favorite(DBusConnection *conn,
5083                                         DBusMessage *msg, void *user_data)
5084 {
5085         DBusMessage *reply;
5086         uid_t uid = USER_NONE;
5087         dbus_bool_t user_favorite = false;
5088         struct connman_service *service = user_data;
5089
5090         connman_dbus_get_connection_unix_user_sync(conn,
5091                                         dbus_message_get_sender(msg),
5092                                         &uid);
5093         if (uid == USER_ROOT)
5094                 user_favorite = service->favorite;
5095         else if (uid != USER_NONE && uid == service->user.favorite_user) {
5096                 DBG("The service is favorite to this user!");
5097                 user_favorite = true;
5098         }
5099
5100         reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
5101         dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN,
5102                                 &user_favorite, DBUS_TYPE_INVALID);
5103         return reply;
5104 }
5105
5106 static struct _services_notify {
5107         int id;
5108         GHashTable *add;
5109         GHashTable *remove;
5110 } *services_notify;
5111
5112 static void service_append_added_foreach(gpointer data, gpointer user_data)
5113 {
5114         struct connman_service *service = data;
5115         DBusMessageIter *iter = user_data;
5116
5117         if (!service || !service->path) {
5118                 DBG("service %p or path is NULL", service);
5119                 return;
5120         }
5121
5122         if (g_hash_table_lookup(services_notify->add, service->path)) {
5123                 DBG("new %s", service->path);
5124
5125                 append_struct(service, iter);
5126                 g_hash_table_remove(services_notify->add, service->path);
5127         } else {
5128                 DBG("changed %s", service->path);
5129
5130                 append_struct_service(iter, NULL, service);
5131         }
5132 }
5133
5134 static void service_append_ordered(DBusMessageIter *iter, void *user_data)
5135 {
5136         g_list_foreach(service_list, service_append_added_foreach, iter);
5137 }
5138
5139 static void append_removed(gpointer key, gpointer value, gpointer user_data)
5140 {
5141         char *objpath = key;
5142         DBusMessageIter *iter = user_data;
5143
5144         DBG("removed %s", objpath);
5145         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath);
5146 }
5147
5148 static void service_append_removed(DBusMessageIter *iter, void *user_data)
5149 {
5150         g_hash_table_foreach(services_notify->remove, append_removed, iter);
5151 }
5152
5153 static gboolean service_send_changed(gpointer data)
5154 {
5155         DBusMessage *signal;
5156
5157         DBG("");
5158
5159         services_notify->id = 0;
5160
5161         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
5162                         CONNMAN_MANAGER_INTERFACE, "ServicesChanged");
5163         if (!signal)
5164                 return FALSE;
5165
5166         __connman_dbus_append_objpath_dict_array(signal,
5167                                         service_append_ordered, NULL);
5168         __connman_dbus_append_objpath_array(signal,
5169                                         service_append_removed, NULL);
5170
5171         dbus_connection_send(connection, signal, NULL);
5172         dbus_message_unref(signal);
5173
5174         g_hash_table_remove_all(services_notify->remove);
5175         g_hash_table_remove_all(services_notify->add);
5176
5177         return FALSE;
5178 }
5179
5180 static void service_schedule_changed(void)
5181 {
5182         if (services_notify->id != 0)
5183                 return;
5184
5185         services_notify->id = g_timeout_add(100, service_send_changed, NULL);
5186 }
5187
5188 static void service_schedule_added(struct connman_service *service)
5189 {
5190         DBG("service %p", service);
5191
5192         g_hash_table_remove(services_notify->remove, service->path);
5193         g_hash_table_replace(services_notify->add, service->path, service);
5194
5195         service_schedule_changed();
5196 }
5197
5198 static void service_schedule_removed(struct connman_service *service)
5199 {
5200         if (!service || !service->path) {
5201                 DBG("service %p or path is NULL", service);
5202                 return;
5203         }
5204
5205         DBG("service %p %s", service, service->path);
5206
5207         g_hash_table_remove(services_notify->add, service->path);
5208         g_hash_table_replace(services_notify->remove, g_strdup(service->path),
5209                         NULL);
5210
5211         service_schedule_changed();
5212 }
5213
5214 static bool allow_property_changed(struct connman_service *service)
5215 {
5216 #if defined TIZEN_EXT
5217         if (service->path == NULL)
5218                 return FALSE;
5219 #endif
5220         if (g_hash_table_lookup_extended(services_notify->add, service->path,
5221                                         NULL, NULL)) {
5222                 DBG("no property updates for service %p", service);
5223                 return false;
5224         }
5225
5226         return true;
5227 }
5228
5229 static const GDBusMethodTable service_methods[] = {
5230         { GDBUS_DEPRECATED_METHOD("GetProperties",
5231                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
5232                         get_properties) },
5233         { GDBUS_METHOD("SetProperty",
5234                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
5235                         NULL, set_property) },
5236         { GDBUS_METHOD("ClearProperty",
5237                         GDBUS_ARGS({ "name", "s" }), NULL,
5238                         clear_property) },
5239         { GDBUS_ASYNC_METHOD("Connect", NULL, NULL,
5240                               connect_service) },
5241         { GDBUS_METHOD("Disconnect", NULL, NULL,
5242                         disconnect_service) },
5243         { GDBUS_METHOD("Remove", NULL, NULL, remove_service) },
5244         { GDBUS_METHOD("MoveBefore",
5245                         GDBUS_ARGS({ "service", "o" }), NULL,
5246                         move_before) },
5247         { GDBUS_METHOD("MoveAfter",
5248                         GDBUS_ARGS({ "service", "o" }), NULL,
5249                         move_after) },
5250         { GDBUS_METHOD("ResetCounters", NULL, NULL, reset_counters) },
5251         { GDBUS_METHOD("GetUserFavorite",
5252                         NULL, GDBUS_ARGS({ "value", "v" }),
5253                         get_user_favorite) },
5254         { },
5255 };
5256
5257 static const GDBusSignalTable service_signals[] = {
5258         { GDBUS_SIGNAL("PropertyChanged",
5259                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
5260         { },
5261 };
5262
5263 static void service_free(gpointer user_data)
5264 {
5265         struct connman_service *service = user_data;
5266         char *path = service->path;
5267
5268         DBG("service %p", service);
5269
5270         reply_pending(service, ENOENT);
5271
5272         __connman_notifier_service_remove(service);
5273         service_schedule_removed(service);
5274
5275         __connman_wispr_stop(service);
5276         stats_stop(service);
5277
5278         service->path = NULL;
5279
5280         if (path) {
5281                 __connman_connection_update_gateway();
5282
5283                 g_dbus_unregister_interface(connection, path,
5284                                                 CONNMAN_SERVICE_INTERFACE);
5285                 g_free(path);
5286         }
5287
5288         g_hash_table_destroy(service->counter_table);
5289
5290         if (service->network) {
5291                 __connman_network_disconnect(service->network);
5292                 connman_network_unref(service->network);
5293                 service->network = NULL;
5294         }
5295
5296         if (service->provider)
5297                 connman_provider_unref(service->provider);
5298
5299         if (service->ipconfig_ipv4) {
5300                 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
5301                 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
5302                 __connman_ipconfig_unref(service->ipconfig_ipv4);
5303                 service->ipconfig_ipv4 = NULL;
5304         }
5305
5306         if (service->ipconfig_ipv6) {
5307                 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
5308                 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
5309                 __connman_ipconfig_unref(service->ipconfig_ipv6);
5310                 service->ipconfig_ipv6 = NULL;
5311         }
5312
5313         g_strfreev(service->timeservers);
5314         g_strfreev(service->timeservers_config);
5315         g_strfreev(service->nameservers);
5316         g_strfreev(service->nameservers_config);
5317         g_strfreev(service->nameservers_auto);
5318         g_strfreev(service->domains);
5319         g_strfreev(service->proxies);
5320         g_strfreev(service->excludes);
5321
5322         g_free(service->hostname);
5323         g_free(service->domainname);
5324         g_free(service->pac);
5325         g_free(service->name);
5326         g_free(service->passphrase);
5327         g_free(service->identifier);
5328         g_free(service->eap);
5329         g_free(service->identity);
5330         g_free(service->agent_identity);
5331         g_free(service->ca_cert_file);
5332         g_free(service->client_cert_file);
5333         g_free(service->private_key_file);
5334         g_free(service->private_key_passphrase);
5335         g_free(service->phase2);
5336         g_free(service->config_file);
5337         g_free(service->config_entry);
5338
5339         if (service->stats.timer)
5340                 g_timer_destroy(service->stats.timer);
5341         if (service->stats_roaming.timer)
5342                 g_timer_destroy(service->stats_roaming.timer);
5343
5344         if (current_default == service)
5345                 current_default = NULL;
5346
5347         g_free(service);
5348 }
5349
5350 static void stats_init(struct connman_service *service)
5351 {
5352         /* home */
5353         service->stats.valid = false;
5354         service->stats.enabled = false;
5355         service->stats.timer = g_timer_new();
5356
5357         /* roaming */
5358         service->stats_roaming.valid = false;
5359         service->stats_roaming.enabled = false;
5360         service->stats_roaming.timer = g_timer_new();
5361 }
5362
5363 static void service_initialize(struct connman_service *service)
5364 {
5365         DBG("service %p", service);
5366
5367         service->refcount = 1;
5368
5369         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
5370
5371         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
5372         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
5373
5374         service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
5375         service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
5376         service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
5377
5378         service->favorite  = false;
5379         service->immutable = false;
5380         service->hidden = false;
5381
5382         service->ignore = false;
5383
5384         service->user.favorite_user = USER_NONE;
5385         service->user.current_user = USER_NONE;
5386
5387         service->request_passphrase_input = false;
5388
5389         service->connect_reason = CONNMAN_SERVICE_CONNECT_REASON_NONE;
5390
5391         service->order = 0;
5392
5393         stats_init(service);
5394
5395         service->provider = NULL;
5396
5397         service->wps = false;
5398 #if defined TIZEN_EXT
5399         service->storage_reload = false;
5400         /*
5401          * Description: TIZEN implements system global connection management.
5402          */
5403         service->user_pdn_connection_refcount = 0;
5404         __sync_synchronize();
5405 #endif
5406 }
5407
5408 /**
5409  * connman_service_create:
5410  *
5411  * Allocate a new service.
5412  *
5413  * Returns: a newly-allocated #connman_service structure
5414  */
5415 struct connman_service *connman_service_create(void)
5416 {
5417         GSList *list;
5418         struct connman_stats_counter *counters;
5419         const char *counter;
5420
5421         struct connman_service *service;
5422
5423         service = g_try_new0(struct connman_service, 1);
5424         if (!service)
5425                 return NULL;
5426
5427         DBG("service %p", service);
5428
5429         service->counter_table = g_hash_table_new_full(g_str_hash,
5430                                                 g_str_equal, NULL, g_free);
5431
5432         for (list = counter_list; list; list = list->next) {
5433                 counter = list->data;
5434
5435                 counters = g_try_new0(struct connman_stats_counter, 1);
5436                 if (!counters) {
5437                         g_hash_table_destroy(service->counter_table);
5438                         g_free(service);
5439                         return NULL;
5440                 }
5441
5442                 counters->append_all = true;
5443
5444                 g_hash_table_replace(service->counter_table, (gpointer)counter,
5445                                 counters);
5446         }
5447
5448         service_initialize(service);
5449
5450         return service;
5451 }
5452
5453 /**
5454  * connman_service_ref:
5455  * @service: service structure
5456  *
5457  * Increase reference counter of service
5458  */
5459 struct connman_service *
5460 connman_service_ref_debug(struct connman_service *service,
5461                         const char *file, int line, const char *caller)
5462 {
5463         DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
5464                 file, line, caller);
5465
5466         __sync_fetch_and_add(&service->refcount, 1);
5467
5468         return service;
5469 }
5470
5471 /**
5472  * connman_service_unref:
5473  * @service: service structure
5474  *
5475  * Decrease reference counter of service and release service if no
5476  * longer needed.
5477  */
5478 void connman_service_unref_debug(struct connman_service *service,
5479                         const char *file, int line, const char *caller)
5480 {
5481         DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
5482                 file, line, caller);
5483
5484         if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
5485                 return;
5486
5487         service_list = g_list_remove(service_list, service);
5488
5489         __connman_service_disconnect(service);
5490
5491         g_hash_table_remove(service_hash, service->identifier);
5492 }
5493
5494 static gint service_compare(gconstpointer a, gconstpointer b)
5495 {
5496         struct connman_service *service_a = (void *) a;
5497         struct connman_service *service_b = (void *) b;
5498         enum connman_service_state state_a, state_b;
5499         bool a_connected, b_connected;
5500         gint strength;
5501
5502         state_a = service_a->state;
5503         state_b = service_b->state;
5504         a_connected = is_connected(service_a);
5505         b_connected = is_connected(service_b);
5506
5507         if (a_connected && b_connected) {
5508                 if (service_a->order > service_b->order)
5509                         return -1;
5510
5511                 if (service_a->order < service_b->order)
5512                         return 1;
5513         }
5514
5515         if (state_a != state_b) {
5516                 if (a_connected && b_connected) {
5517                         /* We prefer online over ready state */
5518                         if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
5519                                 return -1;
5520
5521                         if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
5522                                 return 1;
5523                 }
5524
5525                 if (a_connected)
5526                         return -1;
5527                 if (b_connected)
5528                         return 1;
5529
5530                 if (is_connecting(service_a))
5531                         return -1;
5532                 if (is_connecting(service_b))
5533                         return 1;
5534         }
5535
5536         if (service_a->favorite && !service_b->favorite)
5537                 return -1;
5538
5539         if (!service_a->favorite && service_b->favorite)
5540                 return 1;
5541
5542         if (service_a->type != service_b->type) {
5543
5544                 if (service_a->type == CONNMAN_SERVICE_TYPE_ETHERNET)
5545                         return -1;
5546                 if (service_b->type == CONNMAN_SERVICE_TYPE_ETHERNET)
5547                         return 1;
5548
5549                 if (service_a->type == CONNMAN_SERVICE_TYPE_WIFI)
5550                         return -1;
5551                 if (service_b->type == CONNMAN_SERVICE_TYPE_WIFI)
5552                         return 1;
5553
5554                 if (service_a->type == CONNMAN_SERVICE_TYPE_CELLULAR)
5555                         return -1;
5556                 if (service_b->type == CONNMAN_SERVICE_TYPE_CELLULAR)
5557                         return 1;
5558
5559                 if (service_a->type == CONNMAN_SERVICE_TYPE_BLUETOOTH)
5560                         return -1;
5561                 if (service_b->type == CONNMAN_SERVICE_TYPE_BLUETOOTH)
5562                         return 1;
5563
5564                 if (service_a->type == CONNMAN_SERVICE_TYPE_VPN)
5565                         return -1;
5566                 if (service_b->type == CONNMAN_SERVICE_TYPE_VPN)
5567                         return 1;
5568
5569                 if (service_a->type == CONNMAN_SERVICE_TYPE_GADGET)
5570                         return -1;
5571                 if (service_b->type == CONNMAN_SERVICE_TYPE_GADGET)
5572                         return 1;
5573         }
5574
5575         strength = (gint) service_b->strength - (gint) service_a->strength;
5576         if (strength)
5577                 return strength;
5578
5579         return g_strcmp0(service_a->name, service_b->name);
5580 }
5581
5582 static void service_list_sort(void)
5583 {
5584         if (service_list && service_list->next) {
5585                 service_list = g_list_sort(service_list, service_compare);
5586                 service_schedule_changed();
5587         }
5588 }
5589
5590 /**
5591  * connman_service_get_type:
5592  * @service: service structure
5593  *
5594  * Get the type of service
5595  */
5596 enum connman_service_type connman_service_get_type(struct connman_service *service)
5597 {
5598         if (!service)
5599                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5600
5601         return service->type;
5602 }
5603
5604 /**
5605  * connman_service_get_interface:
5606  * @service: service structure
5607  *
5608  * Get network interface of service
5609  */
5610 char *connman_service_get_interface(struct connman_service *service)
5611 {
5612         int index;
5613
5614         if (!service)
5615                 return NULL;
5616
5617         index = __connman_service_get_index(service);
5618
5619         return connman_inet_ifname(index);
5620 }
5621
5622 /**
5623  * __connman_service_is_user_allowed:
5624  * @type: service type
5625  * @uid: user id
5626  *
5627  * Check a user is allowed to operate a type of service
5628  */
5629 bool __connman_service_is_user_allowed(enum connman_service_type type,
5630                                         uid_t uid)
5631 {
5632         GList *list;
5633         uid_t owner_user = USER_NONE;
5634
5635         for (list = service_list; list; list = list->next) {
5636                 struct connman_service *service = list->data;
5637
5638                 if (service->type != type)
5639                         continue;
5640
5641                 if (is_connected(service)) {
5642                         owner_user = service->user.favorite_user;
5643                         break;
5644                 }
5645         }
5646
5647         if (uid == USER_NONE ||
5648                         (uid != USER_ROOT &&
5649                         owner_user != USER_NONE &&
5650                         owner_user != uid))
5651                 return false;
5652
5653         return true;
5654 }
5655
5656 /**
5657  * connman_service_get_network:
5658  * @service: service structure
5659  *
5660  * Get the service network
5661  */
5662 struct connman_network *
5663 __connman_service_get_network(struct connman_service *service)
5664 {
5665         if (!service)
5666                 return NULL;
5667
5668         return service->network;
5669 }
5670
5671 struct connman_ipconfig *
5672 __connman_service_get_ip4config(struct connman_service *service)
5673 {
5674         if (!service)
5675                 return NULL;
5676
5677         return service->ipconfig_ipv4;
5678 }
5679
5680 struct connman_ipconfig *
5681 __connman_service_get_ip6config(struct connman_service *service)
5682 {
5683         if (!service)
5684                 return NULL;
5685
5686         return service->ipconfig_ipv6;
5687 }
5688
5689 struct connman_ipconfig *
5690 __connman_service_get_ipconfig(struct connman_service *service, int family)
5691 {
5692         if (family == AF_INET)
5693                 return __connman_service_get_ip4config(service);
5694         else if (family == AF_INET6)
5695                 return __connman_service_get_ip6config(service);
5696         else
5697                 return NULL;
5698
5699 }
5700
5701 bool __connman_service_is_connected_state(struct connman_service *service,
5702                                         enum connman_ipconfig_type type)
5703 {
5704         if (!service)
5705                 return false;
5706
5707         switch (type) {
5708         case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
5709                 break;
5710         case CONNMAN_IPCONFIG_TYPE_IPV4:
5711                 return is_connected_state(service, service->state_ipv4);
5712         case CONNMAN_IPCONFIG_TYPE_IPV6:
5713                 return is_connected_state(service, service->state_ipv6);
5714         case CONNMAN_IPCONFIG_TYPE_ALL:
5715                 return is_connected_state(service,
5716                                         CONNMAN_IPCONFIG_TYPE_IPV4) &&
5717                         is_connected_state(service,
5718                                         CONNMAN_IPCONFIG_TYPE_IPV6);
5719         }
5720
5721         return false;
5722 }
5723 enum connman_service_security __connman_service_get_security(
5724                                 struct connman_service *service)
5725 {
5726         if (!service)
5727                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5728
5729         return service->security;
5730 }
5731
5732 const char *__connman_service_get_phase2(struct connman_service *service)
5733 {
5734         if (!service)
5735                 return NULL;
5736
5737         return service->phase2;
5738 }
5739
5740 bool __connman_service_wps_enabled(struct connman_service *service)
5741 {
5742         if (!service)
5743                 return false;
5744
5745         return service->wps;
5746 }
5747
5748 void __connman_service_mark_dirty(void)
5749 {
5750         services_dirty = true;
5751 }
5752
5753 #if defined TIZEN_EXT
5754 /**
5755   * Returns profile count if there is any connected profiles
5756   * that use same interface
5757   */
5758 int __connman_service_get_connected_count_of_iface(
5759                                         struct connman_service *service)
5760 {
5761         GList *list;
5762         int count = 0;
5763         int index1 = 0;
5764         int index2 = 0;
5765
5766         DBG("");
5767
5768         index1 = __connman_service_get_index(service);
5769
5770         if (index1 <= 0)
5771                 return 0;
5772
5773         for (list = service_list; list; list = list->next) {
5774                 struct connman_service *service2 = list->data;
5775
5776                 if (service == service2)
5777                         continue;
5778
5779                 index2 = __connman_service_get_index(service2);
5780
5781                 if (is_connected(service2) && index2 > 0 && index1 == index2)
5782                         count++;
5783
5784                 index2 = 0;
5785         }
5786
5787         DBG("Interface index %d, count %d", index1, count);
5788
5789         return count;
5790 }
5791
5792 void __connman_service_set_storage_reload(struct connman_service *service,
5793                                         bool storage_reload)
5794 {
5795         if (service != NULL)
5796                 service->storage_reload = storage_reload;
5797 }
5798 #endif
5799
5800 /**
5801  * __connman_service_set_favorite_delayed:
5802  * @service: service structure
5803  * @favorite: favorite value
5804  * @delay_ordering: do not order service sequence
5805  *
5806  * Change the favorite setting of service
5807  */
5808 int __connman_service_set_favorite_delayed(struct connman_service *service,
5809                                         bool favorite,
5810                                         bool delay_ordering)
5811 {
5812 #if defined TIZEN_EXT
5813         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
5814                 return -EIO;
5815 #endif
5816         if (service->hidden)
5817                 return -EOPNOTSUPP;
5818
5819         if (service->favorite == favorite)
5820                 return -EALREADY;
5821
5822         service->favorite = favorite;
5823
5824         if (!delay_ordering)
5825                 __connman_service_get_order(service);
5826
5827         favorite_changed(service);
5828
5829         if (!delay_ordering) {
5830
5831                 service_list_sort();
5832
5833                 __connman_connection_update_gateway();
5834         }
5835
5836         return 0;
5837 }
5838
5839 /**
5840  * __connman_service_set_favorite:
5841  * @service: service structure
5842  * @favorite: favorite value
5843  *
5844  * Change the favorite setting of service
5845  */
5846 int __connman_service_set_favorite(struct connman_service *service,
5847                                                 bool favorite)
5848 {
5849         return __connman_service_set_favorite_delayed(service, favorite,
5850                                                         false);
5851 }
5852
5853 bool connman_service_get_favorite(struct connman_service *service)
5854 {
5855         return service->favorite;
5856 }
5857
5858 bool connman_service_get_autoconnect(struct connman_service *service)
5859 {
5860         return service->autoconnect;
5861 }
5862
5863 int __connman_service_set_immutable(struct connman_service *service,
5864                                                 bool immutable)
5865 {
5866         if (service->hidden)
5867                 return -EOPNOTSUPP;
5868
5869         if (service->immutable == immutable)
5870                 return 0;
5871
5872         service->immutable = immutable;
5873
5874         immutable_changed(service);
5875
5876         return 0;
5877 }
5878
5879 int __connman_service_set_ignore(struct connman_service *service,
5880                                                 bool ignore)
5881 {
5882         if (!service)
5883                 return -EINVAL;
5884
5885         service->ignore = ignore;
5886
5887         return 0;
5888 }
5889
5890 void __connman_service_set_string(struct connman_service *service,
5891                                   const char *key, const char *value)
5892 {
5893         if (service->hidden)
5894                 return;
5895         if (g_str_equal(key, "EAP")) {
5896                 g_free(service->eap);
5897                 service->eap = g_strdup(value);
5898         } else if (g_str_equal(key, "Identity")) {
5899                 g_free(service->identity);
5900                 service->identity = g_strdup(value);
5901         } else if (g_str_equal(key, "CACertFile")) {
5902                 g_free(service->ca_cert_file);
5903                 service->ca_cert_file = g_strdup(value);
5904         } else if (g_str_equal(key, "ClientCertFile")) {
5905                 g_free(service->client_cert_file);
5906                 service->client_cert_file = g_strdup(value);
5907         } else if (g_str_equal(key, "PrivateKeyFile")) {
5908                 g_free(service->private_key_file);
5909                 service->private_key_file = g_strdup(value);
5910         } else if (g_str_equal(key, "PrivateKeyPassphrase")) {
5911                 g_free(service->private_key_passphrase);
5912                 service->private_key_passphrase = g_strdup(value);
5913         } else if (g_str_equal(key, "Phase2")) {
5914                 g_free(service->phase2);
5915                 service->phase2 = g_strdup(value);
5916         } else if (g_str_equal(key, "Passphrase"))
5917                 __connman_service_set_passphrase(service, value);
5918 }
5919
5920 void __connman_service_set_search_domains(struct connman_service *service,
5921                                         char **domains)
5922 {
5923         searchdomain_remove_all(service);
5924
5925         if (service->domains)
5926                 g_strfreev(service->domains);
5927
5928         service->domains = g_strdupv(domains);
5929
5930         searchdomain_add_all(service);
5931 }
5932
5933 static void service_complete(struct connman_service *service)
5934 {
5935         reply_pending(service, EIO);
5936
5937         if (service->connect_reason != CONNMAN_SERVICE_CONNECT_REASON_USER)
5938                 __connman_service_auto_connect(service->connect_reason);
5939
5940         g_get_current_time(&service->modified);
5941         service_save(service);
5942 }
5943
5944 static void report_error_cb(void *user_context, bool retry,
5945                                                         void *user_data)
5946 {
5947         struct connman_service *service = user_context;
5948
5949         if (retry)
5950                 __connman_service_connect(service,
5951                                         CONNMAN_SERVICE_CONNECT_REASON_USER);
5952         else {
5953                 /* It is not relevant to stay on Failure state
5954                  * when failing is due to wrong user input */
5955                 __connman_service_clear_error(service);
5956 #if defined TIZEN_EXT
5957                 /* Reseting the state back in case of failure state */
5958                 service->state_ipv4 = service->state_ipv6 =
5959                                 CONNMAN_SERVICE_STATE_IDLE;
5960
5961                 if (service->error != CONNMAN_SERVICE_ERROR_AUTH_FAILED)
5962                         set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
5963 #endif
5964                 service_complete(service);
5965                 __connman_connection_update_gateway();
5966         }
5967 }
5968
5969 static int check_wpspin(struct connman_service *service, const char *wpspin)
5970 {
5971         int length;
5972         guint i;
5973
5974         if (!wpspin)
5975                 return 0;
5976
5977         length = strlen(wpspin);
5978
5979         /* If 0, it will mean user wants to use PBC method */
5980         if (length == 0) {
5981                 connman_network_set_string(service->network,
5982                                                         "WiFi.PinWPS", NULL);
5983                 return 0;
5984         }
5985
5986         /* A WPS PIN is always 8 chars length,
5987          * its content is in digit representation.
5988          */
5989         if (length != 8)
5990                 return -ENOKEY;
5991
5992         for (i = 0; i < 8; i++)
5993                 if (!isdigit((unsigned char) wpspin[i]))
5994                         return -ENOKEY;
5995
5996         connman_network_set_string(service->network, "WiFi.PinWPS", wpspin);
5997
5998         return 0;
5999 }
6000
6001 #if defined TIZEN_EXT
6002 static int __connman_service_connect_hidden(struct connman_service *service,
6003                         const char *name, int name_len,
6004                         const char *identity, const char *passphrase, void *user_data)
6005 {
6006         GList *list;
6007
6008         for (list = service_list; list; list = list->next) {
6009                 struct connman_service *target = list->data;
6010                 const char *target_ssid = NULL;
6011                 unsigned int target_ssid_len = 0;
6012
6013                 if (service->network != NULL &&
6014                                         service->security == target->security) {
6015                         target_ssid = connman_network_get_blob(service->network,
6016                                                         "WiFi.SSID", &target_ssid_len);
6017                         if (target_ssid_len == name_len &&
6018                                                         memcmp(target_ssid, name, name_len) == 0) {
6019                                 return connman_network_connect_hidden(service->network,
6020                                                         (char *)identity, (char *)passphrase, user_data);
6021                         }
6022                 }
6023         }
6024
6025         return -ENOENT;
6026 }
6027 #endif
6028
6029 static void request_input_cb(struct connman_service *service,
6030                         bool values_received,
6031                         const char *name, int name_len,
6032                         const char *identity, const char *passphrase,
6033                         bool wps, const char *wpspin,
6034                         const char *error, void *user_data)
6035 {
6036         struct connman_device *device;
6037         const char *security;
6038         int err = 0;
6039
6040         DBG("RequestInput return, %p", service);
6041
6042         if (error) {
6043                 DBG("error: %s", error);
6044
6045                 if (g_strcmp0(error,
6046                                 "net.connman.Agent.Error.Canceled") == 0) {
6047                         err = -EINVAL;
6048
6049                         if (service->hidden)
6050                                 __connman_service_return_error(service,
6051                                                         ECANCELED, user_data);
6052                         goto done;
6053                 } else {
6054                         if (service->hidden)
6055                                 __connman_service_return_error(service,
6056                                                         ETIMEDOUT, user_data);
6057                 }
6058         }
6059
6060         if (service->hidden && name_len > 0 && name_len <= 32) {
6061 #if defined TIZEN_EXT
6062                 /* TIZEN already has Wi-Fi hidden scan before this hidden connection */
6063                 err = __connman_service_connect_hidden(service, name, name_len,
6064                                                 identity, passphrase, user_data);
6065                 if (err == 0 || err == -EALREADY || err == -EINPROGRESS)
6066                         return;
6067 #endif
6068
6069                 device = connman_network_get_device(service->network);
6070                 security = connman_network_get_string(service->network,
6071                                                         "WiFi.Security");
6072                 err = __connman_device_request_hidden_scan(device,
6073                                                 name, name_len,
6074                                                 identity, passphrase,
6075                                                 security, user_data);
6076                 if (err < 0)
6077                         __connman_service_return_error(service, -err,
6078                                                         user_data);
6079         }
6080
6081         if (!values_received || service->hidden) {
6082                 err = -EINVAL;
6083                 goto done;
6084         }
6085
6086         if (wps && service->network) {
6087                 err = check_wpspin(service, wpspin);
6088                 if (err < 0)
6089                         goto done;
6090
6091                 connman_network_set_bool(service->network, "WiFi.UseWPS", wps);
6092         }
6093
6094         if (identity)
6095                 __connman_service_set_agent_identity(service, identity);
6096
6097         if (passphrase)
6098                 err = __connman_service_set_passphrase(service, passphrase);
6099
6100  done:
6101         if (err >= 0) {
6102                 /* We forget any previous error. */
6103                 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
6104
6105                 __connman_service_connect(service,
6106                                         CONNMAN_SERVICE_CONNECT_REASON_USER);
6107
6108         } else if (err == -ENOKEY) {
6109                 __connman_service_indicate_error(service,
6110                                         CONNMAN_SERVICE_ERROR_INVALID_KEY);
6111         } else {
6112                 /* It is not relevant to stay on Failure state
6113                  * when failing is due to wrong user input */
6114                 service->state = CONNMAN_SERVICE_STATE_IDLE;
6115
6116                 if (!service->hidden) {
6117                         /*
6118                          * If there was a real error when requesting
6119                          * hidden scan, then that error is returned already
6120                          * to the user somewhere above so do not try to
6121                          * do this again.
6122                          */
6123                         __connman_service_return_error(service, -err,
6124                                                         user_data);
6125                 }
6126
6127                 service_complete(service);
6128                 __connman_connection_update_gateway();
6129         }
6130 }
6131
6132 static void downgrade_connected_services(void)
6133 {
6134         struct connman_service *up_service;
6135         GList *list;
6136
6137         for (list = service_list; list; list = list->next) {
6138                 up_service = list->data;
6139
6140                 if (!is_connected(up_service))
6141                         continue;
6142
6143                 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
6144                         return;
6145
6146                 downgrade_state(up_service);
6147         }
6148 }
6149
6150 static int service_update_preferred_order(struct connman_service *default_service,
6151                 struct connman_service *new_service,
6152                 enum connman_service_state new_state)
6153 {
6154         unsigned int *tech_array;
6155         int i;
6156
6157         if (!default_service || default_service == new_service ||
6158                         default_service->state != new_state)
6159                 return 0;
6160
6161         tech_array = connman_setting_get_uint_list("PreferredTechnologies");
6162         if (tech_array) {
6163
6164                 for (i = 0; tech_array[i] != 0; i += 1) {
6165                         if (default_service->type == tech_array[i])
6166                                 return -EALREADY;
6167
6168                         if (new_service->type == tech_array[i]) {
6169                                 switch_default_service(default_service,
6170                                                 new_service);
6171                                 __connman_connection_update_gateway();
6172                                 return 0;
6173                         }
6174                 }
6175         }
6176
6177         return -EALREADY;
6178 }
6179
6180 #if defined TIZEN_EXT
6181 static gboolean __connman_service_can_drop(struct connman_service *service)
6182 {
6183         if (is_connected(service) == TRUE || is_connecting(service) == TRUE) {
6184                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
6185                         return TRUE;
6186                 else if (connman_service_is_no_ref_user_pdn_connection(service) == TRUE)
6187                         return TRUE;
6188         }
6189         return FALSE;
6190 }
6191
6192 static struct connman_device *default_connecting_device = NULL;
6193
6194 static void __connman_service_disconnect_default(struct connman_service *service)
6195 {
6196         struct connman_device *default_device = NULL;
6197
6198         if (default_connecting_device == NULL)
6199                 return;
6200
6201         default_device = connman_network_get_device(
6202                         __connman_service_get_network(service));
6203
6204         DBG("Disconnecting service %p %s", service, service->path);
6205         DBG("Disconnecting device %p %p %s",
6206                         default_connecting_device,
6207                         default_device,
6208                         connman_device_get_string(default_device, "Name"));
6209
6210         if (default_connecting_device == default_device)
6211                 default_connecting_device = NULL;
6212 }
6213
6214 static void __connman_service_connect_default(struct connman_service *current)
6215 {
6216         int err;
6217         GList *list;
6218         bool default_internet;
6219         struct connman_service *service;
6220         struct connman_service *default_service = NULL;
6221         struct connman_device *default_device = NULL;
6222
6223         if (current->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
6224                 switch (current->state) {
6225                 case CONNMAN_SERVICE_STATE_UNKNOWN:
6226                 case CONNMAN_SERVICE_STATE_ASSOCIATION:
6227                 case CONNMAN_SERVICE_STATE_CONFIGURATION:
6228                         return;
6229                 default:
6230                         break;
6231                 }
6232
6233                 if (default_connecting_device &&
6234                                 __connman_service_is_internet_profile(current) == TRUE) {
6235                         if (current->network == NULL)
6236                                 return;
6237
6238                         default_device = connman_network_get_device(current->network);
6239                         if (default_connecting_device == default_device) {
6240                                 DBG("Cellular service[%s]  %p %s",
6241                                                 state2string(current->state), current, current->path);
6242                                 DBG("Cellular device %p %p %s",
6243                                                 default_connecting_device, default_device,
6244                                                 connman_device_get_string(default_device, "Name"));
6245
6246                                 default_connecting_device = NULL;
6247                         }
6248                 }
6249
6250                 return;
6251         } else if (is_connected(current) == TRUE || is_connecting(current) == TRUE)
6252                 return;
6253
6254         /* Always-on: keep default cellular connection as possible */
6255         for (list = service_list; list; list = list->next) {
6256                 service = list->data;
6257
6258                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR ||
6259                                 __connman_service_is_internet_profile(service) != TRUE ||
6260                                 service->network == NULL) {
6261                         continue;
6262                 }
6263
6264                 default_internet =
6265                                 connman_network_get_bool(service->network, "DefaultInternet");
6266
6267                 DBG("service: %p %s %s %s (default: %d)", service, service->name,
6268                                 __connman_service_type2string(service->type),
6269                                 state2string(service->state), default_internet);
6270
6271                 if (default_internet) {
6272                         default_service = service;
6273                         if (is_connected(default_service) == TRUE ||
6274                                         is_connecting(default_service) == TRUE)
6275                                 return;
6276
6277                         default_device = connman_network_get_device(default_service->network);
6278                         if (default_connecting_device == default_device) {
6279                                 DBG("Device is connecting (%p)", default_connecting_device);
6280                                 return;
6281                         }
6282
6283                         default_connecting_device = default_device;
6284                         default_service->connect_reason = CONNMAN_SERVICE_CONNECT_REASON_USER;
6285
6286                         err = __connman_network_connect(default_service->network);
6287                         DBG("Connecting default service %p %s [%d]",
6288                                         default_service, default_service->path, err);
6289                         DBG("Connecting device %p %s", default_connecting_device,
6290                                         connman_device_get_string(default_connecting_device, "Name"));
6291                         if (err < 0 && err != -EINPROGRESS) {
6292                                 default_connecting_device = NULL;
6293                         } else
6294                                 break;
6295                 }
6296         }
6297 }
6298 #endif
6299
6300 static void single_connected_tech(struct connman_service *allowed)
6301 {
6302         struct connman_service *service;
6303         GSList *services = NULL, *list;
6304         GList *iter;
6305
6306         DBG("keeping %p %s", allowed, allowed->path);
6307
6308 #if defined TIZEN_EXT
6309         if (!allowed || allowed->type == CONNMAN_SERVICE_TYPE_CELLULAR)
6310                 return;
6311 #endif
6312
6313         for (iter = service_list; iter; iter = iter->next) {
6314                 service = iter->data;
6315
6316 #if defined TIZEN_EXT
6317                 if (service != allowed && service->type != allowed->type &&
6318                                 __connman_service_can_drop(service) == TRUE)
6319 #else
6320                 if (!is_connected(service))
6321                         break;
6322
6323                 if (service == allowed)
6324                         continue;
6325 #endif
6326                 services = g_slist_prepend(services, service);
6327         }
6328
6329         for (list = services; list; list = list->next) {
6330                 service = list->data;
6331
6332                 DBG("disconnecting %p %s", service, service->path);
6333 #if defined TIZEN_EXT
6334                 __connman_service_disconnect_default(service);
6335 #endif
6336                 __connman_service_disconnect(service);
6337         }
6338
6339         g_slist_free(services);
6340 }
6341
6342 #if defined TIZEN_EXT
6343 static void set_priority_connected_service(void)
6344 {
6345         struct connman_service *service;
6346         GList *list;
6347
6348         for (list = service_list; list; list = list->next) {
6349                 service = list->data;
6350
6351                 if (is_connected(service) == FALSE)
6352                         service->order = 5;
6353                 else
6354                         service->order = 6;
6355         }
6356 }
6357 #endif
6358
6359 static const char *get_dbus_sender(struct connman_service *service)
6360 {
6361         if (!service->pending)
6362                 return NULL;
6363
6364         return dbus_message_get_sender(service->pending);
6365 }
6366
6367 static int service_indicate_state(struct connman_service *service)
6368 {
6369         enum connman_service_state old_state, new_state;
6370         struct connman_service *def_service;
6371         enum connman_ipconfig_method method;
6372         int result;
6373
6374         if (!service)
6375                 return -EINVAL;
6376
6377         old_state = service->state;
6378         new_state = combine_state(service->state_ipv4, service->state_ipv6);
6379
6380         DBG("service %p old %s - new %s/%s => %s",
6381                                         service,
6382                                         state2string(old_state),
6383                                         state2string(service->state_ipv4),
6384                                         state2string(service->state_ipv6),
6385                                         state2string(new_state));
6386
6387         if (old_state == new_state)
6388                 return -EALREADY;
6389
6390         def_service = __connman_service_get_default();
6391
6392         if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
6393                 result = service_update_preferred_order(def_service,
6394                                 service, new_state);
6395                 if (result == -EALREADY)
6396                         return result;
6397         }
6398
6399         if (old_state == CONNMAN_SERVICE_STATE_ONLINE)
6400                 __connman_notifier_leave_online(service->type);
6401
6402         if (is_connected_state(service, old_state) &&
6403                         !is_connected_state(service, new_state))
6404                 searchdomain_remove_all(service);
6405
6406         service->state = new_state;
6407         state_changed(service);
6408
6409         switch(new_state) {
6410         case CONNMAN_SERVICE_STATE_UNKNOWN:
6411
6412                 break;
6413
6414         case CONNMAN_SERVICE_STATE_IDLE:
6415                 if (old_state != CONNMAN_SERVICE_STATE_DISCONNECT)
6416                         __connman_service_disconnect(service);
6417
6418                 break;
6419
6420         case CONNMAN_SERVICE_STATE_ASSOCIATION:
6421
6422                 break;
6423
6424         case CONNMAN_SERVICE_STATE_CONFIGURATION:
6425                 if (!service->new_service &&
6426                                 __connman_stats_service_register(service) == 0) {
6427                         /*
6428                          * For new services the statistics are updated after
6429                          * we have successfully connected.
6430                          */
6431                         __connman_stats_get(service, false,
6432                                                 &service->stats.data);
6433                         __connman_stats_get(service, true,
6434                                                 &service->stats_roaming.data);
6435                 }
6436
6437                 break;
6438
6439         case CONNMAN_SERVICE_STATE_READY:
6440                 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
6441
6442                 if (service->new_service &&
6443                                 __connman_stats_service_register(service) == 0) {
6444                         /*
6445                          * This is normally done after configuring state
6446                          * but for new service do this after we have connected
6447                          * successfully.
6448                          */
6449                         __connman_stats_get(service, false,
6450                                                 &service->stats.data);
6451                         __connman_stats_get(service, true,
6452                                                 &service->stats_roaming.data);
6453                 }
6454
6455                 service->new_service = false;
6456
6457                 default_changed();
6458
6459                 def_service = __connman_service_get_default();
6460
6461                 service_update_preferred_order(def_service, service, new_state);
6462
6463                 __connman_service_set_favorite(service, true);
6464
6465                 reply_pending(service, 0);
6466
6467                 g_get_current_time(&service->modified);
6468                 service_save(service);
6469
6470                 searchdomain_add_all(service);
6471                 dns_changed(service);
6472                 domain_changed(service);
6473                 proxy_changed(service);
6474
6475                 if (old_state != CONNMAN_SERVICE_STATE_ONLINE)
6476                         __connman_notifier_connect(service->type);
6477
6478                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
6479                         connman_network_get_bool(service->network,
6480                                                 "WiFi.UseWPS")) {
6481                         const char *pass;
6482
6483                         pass = connman_network_get_string(service->network,
6484                                                         "WiFi.Passphrase");
6485
6486                         __connman_service_set_passphrase(service, pass);
6487
6488                         connman_network_set_bool(service->network,
6489                                                         "WiFi.UseWPS", false);
6490                 }
6491
6492                 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
6493                 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
6494                         __connman_ipconfig_disable_ipv6(
6495                                                 service->ipconfig_ipv6);
6496
6497                 if (connman_setting_get_bool("SingleConnectedTechnology"))
6498                         single_connected_tech(service);
6499                 else if (service->type != CONNMAN_SERVICE_TYPE_VPN)
6500                         vpn_auto_connect();
6501
6502 #if defined TIZEN_EXT
6503                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
6504                         set_priority_connected_service();
6505 #endif
6506
6507                 break;
6508
6509         case CONNMAN_SERVICE_STATE_ONLINE:
6510
6511                 break;
6512
6513         case CONNMAN_SERVICE_STATE_DISCONNECT:
6514                 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
6515
6516                 reply_pending(service, ECONNABORTED);
6517
6518                 def_service = __connman_service_get_default();
6519                 service->disconnect_reason = connman_network_get_disconnect_reason(service->network);
6520
6521                 if (!__connman_notifier_is_connected() &&
6522                         def_service &&
6523                                 def_service->provider)
6524                         connman_provider_disconnect(def_service->provider);
6525
6526                 default_changed();
6527
6528                 __connman_wispr_stop(service);
6529
6530                 __connman_wpad_stop(service);
6531
6532 #if defined TIZEN_EXT
6533                 /**
6534                   * Skip the functions if there is any connected profiles
6535                   * that use same interface
6536                   */
6537                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR ||
6538                         __connman_service_get_connected_count_of_iface(
6539                                                         service) <= 0) {
6540 #endif
6541                 dns_changed(service);
6542                 domain_changed(service);
6543                 proxy_changed(service);
6544 #if defined TIZEN_EXT
6545                 }
6546 #endif
6547
6548                 /*
6549                  * Previous services which are connected and which states
6550                  * are set to online should reset relevantly ipconfig_state
6551                  * to ready so wispr/portal will be rerun on those
6552                  */
6553                 downgrade_connected_services();
6554
6555                 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
6556                 break;
6557
6558         case CONNMAN_SERVICE_STATE_FAILURE:
6559 #if defined TIZEN_EXT
6560                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
6561                         service->order = 5;
6562 #endif
6563                 if (service->connect_reason == CONNMAN_SERVICE_CONNECT_REASON_USER &&
6564                         connman_agent_report_error(service, service->path,
6565                                         error2string(service->error),
6566                                         report_error_cb,
6567                                         get_dbus_sender(service),
6568                                         NULL) == -EINPROGRESS)
6569                         return 0;
6570                 service_complete(service);
6571
6572                 break;
6573         }
6574
6575         service_list_sort();
6576
6577 #if defined TIZEN_EXT
6578         __connman_service_connect_default(service);
6579 #endif
6580
6581         __connman_connection_update_gateway();
6582
6583         if ((old_state == CONNMAN_SERVICE_STATE_ONLINE &&
6584                         new_state != CONNMAN_SERVICE_STATE_READY) ||
6585                 (old_state == CONNMAN_SERVICE_STATE_READY &&
6586                         new_state != CONNMAN_SERVICE_STATE_ONLINE)) {
6587                 __connman_notifier_disconnect(service->type);
6588         }
6589
6590         if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
6591                 __connman_notifier_enter_online(service->type);
6592                 default_changed();
6593         }
6594
6595         if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
6596                 service->connect_reason == CONNMAN_SERVICE_CONNECT_REASON_USER &&
6597                 (new_state == CONNMAN_SERVICE_STATE_READY ||
6598                 new_state == CONNMAN_SERVICE_STATE_ONLINE)) {
6599                 if (service->user.favorite_user != service->user.current_user) {
6600                         DBG("Now set service favorite user id from %d to %d",
6601                         service->user.favorite_user, service->user.current_user);
6602
6603                         service->user.favorite_user = service->user.current_user;
6604
6605                         service_save(service);
6606                 }
6607         }
6608
6609         return 0;
6610 }
6611
6612 int __connman_service_indicate_error(struct connman_service *service,
6613                                         enum connman_service_error error)
6614 {
6615         DBG("service %p error %d", service, error);
6616
6617         if (!service)
6618                 return -EINVAL;
6619
6620         if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
6621                 return -EALREADY;
6622
6623         set_error(service, error);
6624
6625 /* default internet service: fix not cleared if pdp activation*/
6626 #if defined TIZEN_EXT
6627                 /*
6628                  * If connection failed for default service(DefaultInternet),
6629                  * default_connecting_device should be cleared.
6630                  */
6631                 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
6632                                 service->error == CONNMAN_SERVICE_ERROR_CONNECT_FAILED)
6633                         __connman_service_disconnect_default(service);
6634
6635                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
6636                                 service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY) {
6637                         g_free(service->passphrase);
6638                         service->passphrase = NULL;
6639                 }
6640 #endif
6641
6642         __connman_service_ipconfig_indicate_state(service,
6643                                                 CONNMAN_SERVICE_STATE_FAILURE,
6644                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
6645         __connman_service_ipconfig_indicate_state(service,
6646                                                 CONNMAN_SERVICE_STATE_FAILURE,
6647                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
6648         return 0;
6649 }
6650
6651 int __connman_service_clear_error(struct connman_service *service)
6652 {
6653         DBusMessage *pending, *provider_pending;
6654
6655         DBG("service %p", service);
6656
6657         if (!service)
6658                 return -EINVAL;
6659
6660         if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
6661                 return -EINVAL;
6662
6663         pending = service->pending;
6664         service->pending = NULL;
6665         provider_pending = service->provider_pending;
6666         service->provider_pending = NULL;
6667
6668         __connman_service_ipconfig_indicate_state(service,
6669                                                 CONNMAN_SERVICE_STATE_IDLE,
6670                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
6671
6672         __connman_service_ipconfig_indicate_state(service,
6673                                                 CONNMAN_SERVICE_STATE_IDLE,
6674                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
6675
6676         service->pending = pending;
6677         service->provider_pending = provider_pending;
6678
6679         return 0;
6680 }
6681
6682 int __connman_service_indicate_default(struct connman_service *service)
6683 {
6684         DBG("service %p state %s", service, state2string(service->state));
6685
6686         if (!is_connected(service)) {
6687                 /*
6688                  * If service is not yet fully connected, then we must not
6689                  * change the default yet. The default gw will be changed
6690                  * after the service state is in ready.
6691                  */
6692                 return -EINPROGRESS;
6693         }
6694
6695         default_changed();
6696
6697         return 0;
6698 }
6699
6700 enum connman_service_state __connman_service_ipconfig_get_state(
6701                                         struct connman_service *service,
6702                                         enum connman_ipconfig_type type)
6703 {
6704         if (!service)
6705                 return CONNMAN_SERVICE_STATE_UNKNOWN;
6706
6707         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
6708                 return service->state_ipv4;
6709
6710         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
6711                 return service->state_ipv6;
6712
6713         return CONNMAN_SERVICE_STATE_UNKNOWN;
6714 }
6715
6716 static void check_proxy_setup(struct connman_service *service)
6717 {
6718         /*
6719          * We start WPAD if we haven't got a PAC URL from DHCP and
6720          * if our proxy manual configuration is either empty or set
6721          * to AUTO with an empty URL.
6722          */
6723
6724         if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
6725                 goto done;
6726
6727         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
6728                 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
6729                         service->pac))
6730                 goto done;
6731
6732         if (__connman_wpad_start(service) < 0) {
6733                 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
6734                 __connman_notifier_proxy_changed(service);
6735                 goto done;
6736         }
6737
6738         return;
6739
6740 done:
6741         __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
6742 }
6743
6744 #if defined TIZEN_EXT
6745 void connman_check_proxy_setup_and_wispr_start(struct connman_service *service){
6746
6747         DBG("check the proxy and start wispr");
6748         check_proxy_setup(service);
6749         return;
6750 }
6751 #endif
6752
6753 /*
6754  * How many networks are connected at the same time. If more than 1,
6755  * then set the rp_filter setting properly (loose mode routing) so that network
6756  * connectivity works ok. This is only done for IPv4 networks as IPv6
6757  * does not have rp_filter knob.
6758  */
6759 static int connected_networks_count;
6760 static int original_rp_filter;
6761
6762 static void service_rp_filter(struct connman_service *service,
6763                                 bool connected)
6764 {
6765         enum connman_ipconfig_method method;
6766
6767         method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
6768
6769         switch (method) {
6770         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
6771         case CONNMAN_IPCONFIG_METHOD_OFF:
6772         case CONNMAN_IPCONFIG_METHOD_AUTO:
6773                 return;
6774         case CONNMAN_IPCONFIG_METHOD_FIXED:
6775         case CONNMAN_IPCONFIG_METHOD_MANUAL:
6776         case CONNMAN_IPCONFIG_METHOD_DHCP:
6777                 break;
6778         }
6779
6780         if (connected) {
6781                 if (connected_networks_count == 1) {
6782                         int filter_value;
6783                         filter_value = __connman_ipconfig_set_rp_filter();
6784                         if (filter_value < 0)
6785                                 return;
6786
6787                         original_rp_filter = filter_value;
6788                 }
6789                 connected_networks_count++;
6790
6791         } else {
6792                 if (connected_networks_count == 2)
6793                         __connman_ipconfig_unset_rp_filter(original_rp_filter);
6794
6795                 connected_networks_count--;
6796                 if (connected_networks_count < 0)
6797                         connected_networks_count = 0;
6798         }
6799
6800         DBG("%s %s ipconfig %p method %d count %d filter %d",
6801                 connected ? "connected" : "disconnected", service->identifier,
6802                 service->ipconfig_ipv4, method,
6803                 connected_networks_count, original_rp_filter);
6804 }
6805
6806 static gboolean redo_wispr(gpointer user_data)
6807 {
6808         struct connman_service *service = user_data;
6809         int refcount = service->refcount - 1;
6810
6811         connman_service_unref(service);
6812         if (refcount == 0) {
6813                 DBG("Service %p already removed", service);
6814                 return FALSE;
6815         }
6816
6817         __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
6818
6819         return FALSE;
6820 }
6821
6822 int __connman_service_online_check_failed(struct connman_service *service,
6823                                         enum connman_ipconfig_type type)
6824 {
6825         DBG("service %p type %d count %d", service, type,
6826                                                 service->online_check_count);
6827
6828         /* currently we only retry IPv6 stuff */
6829         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 ||
6830                         service->online_check_count != 1) {
6831                 connman_warn("Online check failed for %p %s", service,
6832                         service->name);
6833                 return 0;
6834         }
6835
6836         service->online_check_count = 0;
6837
6838         /*
6839          * We set the timeout to 1 sec so that we have a chance to get
6840          * necessary IPv6 router advertisement messages that might have
6841          * DNS data etc.
6842          */
6843         g_timeout_add_seconds(1, redo_wispr, connman_service_ref(service));
6844
6845         return EAGAIN;
6846 }
6847
6848 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
6849                                         enum connman_service_state new_state,
6850                                         enum connman_ipconfig_type type)
6851 {
6852         struct connman_ipconfig *ipconfig = NULL;
6853         enum connman_service_state old_state;
6854         enum connman_ipconfig_method method;
6855
6856         if (!service)
6857                 return -EINVAL;
6858
6859         switch (type) {
6860         case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
6861         case CONNMAN_IPCONFIG_TYPE_ALL:
6862                 return -EINVAL;
6863
6864         case CONNMAN_IPCONFIG_TYPE_IPV4:
6865                 old_state = service->state_ipv4;
6866                 ipconfig = service->ipconfig_ipv4;
6867
6868                 break;
6869
6870         case CONNMAN_IPCONFIG_TYPE_IPV6:
6871                 old_state = service->state_ipv6;
6872                 ipconfig = service->ipconfig_ipv6;
6873
6874                 break;
6875         }
6876
6877         if (!ipconfig)
6878                 return -EINVAL;
6879
6880         /* Any change? */
6881         if (old_state == new_state)
6882                 return -EALREADY;
6883
6884 #if defined TIZEN_EXT
6885         __sync_synchronize();
6886         if (service->user_pdn_connection_refcount > 0 &&
6887                         service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
6888                 if (new_state == CONNMAN_SERVICE_STATE_FAILURE ||
6889                                 new_state == CONNMAN_SERVICE_STATE_DISCONNECT ||
6890                                 new_state == CONNMAN_SERVICE_STATE_IDLE) {
6891                         service->user_pdn_connection_refcount = 0;
6892                         __sync_synchronize();
6893                 }
6894 #endif
6895
6896         DBG("service %p (%s) old state %d (%s) new state %d (%s) type %d (%s)",
6897                 service, service ? service->identifier : NULL,
6898                 old_state, state2string(old_state),
6899                 new_state, state2string(new_state),
6900                 type, __connman_ipconfig_type2string(type));
6901
6902         switch (new_state) {
6903         case CONNMAN_SERVICE_STATE_UNKNOWN:
6904         case CONNMAN_SERVICE_STATE_IDLE:
6905         case CONNMAN_SERVICE_STATE_ASSOCIATION:
6906                 break;
6907         case CONNMAN_SERVICE_STATE_CONFIGURATION:
6908                 __connman_ipconfig_enable(ipconfig);
6909                 break;
6910         case CONNMAN_SERVICE_STATE_READY:
6911 #if defined TIZEN_EXT
6912                 if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
6913                                 __connman_service_is_internet_profile(service) != TRUE) {
6914                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
6915                                 service_rp_filter(service, TRUE);
6916
6917                         break;
6918                 }
6919 #endif
6920                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
6921 #if !defined TIZEN_EXT
6922                         check_proxy_setup(service);
6923 #endif
6924                         service_rp_filter(service, true);
6925                 } else {
6926                         service->online_check_count = 1;
6927                         __connman_wispr_start(service, type);
6928                 }
6929                 break;
6930         case CONNMAN_SERVICE_STATE_ONLINE:
6931                 break;
6932         case CONNMAN_SERVICE_STATE_DISCONNECT:
6933                 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
6934                         return -EINVAL;
6935
6936                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
6937                         service_rp_filter(service, false);
6938
6939                 break;
6940         case CONNMAN_SERVICE_STATE_FAILURE:
6941                 break;
6942         }
6943
6944         /* Keep that state, but if the ipconfig method is OFF, then we set
6945            the state to IDLE so that it will not affect the combined state
6946            in the future.
6947          */
6948         method = __connman_ipconfig_get_method(ipconfig);
6949         switch (method) {
6950         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
6951         case CONNMAN_IPCONFIG_METHOD_OFF:
6952                 new_state = CONNMAN_SERVICE_STATE_IDLE;
6953                 break;
6954
6955         case CONNMAN_IPCONFIG_METHOD_FIXED:
6956         case CONNMAN_IPCONFIG_METHOD_MANUAL:
6957         case CONNMAN_IPCONFIG_METHOD_DHCP:
6958         case CONNMAN_IPCONFIG_METHOD_AUTO:
6959                 break;
6960
6961         }
6962
6963         if (is_connected_state(service, old_state) &&
6964                         !is_connected_state(service, new_state))
6965                 nameserver_remove_all(service);
6966
6967         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
6968                 service->state_ipv4 = new_state;
6969         else
6970                 service->state_ipv6 = new_state;
6971
6972         if (!is_connected_state(service, old_state) &&
6973                         is_connected_state(service, new_state))
6974                 nameserver_add_all(service);
6975
6976 #if defined TIZEN_EXT
6977         int ret = service_indicate_state(service);
6978         /*Sent the Ready changed signal again in case IPv4 IP set
6979           after IPv6 IP set*/
6980
6981         if(ret == -EALREADY && type == CONNMAN_IPCONFIG_TYPE_IPV4
6982                         && new_state == CONNMAN_SERVICE_STATE_READY) {
6983                 DBG("Notify IPv4 state new/old %d/%d", new_state,old_state);
6984                 state_changed(service);
6985         }
6986
6987         return ret;
6988 #endif
6989         return service_indicate_state(service);
6990 }
6991
6992 static bool prepare_network(struct connman_service *service)
6993 {
6994         enum connman_network_type type;
6995         unsigned int ssid_len;
6996
6997         type = connman_network_get_type(service->network);
6998
6999         switch (type) {
7000         case CONNMAN_NETWORK_TYPE_UNKNOWN:
7001         case CONNMAN_NETWORK_TYPE_VENDOR:
7002                 return false;
7003         case CONNMAN_NETWORK_TYPE_WIFI:
7004                 if (!connman_network_get_blob(service->network, "WiFi.SSID",
7005                                                 &ssid_len))
7006                         return false;
7007
7008                 if (service->passphrase)
7009                         connman_network_set_string(service->network,
7010                                 "WiFi.Passphrase", service->passphrase);
7011                 break;
7012         case CONNMAN_NETWORK_TYPE_ETHERNET:
7013         case CONNMAN_NETWORK_TYPE_GADGET:
7014         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
7015         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
7016         case CONNMAN_NETWORK_TYPE_CELLULAR:
7017                 break;
7018         }
7019
7020         return true;
7021 }
7022
7023 static void prepare_8021x(struct connman_service *service)
7024 {
7025         if (service->eap)
7026                 connman_network_set_string(service->network, "WiFi.EAP",
7027                                                                 service->eap);
7028
7029         if (service->identity)
7030                 connman_network_set_string(service->network, "WiFi.Identity",
7031                                                         service->identity);
7032
7033         if (service->ca_cert_file)
7034                 connman_network_set_string(service->network, "WiFi.CACertFile",
7035                                                         service->ca_cert_file);
7036
7037         if (service->client_cert_file)
7038                 connman_network_set_string(service->network,
7039                                                 "WiFi.ClientCertFile",
7040                                                 service->client_cert_file);
7041
7042         if (service->private_key_file)
7043                 connman_network_set_string(service->network,
7044                                                 "WiFi.PrivateKeyFile",
7045                                                 service->private_key_file);
7046
7047         if (service->private_key_passphrase)
7048                 connman_network_set_string(service->network,
7049                                         "WiFi.PrivateKeyPassphrase",
7050                                         service->private_key_passphrase);
7051
7052         if (service->phase2)
7053                 connman_network_set_string(service->network, "WiFi.Phase2",
7054                                                         service->phase2);
7055 }
7056
7057 static int service_connect(struct connman_service *service)
7058 {
7059         int err;
7060
7061         if (service->hidden)
7062                 return -EPERM;
7063
7064         switch (service->type) {
7065         case CONNMAN_SERVICE_TYPE_UNKNOWN:
7066         case CONNMAN_SERVICE_TYPE_SYSTEM:
7067         case CONNMAN_SERVICE_TYPE_GPS:
7068         case CONNMAN_SERVICE_TYPE_P2P:
7069                 return -EINVAL;
7070         case CONNMAN_SERVICE_TYPE_ETHERNET:
7071         case CONNMAN_SERVICE_TYPE_GADGET:
7072         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
7073         case CONNMAN_SERVICE_TYPE_CELLULAR:
7074         case CONNMAN_SERVICE_TYPE_VPN:
7075                 break;
7076         case CONNMAN_SERVICE_TYPE_WIFI:
7077                 switch (service->security) {
7078                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
7079                 case CONNMAN_SERVICE_SECURITY_NONE:
7080                         break;
7081                 case CONNMAN_SERVICE_SECURITY_WEP:
7082                 case CONNMAN_SERVICE_SECURITY_PSK:
7083                 case CONNMAN_SERVICE_SECURITY_WPA:
7084                 case CONNMAN_SERVICE_SECURITY_RSN:
7085                         if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
7086                                 return -ENOKEY;
7087
7088                         if (service->request_passphrase_input) {
7089                                 DBG("Now try to connect other user's favorite service");
7090                                 service->request_passphrase_input = false;
7091                                 return -ENOKEY;
7092                         } else if (!service->passphrase) {
7093                                 if (!service->network)
7094                                         return -EOPNOTSUPP;
7095
7096                                 if (!service->wps ||
7097                                         !connman_network_get_bool(service->network, "WiFi.UseWPS"))
7098                                         return -ENOKEY;
7099                         }
7100                         break;
7101
7102                 case CONNMAN_SERVICE_SECURITY_8021X:
7103                         if (!service->eap)
7104                                 return -EINVAL;
7105
7106 #if defined TIZEN_EXT
7107                         /*
7108                          * never request credentials if using EAP-TLS, EAP-SIM
7109                          * or EAP-AKA (EAP-TLS, EAP-SIM and EAP-AKA networks
7110                          * need to be fully provisioned)
7111                          */
7112                         if (g_str_equal(service->eap, "tls") ||
7113                                 g_str_equal(service->eap, "sim") ||
7114                                 g_str_equal(service->eap, "aka"))
7115                                 break;
7116 #else
7117                         /*
7118                          * never request credentials if using EAP-TLS
7119                          * (EAP-TLS networks need to be fully provisioned)
7120                          */
7121                         if (g_str_equal(service->eap, "tls"))
7122                                 break;
7123 #endif
7124                         /*
7125                          * Return -ENOKEY if either identity or passphrase is
7126                          * missing. Agent provided credentials can be used as
7127                          * fallback if needed.
7128                          */
7129                         if (((!service->identity &&
7130                                         !service->agent_identity) ||
7131                                         !service->passphrase) ||
7132                                         service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
7133                                 return -ENOKEY;
7134
7135                         break;
7136                 }
7137                 break;
7138         }
7139
7140         if (service->network) {
7141                 if (!prepare_network(service))
7142                         return -EINVAL;
7143
7144                 switch (service->security) {
7145                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
7146                 case CONNMAN_SERVICE_SECURITY_NONE:
7147                 case CONNMAN_SERVICE_SECURITY_WEP:
7148                 case CONNMAN_SERVICE_SECURITY_PSK:
7149                 case CONNMAN_SERVICE_SECURITY_WPA:
7150                 case CONNMAN_SERVICE_SECURITY_RSN:
7151                         break;
7152                 case CONNMAN_SERVICE_SECURITY_8021X:
7153                         prepare_8021x(service);
7154                         break;
7155                 }
7156
7157                 if (__connman_stats_service_register(service) == 0) {
7158                         __connman_stats_get(service, false,
7159                                                 &service->stats.data);
7160                         __connman_stats_get(service, true,
7161                                                 &service->stats_roaming.data);
7162                 }
7163
7164                 if (service->ipconfig_ipv4)
7165                         __connman_ipconfig_enable(service->ipconfig_ipv4);
7166                 if (service->ipconfig_ipv6)
7167                         __connman_ipconfig_enable(service->ipconfig_ipv6);
7168
7169                 err = __connman_network_connect(service->network);
7170         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
7171                                         service->provider)
7172                 err = __connman_provider_connect(service->provider);
7173         else
7174                 return -EOPNOTSUPP;
7175
7176         if (err < 0) {
7177                 if (err != -EINPROGRESS) {
7178                         __connman_ipconfig_disable(service->ipconfig_ipv4);
7179                         __connman_ipconfig_disable(service->ipconfig_ipv6);
7180                         __connman_stats_service_unregister(service);
7181                 }
7182         }
7183
7184         return err;
7185 }
7186
7187 int __connman_service_connect(struct connman_service *service,
7188                         enum connman_service_connect_reason reason)
7189 {
7190         int err;
7191
7192         DBG("service %p state %s connect reason %s -> %s",
7193                 service, state2string(service->state),
7194                 reason2string(service->connect_reason),
7195                 reason2string(reason));
7196
7197         if (is_connected(service))
7198                 return -EISCONN;
7199
7200         if (is_connecting(service))
7201                 return -EALREADY;
7202
7203         switch (service->type) {
7204         case CONNMAN_SERVICE_TYPE_UNKNOWN:
7205         case CONNMAN_SERVICE_TYPE_SYSTEM:
7206         case CONNMAN_SERVICE_TYPE_GPS:
7207         case CONNMAN_SERVICE_TYPE_P2P:
7208                 return -EINVAL;
7209
7210         case CONNMAN_SERVICE_TYPE_ETHERNET:
7211         case CONNMAN_SERVICE_TYPE_GADGET:
7212         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
7213         case CONNMAN_SERVICE_TYPE_CELLULAR:
7214         case CONNMAN_SERVICE_TYPE_VPN:
7215         case CONNMAN_SERVICE_TYPE_WIFI:
7216                 break;
7217         }
7218
7219         if (!is_ipconfig_usable(service))
7220                 return -ENOLINK;
7221
7222         __connman_service_clear_error(service);
7223
7224         err = service_connect(service);
7225
7226         service->connect_reason = reason;
7227         if (err >= 0)
7228                 return 0;
7229
7230         if (err == -EINPROGRESS) {
7231                 if (service->timeout == 0)
7232                         service->timeout = g_timeout_add_seconds(
7233                                 CONNECT_TIMEOUT, connect_timeout, service);
7234
7235                 return -EINPROGRESS;
7236         }
7237
7238         if (service->network)
7239                 __connman_network_disconnect(service->network);
7240         else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
7241                                 service->provider)
7242                         connman_provider_disconnect(service->provider);
7243
7244         if (service->connect_reason == CONNMAN_SERVICE_CONNECT_REASON_USER) {
7245                 if (err == -ENOKEY || err == -EPERM) {
7246                         DBusMessage *pending = NULL;
7247
7248                         /*
7249                          * We steal the reply here. The idea is that the
7250                          * connecting client will see the connection status
7251                          * after the real hidden network is connected or
7252                          * connection failed.
7253                          */
7254                         if (service->hidden) {
7255                                 pending = service->pending;
7256                                 service->pending = NULL;
7257                         }
7258
7259                         err = __connman_agent_request_passphrase_input(service,
7260                                         request_input_cb,
7261                                         get_dbus_sender(service),
7262                                         pending);
7263                         if (service->hidden && err != -EINPROGRESS)
7264                                 service->pending = pending;
7265
7266                         return err;
7267                 }
7268                 reply_pending(service, -err);
7269         }
7270
7271         return err;
7272 }
7273
7274 int __connman_service_disconnect(struct connman_service *service)
7275 {
7276         int err;
7277
7278         DBG("service %p", service);
7279
7280         service->connect_reason = CONNMAN_SERVICE_CONNECT_REASON_NONE;
7281         service->proxy = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
7282
7283         connman_agent_cancel(service);
7284
7285         reply_pending(service, ECONNABORTED);
7286
7287         if (service->network) {
7288                 err = __connman_network_disconnect(service->network);
7289         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
7290                                         service->provider)
7291                 err = connman_provider_disconnect(service->provider);
7292         else
7293                 return -EOPNOTSUPP;
7294
7295         if (err < 0 && err != -EINPROGRESS)
7296                 return err;
7297
7298         __connman_6to4_remove(service->ipconfig_ipv4);
7299
7300         if (service->ipconfig_ipv4)
7301                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
7302                                                         NULL);
7303         else
7304                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
7305                                                         NULL);
7306
7307 #if defined TIZEN_EXT
7308         /**
7309           * Skip the functions If there is any connected profiles
7310           * that use same interface
7311           */
7312         if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR ||
7313                 __connman_service_get_connected_count_of_iface(service) <= 0) {
7314 #endif
7315         __connman_ipconfig_address_remove(service->ipconfig_ipv4);
7316         settings_changed(service, service->ipconfig_ipv4);
7317
7318         __connman_ipconfig_address_remove(service->ipconfig_ipv6);
7319         settings_changed(service, service->ipconfig_ipv6);
7320
7321         __connman_ipconfig_disable(service->ipconfig_ipv4);
7322         __connman_ipconfig_disable(service->ipconfig_ipv6);
7323 #if defined TIZEN_EXT
7324         }
7325 #endif
7326
7327         __connman_stats_service_unregister(service);
7328
7329         return err;
7330 }
7331
7332 int __connman_service_disconnect_all(void)
7333 {
7334         struct connman_service *service;
7335         GSList *services = NULL, *list;
7336         GList *iter;
7337
7338         DBG("");
7339
7340         for (iter = service_list; iter; iter = iter->next) {
7341                 service = iter->data;
7342
7343                 if (!is_connected(service))
7344                         break;
7345
7346                 services = g_slist_prepend(services, service);
7347         }
7348
7349         for (list = services; list; list = list->next) {
7350                 struct connman_service *service = list->data;
7351
7352                 service->ignore = true;
7353
7354                 __connman_service_disconnect(service);
7355         }
7356
7357         g_slist_free(services);
7358
7359         return 0;
7360 }
7361
7362 /**
7363  * lookup_by_identifier:
7364  * @identifier: service identifier
7365  *
7366  * Look up a service by identifier (reference count will not be increased)
7367  */
7368 static struct connman_service *lookup_by_identifier(const char *identifier)
7369 {
7370         return g_hash_table_lookup(service_hash, identifier);
7371 }
7372
7373 struct provision_user_data {
7374         const char *ident;
7375         int ret;
7376 };
7377
7378 static void provision_changed(gpointer value, gpointer user_data)
7379 {
7380         struct connman_service *service = value;
7381         struct provision_user_data *data = user_data;
7382         const char *path = data->ident;
7383         int ret;
7384
7385         ret = __connman_config_provision_service_ident(service, path,
7386                         service->config_file, service->config_entry);
7387         if (ret > 0)
7388                 data->ret = ret;
7389 }
7390
7391 int __connman_service_provision_changed(const char *ident)
7392 {
7393         struct provision_user_data data = {
7394                 .ident = ident,
7395                 .ret = 0
7396         };
7397
7398         g_list_foreach(service_list, provision_changed, (void *)&data);
7399
7400         /*
7401          * Because the provision_changed() might have set some services
7402          * as favorite, we must sort the sequence now.
7403          */
7404         if (services_dirty) {
7405                 services_dirty = false;
7406
7407                 service_list_sort();
7408
7409                 __connman_connection_update_gateway();
7410         }
7411
7412         return data.ret;
7413 }
7414
7415 void __connman_service_set_config(struct connman_service *service,
7416                                 const char *file_id, const char *entry)
7417 {
7418         if (!service)
7419                 return;
7420
7421         g_free(service->config_file);
7422         service->config_file = g_strdup(file_id);
7423
7424         g_free(service->config_entry);
7425         service->config_entry = g_strdup(entry);
7426 }
7427
7428 /**
7429  * __connman_service_get:
7430  * @identifier: service identifier
7431  *
7432  * Look up a service by identifier or create a new one if not found
7433  */
7434 static struct connman_service *service_get(const char *identifier)
7435 {
7436         struct connman_service *service;
7437
7438         service = g_hash_table_lookup(service_hash, identifier);
7439         if (service) {
7440                 connman_service_ref(service);
7441                 return service;
7442         }
7443
7444         service = connman_service_create();
7445         if (!service)
7446                 return NULL;
7447
7448         DBG("service %p", service);
7449
7450         service->identifier = g_strdup(identifier);
7451
7452         service_list = g_list_insert_sorted(service_list, service,
7453                                                 service_compare);
7454
7455         g_hash_table_insert(service_hash, service->identifier, service);
7456
7457         return service;
7458 }
7459
7460 static int service_register(struct connman_service *service)
7461 {
7462         DBG("service %p", service);
7463
7464         if (service->path)
7465                 return -EALREADY;
7466
7467         service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
7468                                                 service->identifier);
7469
7470         DBG("path %s", service->path);
7471
7472         if (__connman_config_provision_service(service) < 0)
7473                 service_load(service);
7474
7475         g_dbus_register_interface(connection, service->path,
7476                                         CONNMAN_SERVICE_INTERFACE,
7477                                         service_methods, service_signals,
7478                                                         NULL, service, NULL);
7479
7480         service_list_sort();
7481
7482         __connman_connection_update_gateway();
7483
7484         return 0;
7485 }
7486
7487 static void service_up(struct connman_ipconfig *ipconfig,
7488                 const char *ifname)
7489 {
7490         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7491
7492         DBG("%s up", ifname);
7493
7494         link_changed(service);
7495
7496         service->stats.valid = false;
7497         service->stats_roaming.valid = false;
7498 }
7499
7500 static void service_down(struct connman_ipconfig *ipconfig,
7501                         const char *ifname)
7502 {
7503         DBG("%s down", ifname);
7504 }
7505
7506 static void service_lower_up(struct connman_ipconfig *ipconfig,
7507                         const char *ifname)
7508 {
7509         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7510
7511         DBG("%s lower up", ifname);
7512
7513         stats_start(service);
7514 }
7515
7516 static void service_lower_down(struct connman_ipconfig *ipconfig,
7517                         const char *ifname)
7518 {
7519         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7520
7521         DBG("%s lower down", ifname);
7522
7523         if (!is_idle_state(service, service->state_ipv4))
7524                 __connman_ipconfig_disable(service->ipconfig_ipv4);
7525
7526         if (!is_idle_state(service, service->state_ipv6))
7527                 __connman_ipconfig_disable(service->ipconfig_ipv6);
7528
7529         stats_stop(service);
7530         service_save(service);
7531 }
7532
7533 static void service_ip_bound(struct connman_ipconfig *ipconfig,
7534                         const char *ifname)
7535 {
7536         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7537         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
7538         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
7539
7540         DBG("%s ip bound", ifname);
7541
7542         type = __connman_ipconfig_get_config_type(ipconfig);
7543         method = __connman_ipconfig_get_method(ipconfig);
7544
7545         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
7546                                                         type, method);
7547
7548         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
7549                         method == CONNMAN_IPCONFIG_METHOD_AUTO)
7550                 __connman_service_ipconfig_indicate_state(service,
7551                                                 CONNMAN_SERVICE_STATE_READY,
7552                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
7553
7554         settings_changed(service, ipconfig);
7555 }
7556
7557 static void service_ip_release(struct connman_ipconfig *ipconfig,
7558                         const char *ifname)
7559 {
7560         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7561         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
7562         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
7563
7564         DBG("%s ip release", ifname);
7565
7566         type = __connman_ipconfig_get_config_type(ipconfig);
7567         method = __connman_ipconfig_get_method(ipconfig);
7568
7569         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
7570                                                         type, method);
7571
7572         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
7573                         method == CONNMAN_IPCONFIG_METHOD_OFF)
7574                 __connman_service_ipconfig_indicate_state(service,
7575                                         CONNMAN_SERVICE_STATE_DISCONNECT,
7576                                         CONNMAN_IPCONFIG_TYPE_IPV6);
7577
7578         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
7579                         method == CONNMAN_IPCONFIG_METHOD_OFF)
7580                 __connman_service_ipconfig_indicate_state(service,
7581                                         CONNMAN_SERVICE_STATE_DISCONNECT,
7582                                         CONNMAN_IPCONFIG_TYPE_IPV4);
7583
7584         settings_changed(service, ipconfig);
7585 }
7586
7587 static void service_route_changed(struct connman_ipconfig *ipconfig,
7588                                 const char *ifname)
7589 {
7590         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
7591
7592         DBG("%s route changed", ifname);
7593
7594         settings_changed(service, ipconfig);
7595 }
7596
7597 static const struct connman_ipconfig_ops service_ops = {
7598         .up             = service_up,
7599         .down           = service_down,
7600         .lower_up       = service_lower_up,
7601         .lower_down     = service_lower_down,
7602         .ip_bound       = service_ip_bound,
7603         .ip_release     = service_ip_release,
7604         .route_set      = service_route_changed,
7605         .route_unset    = service_route_changed,
7606 };
7607
7608 static struct connman_ipconfig *create_ip4config(struct connman_service *service,
7609                 int index, enum connman_ipconfig_method method)
7610 {
7611         struct connman_ipconfig *ipconfig_ipv4;
7612
7613         ipconfig_ipv4 = __connman_ipconfig_create(index,
7614                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
7615         if (!ipconfig_ipv4)
7616                 return NULL;
7617
7618         __connman_ipconfig_set_method(ipconfig_ipv4, method);
7619
7620         __connman_ipconfig_set_data(ipconfig_ipv4, service);
7621
7622         __connman_ipconfig_set_ops(ipconfig_ipv4, &service_ops);
7623
7624         return ipconfig_ipv4;
7625 }
7626
7627 static struct connman_ipconfig *create_ip6config(struct connman_service *service,
7628                 int index)
7629 {
7630         struct connman_ipconfig *ipconfig_ipv6;
7631
7632         ipconfig_ipv6 = __connman_ipconfig_create(index,
7633                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
7634         if (!ipconfig_ipv6)
7635                 return NULL;
7636
7637         __connman_ipconfig_set_data(ipconfig_ipv6, service);
7638
7639         __connman_ipconfig_set_ops(ipconfig_ipv6, &service_ops);
7640
7641         return ipconfig_ipv6;
7642 }
7643
7644 void __connman_service_read_ip4config(struct connman_service *service)
7645 {
7646         GKeyFile *keyfile;
7647
7648         if (!service->ipconfig_ipv4)
7649                 return;
7650
7651         keyfile = connman_storage_load_service(service->identifier);
7652         if (!keyfile)
7653                 return;
7654
7655         __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
7656                                 service->identifier, "IPv4.");
7657
7658         g_key_file_free(keyfile);
7659 }
7660
7661 void connman_service_create_ip4config(struct connman_service *service,
7662                                         int index)
7663 {
7664         DBG("ipv4 %p", service->ipconfig_ipv4);
7665
7666         if (service->ipconfig_ipv4)
7667                 return;
7668
7669         service->ipconfig_ipv4 = create_ip4config(service, index,
7670                         CONNMAN_IPCONFIG_METHOD_DHCP);
7671         __connman_service_read_ip4config(service);
7672 }
7673
7674 void __connman_service_read_ip6config(struct connman_service *service)
7675 {
7676         GKeyFile *keyfile;
7677
7678         if (!service->ipconfig_ipv6)
7679                 return;
7680
7681         keyfile = connman_storage_load_service(service->identifier);
7682         if (!keyfile)
7683                 return;
7684
7685         __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
7686                                 service->identifier, "IPv6.");
7687
7688         g_key_file_free(keyfile);
7689 }
7690
7691 void connman_service_create_ip6config(struct connman_service *service,
7692                                                                 int index)
7693 {
7694         DBG("ipv6 %p", service->ipconfig_ipv6);
7695
7696         if (service->ipconfig_ipv6)
7697                 return;
7698
7699         service->ipconfig_ipv6 = create_ip6config(service, index);
7700
7701         __connman_service_read_ip6config(service);
7702 }
7703
7704 /**
7705  * connman_service_lookup_from_network:
7706  * @network: network structure
7707  *
7708  * Look up a service by network (reference count will not be increased)
7709  */
7710 struct connman_service *connman_service_lookup_from_network(struct connman_network *network)
7711 {
7712         struct connman_service *service;
7713         const char *ident, *group;
7714         char *name;
7715
7716         if (!network)
7717                 return NULL;
7718
7719         ident = __connman_network_get_ident(network);
7720         if (!ident)
7721                 return NULL;
7722
7723         group = connman_network_get_group(network);
7724         if (!group)
7725                 return NULL;
7726
7727         name = g_strdup_printf("%s_%s_%s",
7728                         __connman_network_get_type(network), ident, group);
7729         service = lookup_by_identifier(name);
7730         g_free(name);
7731
7732         return service;
7733 }
7734
7735 struct connman_service *__connman_service_lookup_from_index(int index)
7736 {
7737         struct connman_service *service;
7738         GList *list;
7739
7740         for (list = service_list; list; list = list->next) {
7741                 service = list->data;
7742
7743                 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
7744                                                         == index)
7745                         return service;
7746
7747                 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
7748                                                         == index)
7749                         return service;
7750         }
7751
7752         return NULL;
7753 }
7754
7755 struct connman_service *__connman_service_lookup_from_ident(const char *identifier)
7756 {
7757         return lookup_by_identifier(identifier);
7758 }
7759
7760 const char *__connman_service_get_ident(struct connman_service *service)
7761 {
7762         return service->identifier;
7763 }
7764
7765 const char *__connman_service_get_path(struct connman_service *service)
7766 {
7767         return service->path;
7768 }
7769
7770 const char *__connman_service_get_name(struct connman_service *service)
7771 {
7772         return service->name;
7773 }
7774
7775 enum connman_service_state __connman_service_get_state(struct connman_service *service)
7776 {
7777         return service->state;
7778 }
7779
7780 unsigned int __connman_service_get_order(struct connman_service *service)
7781 {
7782         unsigned int order = 0;
7783
7784         if (!service)
7785                 return 0;
7786
7787         service->order = 0;
7788
7789         if (!service->favorite)
7790                 return 0;
7791
7792 #if defined TIZEN_EXT
7793         if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
7794                         service->do_split_routing == FALSE)
7795                 order = 10;
7796         else if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
7797                 if (service->order < 5)
7798                         order = 5;
7799         } else if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
7800                 order = 4;
7801         else if (service->type == CONNMAN_SERVICE_TYPE_BLUETOOTH)
7802                 order = 3;
7803         else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
7804                         __connman_service_is_internet_profile(service) == TRUE)
7805                 order = 1;
7806         else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
7807                         __connman_service_is_tethering_profile(service) == TRUE)
7808                 order = 0;
7809         else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
7810                 order = 0;
7811         else
7812                 order = 2;
7813 #else
7814         if (service == service_list->data)
7815                 order = 1;
7816
7817         if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
7818                         !service->do_split_routing) {
7819                 service->order = 10;
7820                 order = 10;
7821         }
7822 #endif
7823         DBG("service %p name %s order %d split %d", service, service->name,
7824                 order, service->do_split_routing);
7825
7826         return order;
7827 }
7828
7829 void __connman_service_update_ordering(void)
7830 {
7831         if (service_list && service_list->next)
7832                 service_list = g_list_sort(service_list, service_compare);
7833 }
7834
7835 static enum connman_service_type convert_network_type(struct connman_network *network)
7836 {
7837         enum connman_network_type type = connman_network_get_type(network);
7838
7839         switch (type) {
7840         case CONNMAN_NETWORK_TYPE_UNKNOWN:
7841         case CONNMAN_NETWORK_TYPE_VENDOR:
7842                 break;
7843         case CONNMAN_NETWORK_TYPE_ETHERNET:
7844                 return CONNMAN_SERVICE_TYPE_ETHERNET;
7845         case CONNMAN_NETWORK_TYPE_WIFI:
7846                 return CONNMAN_SERVICE_TYPE_WIFI;
7847         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
7848         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
7849                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
7850         case CONNMAN_NETWORK_TYPE_CELLULAR:
7851                 return CONNMAN_SERVICE_TYPE_CELLULAR;
7852         case CONNMAN_NETWORK_TYPE_GADGET:
7853                 return CONNMAN_SERVICE_TYPE_GADGET;
7854         }
7855
7856         return CONNMAN_SERVICE_TYPE_UNKNOWN;
7857 }
7858
7859 static enum connman_service_security convert_wifi_security(const char *security)
7860 {
7861         if (!security)
7862                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
7863         else if (g_str_equal(security, "none"))
7864                 return CONNMAN_SERVICE_SECURITY_NONE;
7865         else if (g_str_equal(security, "wep"))
7866                 return CONNMAN_SERVICE_SECURITY_WEP;
7867         else if (g_str_equal(security, "psk"))
7868                 return CONNMAN_SERVICE_SECURITY_PSK;
7869         else if (g_str_equal(security, "ieee8021x"))
7870                 return CONNMAN_SERVICE_SECURITY_8021X;
7871         else if (g_str_equal(security, "wpa"))
7872                 return CONNMAN_SERVICE_SECURITY_WPA;
7873         else if (g_str_equal(security, "rsn"))
7874                 return CONNMAN_SERVICE_SECURITY_RSN;
7875 #if defined TIZEN_EXT
7876         else if (g_str_equal(security, "ft_psk") == TRUE)
7877                 return CONNMAN_SERVICE_SECURITY_PSK;
7878         else if (g_str_equal(security, "ft_ieee8021x") == TRUE)
7879                 return CONNMAN_SERVICE_SECURITY_8021X;
7880 #endif
7881         else
7882                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
7883 }
7884
7885 static void update_from_network(struct connman_service *service,
7886                                         struct connman_network *network)
7887 {
7888         uint8_t strength = service->strength;
7889         const char *str;
7890
7891         DBG("service %p network %p", service, network);
7892
7893         if (is_connected(service))
7894                 return;
7895
7896         if (is_connecting(service))
7897                 return;
7898
7899         str = connman_network_get_string(network, "Name");
7900         if (str) {
7901                 g_free(service->name);
7902                 service->name = g_strdup(str);
7903                 service->hidden = false;
7904         } else {
7905                 g_free(service->name);
7906                 service->name = NULL;
7907                 service->hidden = true;
7908         }
7909
7910         service->strength = connman_network_get_strength(network);
7911         service->roaming = connman_network_get_bool(network, "Roaming");
7912
7913         if (service->strength == 0) {
7914                 /*
7915                  * Filter out 0-values; it's unclear what they mean
7916                  * and they cause anomalous sorting of the priority list.
7917                  */
7918                 service->strength = strength;
7919         }
7920
7921         str = connman_network_get_string(network, "WiFi.Security");
7922         service->security = convert_wifi_security(str);
7923
7924         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
7925                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
7926
7927         if (service->strength > strength && service->network) {
7928                 connman_network_unref(service->network);
7929                 service->network = connman_network_ref(network);
7930
7931                 strength_changed(service);
7932         }
7933
7934         if (!service->network)
7935                 service->network = connman_network_ref(network);
7936
7937         service_list_sort();
7938 }
7939
7940 /**
7941  * __connman_service_create_from_network:
7942  * @network: network structure
7943  *
7944  * Look up service by network and if not found, create one
7945  */
7946 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
7947 {
7948         struct connman_service *service;
7949         struct connman_device *device;
7950         const char *ident, *group;
7951         char *name;
7952         unsigned int *auto_connect_types;
7953         int i, index;
7954
7955         DBG("network %p", network);
7956
7957         if (!network)
7958                 return NULL;
7959
7960         ident = __connman_network_get_ident(network);
7961         if (!ident)
7962                 return NULL;
7963
7964         group = connman_network_get_group(network);
7965         if (!group)
7966                 return NULL;
7967
7968         name = g_strdup_printf("%s_%s_%s",
7969                         __connman_network_get_type(network), ident, group);
7970         service = service_get(name);
7971         g_free(name);
7972
7973         if (!service)
7974                 return NULL;
7975
7976         if (__connman_network_get_weakness(network))
7977                 return service;
7978
7979         if (service->path) {
7980                 update_from_network(service, network);
7981                 __connman_connection_update_gateway();
7982                 return service;
7983         }
7984
7985         service->type = convert_network_type(network);
7986
7987         auto_connect_types = connman_setting_get_uint_list("DefaultAutoConnectTechnologies");
7988         service->autoconnect = false;
7989         for (i = 0; auto_connect_types &&
7990                      auto_connect_types[i] != 0; i++) {
7991                 if (service->type == auto_connect_types[i]) {
7992                         service->autoconnect = true;
7993                         break;
7994                 }
7995         }
7996
7997         switch (service->type) {
7998         case CONNMAN_SERVICE_TYPE_UNKNOWN:
7999         case CONNMAN_SERVICE_TYPE_SYSTEM:
8000         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
8001         case CONNMAN_SERVICE_TYPE_GPS:
8002         case CONNMAN_SERVICE_TYPE_VPN:
8003         case CONNMAN_SERVICE_TYPE_GADGET:
8004         case CONNMAN_SERVICE_TYPE_WIFI:
8005         case CONNMAN_SERVICE_TYPE_CELLULAR:
8006         case CONNMAN_SERVICE_TYPE_P2P:
8007                 break;
8008         case CONNMAN_SERVICE_TYPE_ETHERNET:
8009                 service->favorite = true;
8010                 break;
8011         }
8012
8013         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
8014         service->state = combine_state(service->state_ipv4, service->state_ipv6);
8015
8016         update_from_network(service, network);
8017
8018         index = connman_network_get_index(network);
8019
8020         if (!service->ipconfig_ipv4)
8021                 service->ipconfig_ipv4 = create_ip4config(service, index,
8022                                 CONNMAN_IPCONFIG_METHOD_DHCP);
8023
8024         if (!service->ipconfig_ipv6)
8025                 service->ipconfig_ipv6 = create_ip6config(service, index);
8026
8027         service_register(service);
8028
8029         if (service->favorite) {
8030                 device = connman_network_get_device(service->network);
8031                 if (device && !connman_device_get_scanning(device)) {
8032
8033                         switch (service->type) {
8034                         case CONNMAN_SERVICE_TYPE_UNKNOWN:
8035                         case CONNMAN_SERVICE_TYPE_SYSTEM:
8036                         case CONNMAN_SERVICE_TYPE_P2P:
8037                                 break;
8038
8039                         case CONNMAN_SERVICE_TYPE_GADGET:
8040                         case CONNMAN_SERVICE_TYPE_ETHERNET:
8041                                 if (service->autoconnect) {
8042                                         __connman_service_connect(service,
8043                                                 CONNMAN_SERVICE_CONNECT_REASON_AUTO);
8044                                         break;
8045                                 }
8046
8047                                 /* fall through */
8048                         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
8049                         case CONNMAN_SERVICE_TYPE_GPS:
8050                         case CONNMAN_SERVICE_TYPE_VPN:
8051                         case CONNMAN_SERVICE_TYPE_WIFI:
8052                         case CONNMAN_SERVICE_TYPE_CELLULAR:
8053                                 __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
8054                                 break;
8055                         }
8056                 }
8057
8058 #if defined TIZEN_EXT
8059                 /* TIZEN synchronizes below information when the service creates */
8060                 if (service->eap != NULL)
8061                         connman_network_set_string(service->network, "WiFi.EAP",
8062                                                                 service->eap);
8063                 if (service->identity != NULL)
8064                         connman_network_set_string(service->network, "WiFi.Identity",
8065                                                                 service->identity);
8066                 if (service->phase2 != NULL)
8067                         connman_network_set_string(service->network, "WiFi.Phase2",
8068                                                                 service->phase2);
8069 #endif
8070         }
8071
8072         __connman_notifier_service_add(service, service->name);
8073         service_schedule_added(service);
8074
8075         return service;
8076 }
8077
8078 void __connman_service_update_from_network(struct connman_network *network)
8079 {
8080         bool need_sort = false;
8081         struct connman_service *service;
8082         uint8_t strength;
8083         bool roaming;
8084         const char *name;
8085         bool stats_enable;
8086
8087         service = connman_service_lookup_from_network(network);
8088         if (!service)
8089                 return;
8090
8091         if (!service->network)
8092                 return;
8093
8094 #if defined TIZEN_EXT
8095         if (service->storage_reload) {
8096                 service_load(service);
8097                 __connman_service_set_storage_reload(service, false);
8098         }
8099 #endif
8100
8101         name = connman_network_get_string(service->network, "Name");
8102         if (g_strcmp0(service->name, name) != 0) {
8103                 g_free(service->name);
8104                 service->name = g_strdup(name);
8105
8106                 if (allow_property_changed(service))
8107                         connman_dbus_property_changed_basic(service->path,
8108                                         CONNMAN_SERVICE_INTERFACE, "Name",
8109                                         DBUS_TYPE_STRING, &service->name);
8110         }
8111
8112         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
8113                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
8114
8115         strength = connman_network_get_strength(service->network);
8116         if (strength == service->strength)
8117                 goto roaming;
8118
8119         service->strength = strength;
8120         need_sort = true;
8121
8122         strength_changed(service);
8123
8124 roaming:
8125         roaming = connman_network_get_bool(service->network, "Roaming");
8126         if (roaming == service->roaming)
8127                 goto sorting;
8128
8129         stats_enable = stats_enabled(service);
8130         if (stats_enable)
8131                 stats_stop(service);
8132
8133         service->roaming = roaming;
8134         need_sort = true;
8135
8136         if (stats_enable)
8137                 stats_start(service);
8138
8139         roaming_changed(service);
8140
8141 sorting:
8142         if (need_sort) {
8143                 service_list_sort();
8144         }
8145 }
8146
8147 void __connman_service_remove_from_network(struct connman_network *network)
8148 {
8149         struct connman_service *service;
8150
8151         service = connman_service_lookup_from_network(network);
8152
8153         DBG("network %p service %p", network, service);
8154
8155         if (!service)
8156                 return;
8157
8158         service->ignore = true;
8159
8160         __connman_connection_gateway_remove(service,
8161                                         CONNMAN_IPCONFIG_TYPE_ALL);
8162
8163         connman_service_unref(service);
8164 }
8165
8166 /**
8167  * __connman_service_create_from_provider:
8168  * @provider: provider structure
8169  *
8170  * Look up service by provider and if not found, create one
8171  */
8172 struct connman_service *
8173 __connman_service_create_from_provider(struct connman_provider *provider)
8174 {
8175         struct connman_service *service;
8176         const char *ident, *str;
8177         char *name;
8178         int index = connman_provider_get_index(provider);
8179
8180         DBG("provider %p", provider);
8181
8182         ident = __connman_provider_get_ident(provider);
8183         if (!ident)
8184                 return NULL;
8185
8186         name = g_strdup_printf("vpn_%s", ident);
8187         service = service_get(name);
8188         g_free(name);
8189
8190         if (!service)
8191                 return NULL;
8192
8193         service->type = CONNMAN_SERVICE_TYPE_VPN;
8194         service->provider = connman_provider_ref(provider);
8195         service->autoconnect = false;
8196         service->favorite = true;
8197
8198         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
8199         service->state = combine_state(service->state_ipv4, service->state_ipv6);
8200
8201         str = connman_provider_get_string(provider, "Name");
8202         if (str) {
8203                 g_free(service->name);
8204                 service->name = g_strdup(str);
8205                 service->hidden = false;
8206         } else {
8207                 g_free(service->name);
8208                 service->name = NULL;
8209                 service->hidden = true;
8210         }
8211
8212         service->strength = 0;
8213
8214         if (!service->ipconfig_ipv4)
8215                 service->ipconfig_ipv4 = create_ip4config(service, index,
8216                                 CONNMAN_IPCONFIG_METHOD_MANUAL);
8217
8218         if (!service->ipconfig_ipv6)
8219                 service->ipconfig_ipv6 = create_ip6config(service, index);
8220
8221         service_register(service);
8222
8223         __connman_notifier_service_add(service, service->name);
8224         service_schedule_added(service);
8225
8226         return service;
8227 }
8228
8229 static void remove_unprovisioned_services(void)
8230 {
8231         gchar **services;
8232         GKeyFile *keyfile, *configkeyfile;
8233         char *file, *section;
8234         int i = 0;
8235
8236         services = connman_storage_get_services();
8237         if (!services)
8238                 return;
8239
8240         for (; services[i]; i++) {
8241                 file = section = NULL;
8242                 keyfile = configkeyfile = NULL;
8243
8244                 keyfile = connman_storage_load_service(services[i]);
8245                 if (!keyfile)
8246                         continue;
8247
8248                 file = g_key_file_get_string(keyfile, services[i],
8249                                         "Config.file", NULL);
8250                 if (!file)
8251                         goto next;
8252
8253                 section = g_key_file_get_string(keyfile, services[i],
8254                                         "Config.ident", NULL);
8255                 if (!section)
8256                         goto next;
8257
8258                 configkeyfile = __connman_storage_load_config(file);
8259                 if (!configkeyfile) {
8260                         /*
8261                          * Config file is missing, remove the provisioned
8262                          * service.
8263                          */
8264                         __connman_storage_remove_service(services[i]);
8265                         goto next;
8266                 }
8267
8268                 if (!g_key_file_has_group(configkeyfile, section))
8269                         /*
8270                          * Config section is missing, remove the provisioned
8271                          * service.
8272                          */
8273                         __connman_storage_remove_service(services[i]);
8274
8275         next:
8276                 if (keyfile)
8277                         g_key_file_free(keyfile);
8278
8279                 if (configkeyfile)
8280                         g_key_file_free(configkeyfile);
8281
8282                 g_free(section);
8283                 g_free(file);
8284         }
8285
8286         g_strfreev(services);
8287 }
8288
8289 static int agent_probe(struct connman_agent *agent)
8290 {
8291         DBG("agent %p", agent);
8292         return 0;
8293 }
8294
8295 static void agent_remove(struct connman_agent *agent)
8296 {
8297         DBG("agent %p", agent);
8298 }
8299
8300 static void *agent_context_ref(void *context)
8301 {
8302         struct connman_service *service = context;
8303
8304         return (void *)connman_service_ref(service);
8305 }
8306
8307 static void agent_context_unref(void *context)
8308 {
8309         struct connman_service *service = context;
8310
8311         connman_service_unref(service);
8312 }
8313
8314 static struct connman_agent_driver agent_driver = {
8315         .name           = "service",
8316         .interface      = CONNMAN_AGENT_INTERFACE,
8317         .probe          = agent_probe,
8318         .remove         = agent_remove,
8319         .context_ref    = agent_context_ref,
8320         .context_unref  = agent_context_unref,
8321 };
8322
8323 int __connman_service_init(void)
8324 {
8325         int err;
8326
8327         DBG("");
8328
8329         err = connman_agent_driver_register(&agent_driver);
8330         if (err < 0) {
8331                 connman_error("Cannot register agent driver for %s",
8332                                                 agent_driver.name);
8333                 return err;
8334         }
8335
8336         connection = connman_dbus_get_connection();
8337
8338         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
8339                                                         NULL, service_free);
8340
8341         services_notify = g_new0(struct _services_notify, 1);
8342         services_notify->remove = g_hash_table_new_full(g_str_hash,
8343                         g_str_equal, g_free, NULL);
8344         services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
8345
8346         remove_unprovisioned_services();
8347
8348         return 0;
8349 }
8350
8351 void __connman_service_cleanup(void)
8352 {
8353         DBG("");
8354
8355         if (vpn_autoconnect_timeout) {
8356                 g_source_remove(vpn_autoconnect_timeout);
8357                 vpn_autoconnect_timeout = 0;
8358         }
8359
8360         if (autoconnect_timeout != 0) {
8361                 g_source_remove(autoconnect_timeout);
8362                 autoconnect_timeout = 0;
8363         }
8364
8365         connman_agent_driver_unregister(&agent_driver);
8366
8367         g_list_free(service_list);
8368         service_list = NULL;
8369
8370         g_hash_table_destroy(service_hash);
8371         service_hash = NULL;
8372
8373         g_slist_free(counter_list);
8374         counter_list = NULL;
8375
8376         if (services_notify->id != 0) {
8377                 g_source_remove(services_notify->id);
8378                 service_send_changed(NULL);
8379                 g_hash_table_destroy(services_notify->remove);
8380                 g_hash_table_destroy(services_notify->add);
8381         }
8382         g_free(services_notify);
8383
8384         dbus_connection_unref(connection);
8385 }