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