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