service: Remove previous nameservers and search domains
[framework/connectivity/connman.git] / src / service.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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
33 #include <connman/storage.h>
34 #include <connman/setting.h>
35
36 #include "connman.h"
37
38 #define CONNECT_TIMEOUT         120
39
40 static DBusConnection *connection = NULL;
41
42 static GSequence *service_list = NULL;
43 static GHashTable *service_hash = NULL;
44 static GSList *counter_list = NULL;
45 static unsigned int autoconnect_timeout = 0;
46 static struct connman_service *current_default = NULL;
47
48 struct connman_stats {
49         connman_bool_t valid;
50         connman_bool_t enabled;
51         struct connman_stats_data data_last;
52         struct connman_stats_data data;
53         GTimer *timer;
54 };
55
56 struct connman_stats_counter {
57         connman_bool_t append_all;
58         struct connman_stats stats;
59         struct connman_stats stats_roaming;
60 };
61
62 struct connman_service {
63         int refcount;
64         int session_usage_count;
65         char *identifier;
66         char *path;
67         enum connman_service_type type;
68         enum connman_service_security security;
69         enum connman_service_state state;
70         enum connman_service_state state_ipv4;
71         enum connman_service_state state_ipv6;
72         enum connman_service_error error;
73         connman_uint8_t strength;
74         connman_bool_t favorite;
75         connman_bool_t immutable;
76         connman_bool_t hidden;
77         connman_bool_t ignore;
78         connman_bool_t autoconnect;
79         connman_bool_t userconnect;
80         GTimeVal modified;
81         unsigned int order;
82         char *name;
83         char *passphrase;
84         char *agent_passphrase;
85         connman_bool_t roaming;
86         struct connman_ipconfig *ipconfig_ipv4;
87         struct connman_ipconfig *ipconfig_ipv6;
88         struct connman_network *network;
89         struct connman_provider *provider;
90         char **nameservers;
91         char **nameservers_config;
92         char **nameservers_auto;
93         char **domains;
94         char *domainname;
95         char **timeservers;
96         char **timeservers_config;
97         /* 802.1x settings from the config files */
98         char *eap;
99         char *identity;
100         char *agent_identity;
101         char *ca_cert_file;
102         char *client_cert_file;
103         char *private_key_file;
104         char *private_key_passphrase;
105         char *phase2;
106         DBusMessage *pending;
107         guint timeout;
108         struct connman_stats stats;
109         struct connman_stats stats_roaming;
110         GHashTable *counter_table;
111         enum connman_service_proxy_method proxy;
112         enum connman_service_proxy_method proxy_config;
113         char **proxies;
114         char **excludes;
115         char *pac;
116         connman_bool_t wps;
117         int online_check_count;
118         connman_bool_t do_split_routing;
119         connman_bool_t new_service;
120         connman_bool_t hidden_service;
121 };
122
123 struct find_data {
124         const char *path;
125         struct connman_service *service;
126 };
127
128 static void compare_path(gpointer value, gpointer user_data)
129 {
130         struct connman_service *service = value;
131         struct find_data *data = user_data;
132
133         if (data->service != NULL)
134                 return;
135
136         if (g_strcmp0(service->path, data->path) == 0)
137                 data->service = service;
138 }
139
140 static struct connman_service *find_service(const char *path)
141 {
142         struct find_data data = { .path = path, .service = NULL };
143
144         DBG("path %s", path);
145
146         g_sequence_foreach(service_list, compare_path, &data);
147
148         return data.service;
149 }
150
151 const char *__connman_service_type2string(enum connman_service_type type)
152 {
153         switch (type) {
154         case CONNMAN_SERVICE_TYPE_UNKNOWN:
155                 break;
156         case CONNMAN_SERVICE_TYPE_SYSTEM:
157                 return "system";
158         case CONNMAN_SERVICE_TYPE_ETHERNET:
159                 return "ethernet";
160         case CONNMAN_SERVICE_TYPE_WIFI:
161                 return "wifi";
162         case CONNMAN_SERVICE_TYPE_WIMAX:
163                 return "wimax";
164         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
165                 return "bluetooth";
166         case CONNMAN_SERVICE_TYPE_CELLULAR:
167                 return "cellular";
168         case CONNMAN_SERVICE_TYPE_GPS:
169                 return "gps";
170         case CONNMAN_SERVICE_TYPE_VPN:
171                 return "vpn";
172         case CONNMAN_SERVICE_TYPE_GADGET:
173                 return "gadget";
174         }
175
176         return NULL;
177 }
178
179 enum connman_service_type __connman_service_string2type(const char *str)
180 {
181         if (str == NULL)
182                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
183
184         if (strcmp(str, "ethernet") == 0)
185                 return CONNMAN_SERVICE_TYPE_ETHERNET;
186         if (strcmp(str, "gadget") == 0)
187                 return CONNMAN_SERVICE_TYPE_GADGET;
188         if (strcmp(str, "wifi") == 0)
189                 return CONNMAN_SERVICE_TYPE_WIFI;
190         if (strcmp(str, "cellular") == 0)
191                 return CONNMAN_SERVICE_TYPE_CELLULAR;
192         if (strcmp(str, "bluetooth") == 0)
193                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
194         if (strcmp(str, "wimax") == 0)
195                 return CONNMAN_SERVICE_TYPE_WIMAX;
196         if (strcmp(str, "vpn") == 0)
197                 return CONNMAN_SERVICE_TYPE_VPN;
198         if (strcmp(str, "gps") == 0)
199                 return CONNMAN_SERVICE_TYPE_GPS;
200         if (strcmp(str, "system") == 0)
201                 return CONNMAN_SERVICE_TYPE_SYSTEM;
202
203         return CONNMAN_SERVICE_TYPE_UNKNOWN;
204 }
205
206 static const char *security2string(enum connman_service_security security)
207 {
208         switch (security) {
209         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
210                 break;
211         case CONNMAN_SERVICE_SECURITY_NONE:
212                 return "none";
213         case CONNMAN_SERVICE_SECURITY_WEP:
214                 return "wep";
215         case CONNMAN_SERVICE_SECURITY_PSK:
216         case CONNMAN_SERVICE_SECURITY_WPA:
217         case CONNMAN_SERVICE_SECURITY_RSN:
218                 return "psk";
219         case CONNMAN_SERVICE_SECURITY_8021X:
220                 return "ieee8021x";
221         }
222
223         return NULL;
224 }
225
226 static const char *state2string(enum connman_service_state state)
227 {
228         switch (state) {
229         case CONNMAN_SERVICE_STATE_UNKNOWN:
230                 break;
231         case CONNMAN_SERVICE_STATE_IDLE:
232                 return "idle";
233         case CONNMAN_SERVICE_STATE_ASSOCIATION:
234                 return "association";
235         case CONNMAN_SERVICE_STATE_CONFIGURATION:
236                 return "configuration";
237         case CONNMAN_SERVICE_STATE_READY:
238                 return "ready";
239         case CONNMAN_SERVICE_STATE_ONLINE:
240                 return "online";
241         case CONNMAN_SERVICE_STATE_DISCONNECT:
242                 return "disconnect";
243         case CONNMAN_SERVICE_STATE_FAILURE:
244                 return "failure";
245         }
246
247         return NULL;
248 }
249
250 static const char *error2string(enum connman_service_error error)
251 {
252         switch (error) {
253         case CONNMAN_SERVICE_ERROR_UNKNOWN:
254                 break;
255         case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
256                 return "out-of-range";
257         case CONNMAN_SERVICE_ERROR_PIN_MISSING:
258                 return "pin-missing";
259         case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
260                 return "dhcp-failed";
261         case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
262                 return "connect-failed";
263         case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
264                 return "login-failed";
265         case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
266                 return "auth-failed";
267         case CONNMAN_SERVICE_ERROR_INVALID_KEY:
268                 return "invalid-key";
269         }
270
271         return NULL;
272 }
273
274 static enum connman_service_error string2error(const char *error)
275 {
276         if (g_strcmp0(error, "dhcp-failed") == 0)
277                 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
278         else if (g_strcmp0(error, "pin-missing") == 0)
279                 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
280         else if (g_strcmp0(error, "invalid-key") == 0)
281                 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
282
283         return CONNMAN_SERVICE_ERROR_UNKNOWN;
284 }
285
286 static const char *proxymethod2string(enum connman_service_proxy_method method)
287 {
288         switch (method) {
289         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
290                 return "direct";
291         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
292                 return "manual";
293         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
294                 return "auto";
295         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
296                 break;
297         }
298
299         return NULL;
300 }
301
302 static enum connman_service_proxy_method string2proxymethod(const char *method)
303 {
304         if (g_strcmp0(method, "direct") == 0)
305                 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
306         else if (g_strcmp0(method, "auto") == 0)
307                 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
308         else if (g_strcmp0(method, "manual") == 0)
309                 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
310         else
311                 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
312 }
313
314 static int service_load(struct connman_service *service)
315 {
316         GKeyFile *keyfile;
317         GError *error = NULL;
318         gsize length;
319         gchar *str;
320         connman_bool_t autoconnect;
321         unsigned int ssid_len;
322         int err = 0;
323
324         DBG("service %p", service);
325
326         keyfile = connman_storage_load_service(service->identifier);
327         if (keyfile == NULL) {
328                 service->new_service = TRUE;
329                 return -EIO;
330         } else
331                 service->new_service = FALSE;
332
333         switch (service->type) {
334         case CONNMAN_SERVICE_TYPE_UNKNOWN:
335         case CONNMAN_SERVICE_TYPE_SYSTEM:
336         case CONNMAN_SERVICE_TYPE_GPS:
337         case CONNMAN_SERVICE_TYPE_GADGET:
338                 break;
339         case CONNMAN_SERVICE_TYPE_VPN:
340                 service->do_split_routing = g_key_file_get_boolean(keyfile,
341                                 service->identifier, "SplitRouting", NULL);
342                 break;
343         case CONNMAN_SERVICE_TYPE_WIFI:
344                 if (service->name == NULL) {
345                         gchar *name;
346
347                         name = g_key_file_get_string(keyfile,
348                                         service->identifier, "Name", NULL);
349                         if (name != NULL) {
350                                 g_free(service->name);
351                                 service->name = name;
352                         }
353
354                         if (service->network != NULL)
355                                 connman_network_set_name(service->network,
356                                                                         name);
357                 }
358
359                 if (service->network &&
360                                 connman_network_get_blob(service->network,
361                                         "WiFi.SSID", &ssid_len) == NULL) {
362                         gchar *hex_ssid;
363
364                         hex_ssid = g_key_file_get_string(keyfile,
365                                                         service->identifier,
366                                                                 "SSID", NULL);
367
368                         if (hex_ssid != NULL) {
369                                 gchar *ssid;
370                                 unsigned int i, j = 0, hex;
371                                 size_t hex_ssid_len = strlen(hex_ssid);
372
373                                 ssid = g_try_malloc0(hex_ssid_len / 2);
374                                 if (ssid == NULL) {
375                                         g_free(hex_ssid);
376                                         err = -ENOMEM;
377                                         goto done;
378                                 }
379
380                                 for (i = 0; i < hex_ssid_len; i += 2) {
381                                         sscanf(hex_ssid + i, "%02x", &hex);
382                                         ssid[j++] = hex;
383                                 }
384
385                                 connman_network_set_blob(service->network,
386                                         "WiFi.SSID", ssid, hex_ssid_len / 2);
387                         }
388
389                         g_free(hex_ssid);
390                 }
391                 /* fall through */
392
393         case CONNMAN_SERVICE_TYPE_WIMAX:
394         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
395         case CONNMAN_SERVICE_TYPE_CELLULAR:
396                 service->favorite = g_key_file_get_boolean(keyfile,
397                                 service->identifier, "Favorite", NULL);
398
399                 str = g_key_file_get_string(keyfile,
400                                 service->identifier, "Failure", NULL);
401                 if (str != NULL) {
402                         if (service->favorite == FALSE)
403                                 service->state_ipv4 = service->state_ipv6 =
404                                         CONNMAN_SERVICE_STATE_FAILURE;
405                         service->error = string2error(str);
406                         g_free(str);
407                 }
408                 /* fall through */
409
410         case CONNMAN_SERVICE_TYPE_ETHERNET:
411                 autoconnect = g_key_file_get_boolean(keyfile,
412                                 service->identifier, "AutoConnect", &error);
413                 if (error == NULL)
414                         service->autoconnect = autoconnect;
415                 g_clear_error(&error);
416                 break;
417         }
418
419         str = g_key_file_get_string(keyfile,
420                                 service->identifier, "Modified", NULL);
421         if (str != NULL) {
422                 g_time_val_from_iso8601(str, &service->modified);
423                 g_free(str);
424         }
425
426         str = g_key_file_get_string(keyfile,
427                                 service->identifier, "Passphrase", NULL);
428         if (str != NULL) {
429                 g_free(service->passphrase);
430                 service->passphrase = str;
431         }
432
433         if (service->ipconfig_ipv4 != NULL)
434                 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
435                                         service->identifier, "IPv4.");
436
437         if (service->ipconfig_ipv6 != NULL)
438                 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
439                                         service->identifier, "IPv6.");
440
441         service->nameservers_config = g_key_file_get_string_list(keyfile,
442                         service->identifier, "Nameservers", &length, NULL);
443         if (service->nameservers_config != NULL && length == 0) {
444                 g_strfreev(service->nameservers_config);
445                 service->nameservers_config = NULL;
446         }
447
448         service->timeservers_config = g_key_file_get_string_list(keyfile,
449                         service->identifier, "Timeservers", &length, NULL);
450         if (service->timeservers_config != NULL && length == 0) {
451                 g_strfreev(service->timeservers_config);
452                 service->timeservers_config = NULL;
453         }
454
455         service->domains = g_key_file_get_string_list(keyfile,
456                         service->identifier, "Domains", &length, NULL);
457         if (service->domains != NULL && length == 0) {
458                 g_strfreev(service->domains);
459                 service->domains = NULL;
460         }
461
462         str = g_key_file_get_string(keyfile,
463                                 service->identifier, "Proxy.Method", NULL);
464         if (str != NULL)
465                 service->proxy_config = string2proxymethod(str);
466
467         g_free(str);
468
469         service->proxies = g_key_file_get_string_list(keyfile,
470                         service->identifier, "Proxy.Servers", &length, NULL);
471         if (service->proxies != NULL && length == 0) {
472                 g_strfreev(service->proxies);
473                 service->proxies = NULL;
474         }
475
476         service->excludes = g_key_file_get_string_list(keyfile,
477                         service->identifier, "Proxy.Excludes", &length, NULL);
478         if (service->excludes != NULL && length == 0) {
479                 g_strfreev(service->excludes);
480                 service->excludes = NULL;
481         }
482
483         str = g_key_file_get_string(keyfile,
484                                 service->identifier, "Proxy.URL", NULL);
485         if (str != NULL) {
486                 g_free(service->pac);
487                 service->pac = str;
488         }
489
490         service->hidden_service = g_key_file_get_boolean(keyfile,
491                                         service->identifier, "Hidden", NULL);
492
493 done:
494         g_key_file_free(keyfile);
495
496         return err;
497 }
498
499 static int service_save(struct connman_service *service)
500 {
501         GKeyFile *keyfile;
502         gchar *str;
503         guint freq;
504         const char *cst_str = NULL;
505         int err = 0;
506
507         DBG("service %p new %d", service, service->new_service);
508
509         if (service->new_service == TRUE)
510                 return -ESRCH;
511
512         keyfile = __connman_storage_open_service(service->identifier);
513         if (keyfile == NULL)
514                 return -EIO;
515
516         if (service->name != NULL)
517                 g_key_file_set_string(keyfile, service->identifier,
518                                                 "Name", service->name);
519
520         switch (service->type) {
521         case CONNMAN_SERVICE_TYPE_UNKNOWN:
522         case CONNMAN_SERVICE_TYPE_SYSTEM:
523         case CONNMAN_SERVICE_TYPE_GPS:
524         case CONNMAN_SERVICE_TYPE_GADGET:
525                 break;
526         case CONNMAN_SERVICE_TYPE_VPN:
527                 g_key_file_set_boolean(keyfile, service->identifier,
528                                 "SplitRouting", service->do_split_routing);
529                 break;
530         case CONNMAN_SERVICE_TYPE_WIFI:
531                 if (service->network) {
532                         const unsigned char *ssid;
533                         unsigned int ssid_len = 0;
534
535                         ssid = connman_network_get_blob(service->network,
536                                                         "WiFi.SSID", &ssid_len);
537
538                         if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
539                                 char *identifier = service->identifier;
540                                 GString *str;
541                                 unsigned int i;
542
543                                 str = g_string_sized_new(ssid_len * 2);
544                                 if (str == NULL) {
545                                         err = -ENOMEM;
546                                         goto done;
547                                 }
548
549                                 for (i = 0; i < ssid_len; i++)
550                                         g_string_append_printf(str,
551                                                         "%02x", ssid[i]);
552
553                                 g_key_file_set_string(keyfile, identifier,
554                                                         "SSID", str->str);
555
556                                 g_string_free(str, TRUE);
557                         }
558
559                         freq = connman_network_get_frequency(service->network);
560                         g_key_file_set_integer(keyfile, service->identifier,
561                                                 "Frequency", freq);
562                 }
563                 /* fall through */
564
565         case CONNMAN_SERVICE_TYPE_WIMAX:
566         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
567         case CONNMAN_SERVICE_TYPE_CELLULAR:
568                 g_key_file_set_boolean(keyfile, service->identifier,
569                                         "Favorite", service->favorite);
570
571                 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
572                         service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
573                         const char *failure = error2string(service->error);
574                         if (failure != NULL)
575                                 g_key_file_set_string(keyfile,
576                                                         service->identifier,
577                                                         "Failure", failure);
578                 } else {
579                         g_key_file_remove_key(keyfile, service->identifier,
580                                                         "Failure", NULL);
581                 }
582                 /* fall through */
583
584         case CONNMAN_SERVICE_TYPE_ETHERNET:
585                 if (service->favorite == TRUE)
586                         g_key_file_set_boolean(keyfile, service->identifier,
587                                         "AutoConnect", service->autoconnect);
588                 break;
589         }
590
591         str = g_time_val_to_iso8601(&service->modified);
592         if (str != NULL) {
593                 g_key_file_set_string(keyfile, service->identifier,
594                                                         "Modified", str);
595                 g_free(str);
596         }
597
598         if (service->passphrase != NULL && strlen(service->passphrase) > 0)
599                 g_key_file_set_string(keyfile, service->identifier,
600                                         "Passphrase", service->passphrase);
601         else
602                 g_key_file_remove_key(keyfile, service->identifier,
603                                                         "Passphrase", NULL);
604
605         if (service->ipconfig_ipv4 != NULL)
606                 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
607                                         service->identifier, "IPv4.");
608
609         if (service->ipconfig_ipv6 != NULL)
610                 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
611                                                 service->identifier, "IPv6.");
612
613         if (service->nameservers_config != NULL) {
614                 guint len = g_strv_length(service->nameservers_config);
615
616                 g_key_file_set_string_list(keyfile, service->identifier,
617                                                                 "Nameservers",
618                                 (const gchar **) service->nameservers_config, len);
619         } else
620         g_key_file_remove_key(keyfile, service->identifier,
621                                                         "Nameservers", NULL);
622
623         if (service->timeservers_config != NULL) {
624                 guint len = g_strv_length(service->timeservers_config);
625
626                 g_key_file_set_string_list(keyfile, service->identifier,
627                                                                 "Timeservers",
628                                 (const gchar **) service->timeservers_config, len);
629         } else
630                 g_key_file_remove_key(keyfile, service->identifier,
631                                                         "Timeservers", NULL);
632
633         if (service->domains != NULL) {
634                 guint len = g_strv_length(service->domains);
635
636                 g_key_file_set_string_list(keyfile, service->identifier,
637                                                                 "Domains",
638                                 (const gchar **) service->domains, len);
639         } else
640                 g_key_file_remove_key(keyfile, service->identifier,
641                                                         "Domains", NULL);
642
643         cst_str = proxymethod2string(service->proxy_config);
644         if (cst_str != NULL)
645                 g_key_file_set_string(keyfile, service->identifier,
646                                 "Proxy.Method", cst_str);
647
648         if (service->proxies != NULL) {
649                 guint len = g_strv_length(service->proxies);
650
651                 g_key_file_set_string_list(keyfile, service->identifier,
652                                 "Proxy.Servers",
653                                 (const gchar **) service->proxies, len);
654         } else
655                 g_key_file_remove_key(keyfile, service->identifier,
656                                                 "Proxy.Servers", NULL);
657
658         if (service->excludes != NULL) {
659                 guint len = g_strv_length(service->excludes);
660
661                 g_key_file_set_string_list(keyfile, service->identifier,
662                                 "Proxy.Excludes",
663                                 (const gchar **) service->excludes, len);
664         } else
665                 g_key_file_remove_key(keyfile, service->identifier,
666                                                 "Proxy.Excludes", NULL);
667
668         if (service->pac != NULL && strlen(service->pac) > 0)
669                 g_key_file_set_string(keyfile, service->identifier,
670                                         "Proxy.URL", service->pac);
671         else
672                 g_key_file_remove_key(keyfile, service->identifier,
673                                                         "Proxy.URL", NULL);
674
675         if (service->hidden_service == TRUE)
676                 g_key_file_set_boolean(keyfile, service->identifier, "Hidden",
677                                                                         TRUE);
678
679 done:
680         __connman_storage_save_service(keyfile, service->identifier);
681
682         g_key_file_free(keyfile);
683
684         return err;
685 }
686
687 static enum connman_service_state combine_state(
688                                         enum connman_service_state state_a,
689                                         enum connman_service_state state_b)
690 {
691         enum connman_service_state result;
692
693         if (state_a == state_b) {
694                 result = state_a;
695                 goto done;
696         }
697
698         if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
699                 result = state_b;
700                 goto done;
701         }
702
703         if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
704                 result = state_a;
705                 goto done;
706         }
707
708         if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
709                 result = state_b;
710                 goto done;
711         }
712
713         if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
714                 result = state_a;
715                 goto done;
716         }
717
718         if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
719                 result = state_a;
720                 goto done;
721         }
722
723         if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
724                 result = state_b;
725                 goto done;
726         }
727
728         if (state_a == CONNMAN_SERVICE_STATE_READY) {
729                 result = state_a;
730                 goto done;
731         }
732
733         if (state_b == CONNMAN_SERVICE_STATE_READY) {
734                 result = state_b;
735                 goto done;
736         }
737
738         if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
739                 result = state_a;
740                 goto done;
741         }
742
743         if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
744                 result = state_b;
745                 goto done;
746         }
747
748         if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
749                 result = state_a;
750                 goto done;
751         }
752
753         if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
754                 result = state_b;
755                 goto done;
756         }
757
758         if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
759                 result = state_a;
760                 goto done;
761         }
762
763         if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
764                 result = state_b;
765                 goto done;
766         }
767
768         result = CONNMAN_SERVICE_STATE_FAILURE;
769
770 done:
771         return result;
772 }
773
774 static connman_bool_t is_connecting_state(struct connman_service *service,
775                                         enum connman_service_state state)
776 {
777         switch (state) {
778         case CONNMAN_SERVICE_STATE_UNKNOWN:
779         case CONNMAN_SERVICE_STATE_IDLE:
780         case CONNMAN_SERVICE_STATE_FAILURE:
781                 if (service->network != NULL)
782                         return connman_network_get_connecting(service->network);
783         case CONNMAN_SERVICE_STATE_DISCONNECT:
784         case CONNMAN_SERVICE_STATE_READY:
785         case CONNMAN_SERVICE_STATE_ONLINE:
786                 break;
787         case CONNMAN_SERVICE_STATE_ASSOCIATION:
788         case CONNMAN_SERVICE_STATE_CONFIGURATION:
789                 return TRUE;
790         }
791
792         return FALSE;
793 }
794
795 static connman_bool_t is_connected_state(const struct connman_service *service,
796                                         enum connman_service_state state)
797 {
798         switch (state) {
799         case CONNMAN_SERVICE_STATE_UNKNOWN:
800         case CONNMAN_SERVICE_STATE_IDLE:
801         case CONNMAN_SERVICE_STATE_ASSOCIATION:
802         case CONNMAN_SERVICE_STATE_CONFIGURATION:
803         case CONNMAN_SERVICE_STATE_DISCONNECT:
804         case CONNMAN_SERVICE_STATE_FAILURE:
805                 break;
806         case CONNMAN_SERVICE_STATE_READY:
807         case CONNMAN_SERVICE_STATE_ONLINE:
808                 return TRUE;
809         }
810
811         return FALSE;
812 }
813
814 static connman_bool_t is_idle_state(const struct connman_service *service,
815                                 enum connman_service_state state)
816 {
817         switch (state) {
818         case CONNMAN_SERVICE_STATE_UNKNOWN:
819         case CONNMAN_SERVICE_STATE_ASSOCIATION:
820         case CONNMAN_SERVICE_STATE_CONFIGURATION:
821         case CONNMAN_SERVICE_STATE_READY:
822         case CONNMAN_SERVICE_STATE_ONLINE:
823         case CONNMAN_SERVICE_STATE_DISCONNECT:
824         case CONNMAN_SERVICE_STATE_FAILURE:
825                 break;
826         case CONNMAN_SERVICE_STATE_IDLE:
827                 return TRUE;
828         }
829
830         return FALSE;
831 }
832
833 static connman_bool_t is_connecting(struct connman_service *service)
834 {
835         return is_connecting_state(service, service->state);
836 }
837
838 static connman_bool_t is_connected(struct connman_service *service)
839 {
840         return is_connected_state(service, service->state);
841 }
842
843 static const char *nameserver_get_ifname(struct connman_service *service)
844 {
845         const char *ifname;
846
847         if (service->ipconfig_ipv4)
848                 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
849         else if (service->ipconfig_ipv6)
850                 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
851         else
852                 ifname = NULL;
853
854         if (ifname == NULL)
855                 return NULL;
856
857         switch (combine_state(service->state_ipv4, service->state_ipv6)) {
858         case CONNMAN_SERVICE_STATE_UNKNOWN:
859         case CONNMAN_SERVICE_STATE_IDLE:
860         case CONNMAN_SERVICE_STATE_ASSOCIATION:
861         case CONNMAN_SERVICE_STATE_CONFIGURATION:
862         case CONNMAN_SERVICE_STATE_FAILURE:
863         case CONNMAN_SERVICE_STATE_DISCONNECT:
864                 return NULL;
865         case CONNMAN_SERVICE_STATE_READY:
866         case CONNMAN_SERVICE_STATE_ONLINE:
867                 break;
868         }
869
870         return ifname;
871 }
872
873 static void remove_nameservers(struct connman_service *service,
874                 const char* interface, char **ns)
875 {
876         const char *ifname = interface;
877         int i;
878
879         if (ns == NULL)
880                 return;
881
882         if (interface == NULL)
883                 ifname = nameserver_get_ifname(service);
884
885         if (ifname == NULL)
886                         return;
887
888         for (i = 0; ns[i] != NULL; i++)
889                 connman_resolver_remove(ifname, NULL, ns[i]);
890 }
891
892 static void remove_searchdomains(struct connman_service *service,
893                 const char *interface, char **sd)
894 {
895         const char *ifname = interface;
896         int i;
897
898         if (sd == NULL)
899                 return;
900
901         if (interface == NULL)
902                 ifname = nameserver_get_ifname(service);
903
904         if (ifname == NULL)
905                 return;
906
907         for (i = 0; sd[i] != NULL; i++)
908                 connman_resolver_remove(ifname, sd[i], NULL);
909 }
910
911 static void update_nameservers(struct connman_service *service)
912 {
913         const char *ifname;
914
915         if (service->ipconfig_ipv4)
916                 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
917         else if (service->ipconfig_ipv6)
918                 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
919         else
920                 ifname = NULL;
921
922         if (ifname == NULL)
923                 return;
924
925         switch (combine_state(service->state_ipv4, service->state_ipv6)) {
926         case CONNMAN_SERVICE_STATE_UNKNOWN:
927         case CONNMAN_SERVICE_STATE_IDLE:
928         case CONNMAN_SERVICE_STATE_ASSOCIATION:
929         case CONNMAN_SERVICE_STATE_CONFIGURATION:
930                 return;
931         case CONNMAN_SERVICE_STATE_FAILURE:
932         case CONNMAN_SERVICE_STATE_DISCONNECT:
933                 connman_resolver_remove_all(ifname);
934                 return;
935         case CONNMAN_SERVICE_STATE_READY:
936         case CONNMAN_SERVICE_STATE_ONLINE:
937                 break;
938         }
939
940         if (service->nameservers_config != NULL) {
941                 int i;
942
943                 for (i = 0; service->nameservers_config[i] != NULL; i++) {
944                         connman_resolver_append(ifname, NULL,
945                                                 service->nameservers_config[i]);
946                 }
947         } else if (service->nameservers != NULL) {
948                 int i;
949
950                 for (i = 0; service->nameservers[i] != NULL; i++) {
951                         connman_resolver_append(ifname, NULL,
952                                                 service->nameservers[i]);
953                 }
954         }
955
956         if (service->domains != NULL) {
957                 int i;
958
959                 for (i = 0; service->domains[i]; i++)
960                         connman_resolver_append(ifname, service->domains[i],
961                                                 NULL);
962         } else if (service->domainname != NULL)
963                 connman_resolver_append(ifname, service->domainname, NULL);
964
965         connman_resolver_flush();
966 }
967
968 /*
969  * The is_auto variable is set to true when IPv6 autoconf nameservers are
970  * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
971  * for details) and not through service.c
972  */
973 int __connman_service_nameserver_append(struct connman_service *service,
974                                 const char *nameserver, gboolean is_auto)
975 {
976         char **nameservers;
977         int len, i;
978
979         DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
980
981         if (nameserver == NULL)
982                 return -EINVAL;
983
984         if (is_auto == TRUE)
985                 nameservers = service->nameservers_auto;
986         else
987                 nameservers = service->nameservers;
988
989         for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
990                 if (g_strcmp0(nameservers[i], nameserver) == 0)
991                         return -EEXIST;
992
993         if (nameservers != NULL) {
994                 len = g_strv_length(nameservers);
995                 nameservers = g_try_renew(char *, nameservers, len + 2);
996         } else {
997                 len = 0;
998                 nameservers = g_try_new0(char *, len + 2);
999         }
1000
1001         if (nameservers == NULL)
1002                 return -ENOMEM;
1003
1004         nameservers[len] = g_strdup(nameserver);
1005         if (nameservers[len] == NULL)
1006                 return -ENOMEM;
1007
1008         nameservers[len + 1] = NULL;
1009
1010         if (is_auto == TRUE) {
1011                 service->nameservers_auto = nameservers;
1012         } else {
1013                 service->nameservers = nameservers;
1014                 update_nameservers(service);
1015         }
1016
1017         return 0;
1018 }
1019
1020 int __connman_service_nameserver_remove(struct connman_service *service,
1021                                 const char *nameserver, gboolean is_auto)
1022 {
1023         char **servers, **nameservers;
1024         gboolean found = FALSE;
1025         int len, i, j;
1026
1027         DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
1028
1029         if (nameserver == NULL)
1030                 return -EINVAL;
1031
1032         if (is_auto == TRUE)
1033                 nameservers = service->nameservers_auto;
1034         else
1035                 nameservers = service->nameservers;
1036
1037         if (nameservers == NULL)
1038                 return 0;
1039
1040         for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
1041                 if (g_strcmp0(nameservers[i], nameserver) == 0) {
1042                         found = TRUE;
1043                         break;
1044                 }
1045
1046         if (found == FALSE)
1047                 return 0;
1048
1049         len = g_strv_length(nameservers);
1050
1051         if (len == 1) {
1052                 g_strfreev(nameservers);
1053                 if (is_auto == TRUE)
1054                         service->nameservers_auto = NULL;
1055                 else
1056                         service->nameservers = NULL;
1057
1058                 return 0;
1059         }
1060
1061         servers = g_try_new0(char *, len);
1062         if (servers == NULL)
1063                 return -ENOMEM;
1064
1065         for (i = 0, j = 0; i < len; i++) {
1066                 if (g_strcmp0(nameservers[i], nameserver) != 0) {
1067                         servers[j] = g_strdup(nameservers[i]);
1068                         if (servers[j] == NULL)
1069                                 return -ENOMEM;
1070                         j++;
1071                 }
1072         }
1073         servers[len - 1] = NULL;
1074
1075         g_strfreev(nameservers);
1076         nameservers = servers;
1077
1078         if (is_auto == TRUE) {
1079                 service->nameservers_auto = nameservers;
1080         } else {
1081                 service->nameservers = nameservers;
1082                 update_nameservers(service);
1083         }
1084
1085         return 0;
1086 }
1087
1088 void __connman_service_nameserver_clear(struct connman_service *service)
1089 {
1090         g_strfreev(service->nameservers);
1091         service->nameservers = NULL;
1092
1093         update_nameservers(service);
1094 }
1095
1096 static void add_nameserver_route(int family, int index, char *nameserver,
1097                                 const char *gw)
1098 {
1099         switch (family) {
1100         case AF_INET:
1101                 if (connman_inet_compare_subnet(index, nameserver) == TRUE)
1102                         break;
1103
1104                 if (connman_inet_add_host_route(index, nameserver, gw) < 0)
1105                         /* For P-t-P link the above route add will fail */
1106                         connman_inet_add_host_route(index, nameserver, NULL);
1107                 break;
1108
1109         case AF_INET6:
1110                 if (connman_inet_add_ipv6_host_route(index, nameserver,
1111                                                                 gw) < 0)
1112                         connman_inet_add_ipv6_host_route(index, nameserver,
1113                                                         NULL);
1114                 break;
1115         }
1116 }
1117
1118 static void nameserver_add_routes(int index, char **nameservers,
1119                                         const char *gw)
1120 {
1121         int i, ret, family;
1122         struct addrinfo hints;
1123         struct addrinfo *addr;
1124
1125         for (i = 0; nameservers[i] != NULL; i++) {
1126                 memset(&hints, 0, sizeof(struct addrinfo));
1127                 hints.ai_flags = AI_NUMERICHOST;
1128                 addr = NULL;
1129
1130                 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1131                 if (ret == EAI_NONAME)
1132                         family = AF_INET; /* use the IPv4 as a default */
1133                 else if (ret != 0)
1134                         continue;
1135                 else
1136                         family = addr->ai_family;
1137
1138                 add_nameserver_route(family, index, nameservers[i], gw);
1139
1140                 freeaddrinfo(addr);
1141         }
1142 }
1143
1144 static void nameserver_del_routes(int index, char **nameservers,
1145                                 enum connman_ipconfig_type type)
1146 {
1147         int i, ret, family;
1148         struct addrinfo hints;
1149         struct addrinfo *addr;
1150
1151         for (i = 0; nameservers[i] != NULL; i++) {
1152                 memset(&hints, 0, sizeof(struct addrinfo));
1153                 hints.ai_flags = AI_NUMERICHOST;
1154                 addr = NULL;
1155
1156                 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1157                 if (ret == EAI_NONAME)
1158                         family = AF_INET; /* use the IPv4 as a default */
1159                 else if (ret != 0)
1160                         continue;
1161                 else
1162                         family = addr->ai_family;
1163
1164                 switch (family) {
1165                 case AF_INET:
1166                         if (type != CONNMAN_IPCONFIG_TYPE_IPV6)
1167                                 connman_inet_del_host_route(index,
1168                                                         nameservers[i]);
1169                         break;
1170                 case AF_INET6:
1171                         if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
1172                                 connman_inet_del_ipv6_host_route(index,
1173                                                         nameservers[i]);
1174                         break;
1175                 }
1176
1177                 freeaddrinfo(addr);
1178         }
1179 }
1180
1181 void __connman_service_nameserver_add_routes(struct connman_service *service,
1182                                                 const char *gw)
1183 {
1184         int index = -1;
1185
1186         if (service == NULL)
1187                 return;
1188
1189         if (service->network != NULL)
1190                 index = connman_network_get_index(service->network);
1191         else if (service->provider != NULL)
1192                 index = connman_provider_get_index(service->provider);
1193
1194         if (service->nameservers_config != NULL) {
1195                 /*
1196                  * Configured nameserver takes preference over the
1197                  * discoverd nameserver gathered from DHCP, VPN, etc.
1198                  */
1199                 nameserver_add_routes(index, service->nameservers_config, gw);
1200         } else if (service->nameservers != NULL) {
1201                 /*
1202                  * We add nameservers host routes for nameservers that
1203                  * are not on our subnet. For those who are, the subnet
1204                  * route will be installed by the time the dns proxy code
1205                  * tries to reach them. The subnet route is installed
1206                  * when setting the interface IP address.
1207                  */
1208                 nameserver_add_routes(index, service->nameservers, gw);
1209         }
1210 }
1211
1212 void __connman_service_nameserver_del_routes(struct connman_service *service,
1213                                         enum connman_ipconfig_type type)
1214 {
1215         int index = -1;
1216
1217         if (service == NULL)
1218                 return;
1219
1220         if (service->network != NULL)
1221                 index = connman_network_get_index(service->network);
1222         else if (service->provider != NULL)
1223                 index = connman_provider_get_index(service->provider);
1224
1225         if (service->nameservers_config != NULL)
1226                 nameserver_del_routes(index, service->nameservers_config,
1227                                         type);
1228         else if (service->nameservers != NULL)
1229                 nameserver_del_routes(index, service->nameservers, type);
1230 }
1231
1232 static struct connman_stats *stats_get(struct connman_service *service)
1233 {
1234         if (service->roaming == TRUE)
1235                 return &service->stats_roaming;
1236         else
1237                 return &service->stats;
1238 }
1239
1240 static connman_bool_t stats_enabled(struct connman_service *service)
1241 {
1242         struct connman_stats *stats = stats_get(service);
1243
1244         return stats->enabled;
1245 }
1246
1247 static void stats_start(struct connman_service *service)
1248 {
1249         struct connman_stats *stats = stats_get(service);
1250
1251         DBG("service %p", service);
1252
1253         if (stats->timer == NULL)
1254                 return;
1255
1256         stats->enabled = TRUE;
1257         stats->data_last.time = stats->data.time;
1258
1259         g_timer_start(stats->timer);
1260 }
1261
1262 static void stats_stop(struct connman_service *service)
1263 {
1264         struct connman_stats *stats = stats_get(service);
1265         unsigned int seconds;
1266
1267         DBG("service %p", service);
1268
1269         if (stats->timer == NULL)
1270                 return;
1271
1272         if (stats->enabled == FALSE)
1273                 return;
1274
1275         g_timer_stop(stats->timer);
1276
1277         seconds = g_timer_elapsed(stats->timer, NULL);
1278         stats->data.time = stats->data_last.time + seconds;
1279
1280         stats->enabled = FALSE;
1281 }
1282
1283 static void reset_stats(struct connman_service *service)
1284 {
1285         DBG("service %p", service);
1286
1287         /* home */
1288         service->stats.valid = FALSE;
1289
1290         service->stats.data.rx_packets = 0;
1291         service->stats.data.tx_packets = 0;
1292         service->stats.data.rx_bytes = 0;
1293         service->stats.data.tx_bytes = 0;
1294         service->stats.data.rx_errors = 0;
1295         service->stats.data.tx_errors = 0;
1296         service->stats.data.rx_dropped = 0;
1297         service->stats.data.tx_dropped = 0;
1298         service->stats.data.time = 0;
1299         service->stats.data_last.time = 0;
1300
1301         g_timer_reset(service->stats.timer);
1302
1303         /* roaming */
1304         service->stats_roaming.valid = FALSE;
1305
1306         service->stats_roaming.data.rx_packets = 0;
1307         service->stats_roaming.data.tx_packets = 0;
1308         service->stats_roaming.data.rx_bytes = 0;
1309         service->stats_roaming.data.tx_bytes = 0;
1310         service->stats_roaming.data.rx_errors = 0;
1311         service->stats_roaming.data.tx_errors = 0;
1312         service->stats_roaming.data.rx_dropped = 0;
1313         service->stats_roaming.data.tx_dropped = 0;
1314         service->stats_roaming.data.time = 0;
1315         service->stats_roaming.data_last.time = 0;
1316
1317         g_timer_reset(service->stats_roaming.timer);
1318 }
1319
1320 struct connman_service *__connman_service_get_default(void)
1321 {
1322         struct connman_service *service;
1323         GSequenceIter *iter;
1324
1325         iter = g_sequence_get_begin_iter(service_list);
1326
1327         if (g_sequence_iter_is_end(iter) == TRUE)
1328                 return NULL;
1329
1330         service = g_sequence_get(iter);
1331
1332         if (is_connected(service) == FALSE)
1333                 return NULL;
1334
1335         return service;
1336 }
1337
1338 static void default_changed(void)
1339 {
1340         struct connman_service *service = __connman_service_get_default();
1341
1342         if (service == current_default)
1343                 return;
1344
1345         __connman_service_timeserver_changed(current_default, NULL);
1346
1347         current_default = service;
1348
1349         __connman_notifier_default_changed(service);
1350 }
1351
1352 static void state_changed(struct connman_service *service)
1353 {
1354         const char *str;
1355
1356         __connman_notifier_service_state_changed(service, service->state);
1357
1358         str = state2string(service->state);
1359         if (str == NULL)
1360                 return;
1361
1362         connman_dbus_property_changed_basic(service->path,
1363                                 CONNMAN_SERVICE_INTERFACE, "State",
1364                                                 DBUS_TYPE_STRING, &str);
1365 }
1366
1367 static void strength_changed(struct connman_service *service)
1368 {
1369         if (service->strength == 0)
1370                 return;
1371
1372         connman_dbus_property_changed_basic(service->path,
1373                                 CONNMAN_SERVICE_INTERFACE, "Strength",
1374                                         DBUS_TYPE_BYTE, &service->strength);
1375 }
1376
1377 static void favorite_changed(struct connman_service *service)
1378 {
1379         if (service->path == NULL)
1380                 return;
1381
1382         connman_dbus_property_changed_basic(service->path,
1383                                 CONNMAN_SERVICE_INTERFACE, "Favorite",
1384                                         DBUS_TYPE_BOOLEAN, &service->favorite);
1385 }
1386
1387 static void immutable_changed(struct connman_service *service)
1388 {
1389         if (service->path == NULL)
1390                 return;
1391
1392         connman_dbus_property_changed_basic(service->path,
1393                                 CONNMAN_SERVICE_INTERFACE, "Immutable",
1394                                         DBUS_TYPE_BOOLEAN, &service->immutable);
1395 }
1396
1397 static void roaming_changed(struct connman_service *service)
1398 {
1399         if (service->path == NULL)
1400                 return;
1401
1402         connman_dbus_property_changed_basic(service->path,
1403                                 CONNMAN_SERVICE_INTERFACE, "Roaming",
1404                                         DBUS_TYPE_BOOLEAN, &service->roaming);
1405 }
1406
1407 static void autoconnect_changed(struct connman_service *service)
1408 {
1409         if (service->path == NULL)
1410                 return;
1411
1412         connman_dbus_property_changed_basic(service->path,
1413                                 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1414                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1415 }
1416
1417 static void append_security(DBusMessageIter *iter, void *user_data)
1418 {
1419         struct connman_service *service = user_data;
1420         const char *str;
1421
1422         str = security2string(service->security);
1423         if (str != NULL)
1424                 dbus_message_iter_append_basic(iter,
1425                                 DBUS_TYPE_STRING, &str);
1426
1427         /*
1428          * Some access points incorrectly advertise WPS even when they
1429          * are configured as open or no security, so filter
1430          * appropriately.
1431          */
1432         if (service->wps == TRUE) {
1433                 switch (service->security) {
1434                 case CONNMAN_SERVICE_SECURITY_PSK:
1435                 case CONNMAN_SERVICE_SECURITY_WPA:
1436                 case CONNMAN_SERVICE_SECURITY_RSN:
1437                         str = "wps";
1438                         dbus_message_iter_append_basic(iter,
1439                                 DBUS_TYPE_STRING, &str);
1440                         break;
1441                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1442                 case CONNMAN_SERVICE_SECURITY_NONE:
1443                 case CONNMAN_SERVICE_SECURITY_WEP:
1444                 case CONNMAN_SERVICE_SECURITY_8021X:
1445                         break;
1446                 }
1447         }
1448 }
1449
1450 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1451 {
1452         struct connman_service *service = user_data;
1453
1454         if (service->ipconfig_ipv4 != NULL)
1455                 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1456                                                                         iter);
1457         else if (service->ipconfig_ipv6 != NULL)
1458                 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1459                                                                         iter);
1460 }
1461
1462 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1463 {
1464         struct connman_service *service = user_data;
1465
1466         DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1467                                 state2string(service->state_ipv4));
1468
1469         if (is_connected_state(service, service->state_ipv4) == FALSE)
1470                 return;
1471
1472         if (service->ipconfig_ipv4 != NULL)
1473                 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1474 }
1475
1476 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1477 {
1478         struct connman_service *service = user_data;
1479
1480         DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1481                                 state2string(service->state_ipv6));
1482
1483         if (is_connected_state(service, service->state_ipv6) == FALSE)
1484                 return;
1485
1486         if (service->ipconfig_ipv6 != NULL)
1487                 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1488                                                 service->ipconfig_ipv4);
1489 }
1490
1491 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1492 {
1493         struct connman_service *service = user_data;
1494
1495         if (service->ipconfig_ipv4 != NULL)
1496                 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1497                                                         iter);
1498 }
1499
1500 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1501 {
1502         struct connman_service *service = user_data;
1503
1504         if (service->ipconfig_ipv6 != NULL)
1505                 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1506                                                         iter);
1507 }
1508
1509 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1510 {
1511         char **servers;
1512         int i;
1513
1514         servers = *nameservers;
1515
1516         for (i = 0; servers[i] != NULL; i++) {
1517                 dbus_message_iter_append_basic(iter,
1518                                         DBUS_TYPE_STRING, &servers[i]);
1519         }
1520 }
1521
1522 static void append_dns(DBusMessageIter *iter, void *user_data)
1523 {
1524         struct connman_service *service = user_data;
1525
1526         if (is_connected(service) == FALSE)
1527                 return;
1528
1529         if (service->nameservers_config != NULL) {
1530                 append_nameserver(iter, &service->nameservers_config);
1531                 return;
1532         } else {
1533                 if (service->nameservers != NULL)
1534                         append_nameserver(iter, &service->nameservers);
1535
1536                 if (service->nameservers_auto != NULL)
1537                         append_nameserver(iter, &service->nameservers_auto);
1538         }
1539 }
1540
1541 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1542 {
1543         struct connman_service *service = user_data;
1544         int i;
1545
1546         if (service->nameservers_config == NULL)
1547                 return;
1548
1549         for (i = 0; service->nameservers_config[i]; i++) {
1550                 dbus_message_iter_append_basic(iter,
1551                                 DBUS_TYPE_STRING,
1552                                 &service->nameservers_config[i]);
1553         }
1554 }
1555
1556 static void append_ts(DBusMessageIter *iter, void *user_data)
1557 {
1558         GSList *list = user_data;
1559
1560         while (list != NULL) {
1561                 char *timeserver = list->data;
1562
1563                 if (timeserver != NULL)
1564                         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
1565                                         &timeserver);
1566
1567                 list = g_slist_next(list);
1568         }
1569 }
1570
1571 static void append_tsconfig(DBusMessageIter *iter, void *user_data)
1572 {
1573         struct connman_service *service = user_data;
1574         int i;
1575
1576         if (service->timeservers_config == NULL)
1577                 return;
1578
1579         for (i = 0; service->timeservers_config[i]; i++) {
1580                 dbus_message_iter_append_basic(iter,
1581                                 DBUS_TYPE_STRING,
1582                                 &service->timeservers_config[i]);
1583         }
1584 }
1585
1586 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1587 {
1588         struct connman_service *service = user_data;
1589         int i;
1590
1591         if (service->domains == NULL)
1592                 return;
1593
1594         for (i = 0; service->domains[i]; i++)
1595                 dbus_message_iter_append_basic(iter,
1596                                 DBUS_TYPE_STRING, &service->domains[i]);
1597 }
1598
1599 static void append_domain(DBusMessageIter *iter, void *user_data)
1600 {
1601         struct connman_service *service = user_data;
1602
1603         if (is_connected(service) == FALSE &&
1604                                 is_connecting(service) == FALSE)
1605                 return;
1606
1607         if (service->domains != NULL)
1608                 append_domainconfig(iter, user_data);
1609         else if (service->domainname != NULL)
1610                 dbus_message_iter_append_basic(iter,
1611                                 DBUS_TYPE_STRING, &service->domainname);
1612 }
1613
1614 static void append_proxies(DBusMessageIter *iter, void *user_data)
1615 {
1616         struct connman_service *service = user_data;
1617         int i;
1618
1619         if (service->proxies == NULL)
1620                 return;
1621
1622         for (i = 0; service->proxies[i]; i++)
1623                 dbus_message_iter_append_basic(iter,
1624                                 DBUS_TYPE_STRING, &service->proxies[i]);
1625 }
1626
1627 static void append_excludes(DBusMessageIter *iter, void *user_data)
1628 {
1629         struct connman_service *service = user_data;
1630         int i;
1631
1632         if (service->excludes == NULL)
1633                 return;
1634
1635         for (i = 0; service->excludes[i]; i++)
1636                 dbus_message_iter_append_basic(iter,
1637                                 DBUS_TYPE_STRING, &service->excludes[i]);
1638 }
1639
1640 static void append_proxy(DBusMessageIter *iter, void *user_data)
1641 {
1642         struct connman_service *service = user_data;
1643         enum connman_service_proxy_method proxy;
1644         const char *pac = NULL;
1645         const char *method = proxymethod2string(
1646                 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1647
1648         DBG("");
1649
1650         if (is_connected(service) == FALSE)
1651                 return;
1652
1653         proxy = connman_service_get_proxy_method(service);
1654
1655         switch (proxy) {
1656         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1657                 return;
1658         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1659                 goto done;
1660         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1661                 connman_dbus_dict_append_array(iter, "Servers",
1662                                         DBUS_TYPE_STRING, append_proxies,
1663                                         service);
1664
1665                 connman_dbus_dict_append_array(iter, "Excludes",
1666                                         DBUS_TYPE_STRING, append_excludes,
1667                                         service);
1668                 break;
1669         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1670                 /* Maybe DHCP, or WPAD,  has provided an url for a pac file */
1671                 if (service->ipconfig_ipv4 != NULL)
1672                         pac = __connman_ipconfig_get_proxy_autoconfig(
1673                                 service->ipconfig_ipv4);
1674                 else if (service->ipconfig_ipv6 != NULL)
1675                         pac = __connman_ipconfig_get_proxy_autoconfig(
1676                                 service->ipconfig_ipv6);
1677
1678                 if (service->pac == NULL && pac == NULL)
1679                         goto done;
1680
1681                 if (service->pac != NULL)
1682                         pac = service->pac;
1683
1684                 connman_dbus_dict_append_basic(iter, "URL",
1685                                         DBUS_TYPE_STRING, &pac);
1686                 break;
1687         }
1688
1689         method = proxymethod2string(proxy);
1690
1691 done:
1692         connman_dbus_dict_append_basic(iter, "Method",
1693                                         DBUS_TYPE_STRING, &method);
1694 }
1695
1696 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1697 {
1698         struct connman_service *service = user_data;
1699         const char *method;
1700
1701         if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1702                 return;
1703
1704         switch (service->proxy_config) {
1705         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1706                 return;
1707         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1708                 break;
1709         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1710                 if (service->proxies != NULL)
1711                         connman_dbus_dict_append_array(iter, "Servers",
1712                                                 DBUS_TYPE_STRING,
1713                                                 append_proxies, service);
1714
1715                 if (service->excludes != NULL)
1716                         connman_dbus_dict_append_array(iter, "Excludes",
1717                                                 DBUS_TYPE_STRING,
1718                                                 append_excludes, service);
1719                 break;
1720         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1721                 if (service->pac != NULL)
1722                         connman_dbus_dict_append_basic(iter, "URL",
1723                                         DBUS_TYPE_STRING, &service->pac);
1724                 break;
1725         }
1726
1727         method = proxymethod2string(service->proxy_config);
1728
1729         connman_dbus_dict_append_basic(iter, "Method",
1730                                 DBUS_TYPE_STRING, &method);
1731 }
1732
1733 static void append_provider(DBusMessageIter *iter, void *user_data)
1734 {
1735         struct connman_service *service = user_data;
1736
1737         DBG("%p %p", service, service->provider);
1738
1739         if (is_connected(service) == FALSE)
1740                 return;
1741
1742         if (service->provider != NULL)
1743                 __connman_provider_append_properties(service->provider, iter);
1744 }
1745
1746
1747 static void settings_changed(struct connman_service *service,
1748                                 struct connman_ipconfig *ipconfig)
1749 {
1750         connman_dbus_property_changed_dict(service->path,
1751                                         CONNMAN_SERVICE_INTERFACE, "IPv4",
1752                                                         append_ipv4, service);
1753
1754         connman_dbus_property_changed_dict(service->path,
1755                                         CONNMAN_SERVICE_INTERFACE, "IPv6",
1756                                                         append_ipv6, service);
1757
1758         __connman_notifier_ipconfig_changed(service, ipconfig);
1759 }
1760
1761 static void ipv4_configuration_changed(struct connman_service *service)
1762 {
1763         connman_dbus_property_changed_dict(service->path,
1764                                         CONNMAN_SERVICE_INTERFACE,
1765                                                         "IPv4.Configuration",
1766                                                         append_ipv4config,
1767                                                         service);
1768 }
1769
1770 static void ipv6_configuration_changed(struct connman_service *service)
1771 {
1772         connman_dbus_property_changed_dict(service->path,
1773                                         CONNMAN_SERVICE_INTERFACE,
1774                                                         "IPv6.Configuration",
1775                                                         append_ipv6config,
1776                                                         service);
1777 }
1778
1779 static void dns_changed(struct connman_service *service)
1780 {
1781         connman_dbus_property_changed_array(service->path,
1782                                 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1783                                         DBUS_TYPE_STRING, append_dns, service);
1784 }
1785
1786 static void dns_configuration_changed(struct connman_service *service)
1787 {
1788         connman_dbus_property_changed_array(service->path,
1789                                 CONNMAN_SERVICE_INTERFACE,
1790                                 "Nameservers.Configuration",
1791                                 DBUS_TYPE_STRING, append_dnsconfig, service);
1792
1793         dns_changed(service);
1794 }
1795
1796 static void domain_changed(struct connman_service *service)
1797 {
1798         connman_dbus_property_changed_array(service->path,
1799                                 CONNMAN_SERVICE_INTERFACE, "Domains",
1800                                 DBUS_TYPE_STRING, append_domain, service);
1801 }
1802
1803 static void domain_configuration_changed(struct connman_service *service)
1804 {
1805         connman_dbus_property_changed_array(service->path,
1806                                 CONNMAN_SERVICE_INTERFACE,
1807                                 "Domains.Configuration",
1808                                 DBUS_TYPE_STRING, append_domainconfig, service);
1809 }
1810
1811 static void proxy_changed(struct connman_service *service)
1812 {
1813         connman_dbus_property_changed_dict(service->path,
1814                                         CONNMAN_SERVICE_INTERFACE, "Proxy",
1815                                                         append_proxy, service);
1816 }
1817
1818 static void proxy_configuration_changed(struct connman_service *service)
1819 {
1820         connman_dbus_property_changed_dict(service->path,
1821                         CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1822                                                 append_proxyconfig, service);
1823
1824         proxy_changed(service);
1825 }
1826
1827 static void timeservers_configuration_changed(struct connman_service *service)
1828 {
1829         connman_dbus_property_changed_array(service->path,
1830                         CONNMAN_SERVICE_INTERFACE,
1831                         "Timeservers.Configuration",
1832                         DBUS_TYPE_STRING,
1833                         append_tsconfig, service);
1834 }
1835
1836 static void link_changed(struct connman_service *service)
1837 {
1838         connman_dbus_property_changed_dict(service->path,
1839                                         CONNMAN_SERVICE_INTERFACE, "Ethernet",
1840                                                 append_ethernet, service);
1841 }
1842
1843 static void stats_append_counters(DBusMessageIter *dict,
1844                         struct connman_stats_data *stats,
1845                         struct connman_stats_data *counters,
1846                         connman_bool_t append_all)
1847 {
1848         if (counters->rx_packets != stats->rx_packets || append_all) {
1849                 counters->rx_packets = stats->rx_packets;
1850                 connman_dbus_dict_append_basic(dict, "RX.Packets",
1851                                         DBUS_TYPE_UINT32, &stats->rx_packets);
1852         }
1853
1854         if (counters->tx_packets != stats->tx_packets || append_all) {
1855                 counters->tx_packets = stats->tx_packets;
1856                 connman_dbus_dict_append_basic(dict, "TX.Packets",
1857                                         DBUS_TYPE_UINT32, &stats->tx_packets);
1858         }
1859
1860         if (counters->rx_bytes != stats->rx_bytes || append_all) {
1861                 counters->rx_bytes = stats->rx_bytes;
1862                 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1863                                         DBUS_TYPE_UINT32, &stats->rx_bytes);
1864         }
1865
1866         if (counters->tx_bytes != stats->tx_bytes || append_all) {
1867                 counters->tx_bytes = stats->tx_bytes;
1868                 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1869                                         DBUS_TYPE_UINT32, &stats->tx_bytes);
1870         }
1871
1872         if (counters->rx_errors != stats->rx_errors || append_all) {
1873                 counters->rx_errors = stats->rx_errors;
1874                 connman_dbus_dict_append_basic(dict, "RX.Errors",
1875                                         DBUS_TYPE_UINT32, &stats->rx_errors);
1876         }
1877
1878         if (counters->tx_errors != stats->tx_errors || append_all) {
1879                 counters->tx_errors = stats->tx_errors;
1880                 connman_dbus_dict_append_basic(dict, "TX.Errors",
1881                                         DBUS_TYPE_UINT32, &stats->tx_errors);
1882         }
1883
1884         if (counters->rx_dropped != stats->rx_dropped || append_all) {
1885                 counters->rx_dropped = stats->rx_dropped;
1886                 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1887                                         DBUS_TYPE_UINT32, &stats->rx_dropped);
1888         }
1889
1890         if (counters->tx_dropped != stats->tx_dropped || append_all) {
1891                 counters->tx_dropped = stats->tx_dropped;
1892                 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1893                                         DBUS_TYPE_UINT32, &stats->tx_dropped);
1894         }
1895
1896         if (counters->time != stats->time || append_all) {
1897                 counters->time = stats->time;
1898                 connman_dbus_dict_append_basic(dict, "Time",
1899                                         DBUS_TYPE_UINT32, &stats->time);
1900         }
1901 }
1902
1903 static void stats_append(struct connman_service *service,
1904                                 const char *counter,
1905                                 struct connman_stats_counter *counters,
1906                                 connman_bool_t append_all)
1907 {
1908         DBusMessageIter array, dict;
1909         DBusMessage *msg;
1910
1911         DBG("service %p counter %s", service, counter);
1912
1913         msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1914         if (msg == NULL)
1915                 return;
1916
1917         dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1918                                 &service->path, DBUS_TYPE_INVALID);
1919
1920         dbus_message_iter_init_append(msg, &array);
1921
1922         /* home counter */
1923         connman_dbus_dict_open(&array, &dict);
1924
1925         stats_append_counters(&dict, &service->stats.data,
1926                                 &counters->stats.data, append_all);
1927
1928         connman_dbus_dict_close(&array, &dict);
1929
1930         /* roaming counter */
1931         connman_dbus_dict_open(&array, &dict);
1932
1933         stats_append_counters(&dict, &service->stats_roaming.data,
1934                                 &counters->stats_roaming.data, append_all);
1935
1936         connman_dbus_dict_close(&array, &dict);
1937
1938         __connman_counter_send_usage(counter, msg);
1939 }
1940
1941 static void stats_update(struct connman_service *service,
1942                                 unsigned int rx_packets, unsigned int tx_packets,
1943                                 unsigned int rx_bytes, unsigned int tx_bytes,
1944                                 unsigned int rx_errors, unsigned int tx_errors,
1945                                 unsigned int rx_dropped, unsigned int tx_dropped)
1946 {
1947         struct connman_stats *stats = stats_get(service);
1948         struct connman_stats_data *data_last = &stats->data_last;
1949         struct connman_stats_data *data = &stats->data;
1950         unsigned int seconds;
1951
1952         DBG("service %p", service);
1953
1954         if (stats->valid == TRUE) {
1955                 data->rx_packets +=
1956                         rx_packets - data_last->rx_packets;
1957                 data->tx_packets +=
1958                         tx_packets - data_last->tx_packets;
1959                 data->rx_bytes +=
1960                         rx_bytes - data_last->rx_bytes;
1961                 data->tx_bytes +=
1962                         tx_bytes - data_last->tx_bytes;
1963                 data->rx_errors +=
1964                         rx_errors - data_last->rx_errors;
1965                 data->tx_errors +=
1966                         tx_errors - data_last->tx_errors;
1967                 data->rx_dropped +=
1968                         rx_dropped - data_last->rx_dropped;
1969                 data->tx_dropped +=
1970                         tx_dropped - data_last->tx_dropped;
1971         } else {
1972                 stats->valid = TRUE;
1973         }
1974
1975         data_last->rx_packets = rx_packets;
1976         data_last->tx_packets = tx_packets;
1977         data_last->rx_bytes = rx_bytes;
1978         data_last->tx_bytes = tx_bytes;
1979         data_last->rx_errors = rx_errors;
1980         data_last->tx_errors = tx_errors;
1981         data_last->rx_dropped = rx_dropped;
1982         data_last->tx_dropped = tx_dropped;
1983
1984         seconds = g_timer_elapsed(stats->timer, NULL);
1985         stats->data.time = stats->data_last.time + seconds;
1986 }
1987
1988 void __connman_service_notify(struct connman_service *service,
1989                         unsigned int rx_packets, unsigned int tx_packets,
1990                         unsigned int rx_bytes, unsigned int tx_bytes,
1991                         unsigned int rx_errors, unsigned int tx_errors,
1992                         unsigned int rx_dropped, unsigned int tx_dropped)
1993 {
1994         GHashTableIter iter;
1995         gpointer key, value;
1996         const char *counter;
1997         struct connman_stats_counter *counters;
1998         struct connman_stats_data *data;
1999         int err;
2000
2001         if (service == NULL)
2002                 return;
2003
2004         if (is_connected(service) == FALSE)
2005                 return;
2006
2007         stats_update(service,
2008                 rx_packets, tx_packets,
2009                 rx_bytes, tx_bytes,
2010                 rx_errors, tx_errors,
2011                 rx_dropped, tx_dropped);
2012
2013         data = &stats_get(service)->data;
2014         err = __connman_stats_update(service, service->roaming, data);
2015         if (err < 0)
2016                 connman_error("Failed to store statistics for %s",
2017                                 service->identifier);
2018
2019         g_hash_table_iter_init(&iter, service->counter_table);
2020         while (g_hash_table_iter_next(&iter, &key, &value)) {
2021                 counter = key;
2022                 counters = value;
2023
2024                 stats_append(service, counter, counters, counters->append_all);
2025                 counters->append_all = FALSE;
2026         }
2027 }
2028
2029 int __connman_service_counter_register(const char *counter)
2030 {
2031         struct connman_service *service;
2032         GSequenceIter *iter;
2033         struct connman_stats_counter *counters;
2034
2035         DBG("counter %s", counter);
2036
2037         counter_list = g_slist_append(counter_list, (gpointer)counter);
2038
2039         iter = g_sequence_get_begin_iter(service_list);
2040
2041         while (g_sequence_iter_is_end(iter) == FALSE) {
2042                 service = g_sequence_get(iter);
2043
2044                 counters = g_try_new0(struct connman_stats_counter, 1);
2045                 if (counters == NULL)
2046                         return -ENOMEM;
2047
2048                 counters->append_all = TRUE;
2049
2050                 g_hash_table_replace(service->counter_table, (gpointer)counter,
2051                                         counters);
2052
2053                 iter = g_sequence_iter_next(iter);
2054         }
2055
2056         return 0;
2057 }
2058
2059 void __connman_service_counter_unregister(const char *counter)
2060 {
2061         struct connman_service *service;
2062         GSequenceIter *iter;
2063
2064         DBG("counter %s", counter);
2065
2066         iter = g_sequence_get_begin_iter(service_list);
2067
2068         while (g_sequence_iter_is_end(iter) == FALSE) {
2069                 service = g_sequence_get(iter);
2070
2071                 g_hash_table_remove(service->counter_table, counter);
2072
2073                 iter = g_sequence_iter_next(iter);
2074         }
2075
2076         counter_list = g_slist_remove(counter_list, counter);
2077 }
2078
2079 GSequence *__connman_service_get_list(struct connman_session *session,
2080                                 service_match_cb service_match,
2081                                 create_service_entry_cb create_service_entry,
2082                                 GDestroyNotify destroy_service_entry)
2083 {
2084         GSequence *list;
2085         GSequenceIter *iter;
2086         struct connman_service *service;
2087         struct service_entry *entry;
2088
2089         list = g_sequence_new(destroy_service_entry);
2090         if (list == NULL)
2091                 return NULL;
2092
2093         iter = g_sequence_get_begin_iter(service_list);
2094
2095         while (g_sequence_iter_is_end(iter) == FALSE) {
2096                 service = g_sequence_get(iter);
2097
2098                 if (service_match(session, service) == TRUE) {
2099                         entry = create_service_entry(service, service->name,
2100                                                         service->state);
2101                         if (entry == NULL)
2102                                 return list;
2103
2104                         g_sequence_append(list, entry);
2105                 }
2106
2107                 iter = g_sequence_iter_next(iter);
2108         }
2109
2110         return list;
2111 }
2112
2113 void __connman_service_session_inc(struct connman_service *service)
2114 {
2115         DBG("service %p ref count %d", service,
2116                 service->session_usage_count + 1);
2117
2118         __sync_fetch_and_add(&service->session_usage_count, 1);
2119 }
2120
2121 connman_bool_t __connman_service_session_dec(struct connman_service *service)
2122 {
2123         DBG("service %p ref count %d", service,
2124                 service->session_usage_count - 1);
2125
2126         if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
2127                 return FALSE;
2128
2129         return TRUE;
2130 }
2131
2132 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2133                                         struct connman_service *service)
2134 {
2135         const char *str;
2136         GSList *list;
2137
2138         str = __connman_service_type2string(service->type);
2139         if (str != NULL)
2140                 connman_dbus_dict_append_basic(dict, "Type",
2141                                                 DBUS_TYPE_STRING, &str);
2142
2143         connman_dbus_dict_append_array(dict, "Security",
2144                                 DBUS_TYPE_STRING, append_security, service);
2145
2146         str = state2string(service->state);
2147         if (str != NULL)
2148                 connman_dbus_dict_append_basic(dict, "State",
2149                                                 DBUS_TYPE_STRING, &str);
2150
2151         str = error2string(service->error);
2152         if (str != NULL)
2153                 connman_dbus_dict_append_basic(dict, "Error",
2154                                                 DBUS_TYPE_STRING, &str);
2155
2156         if (service->strength > 0)
2157                 connman_dbus_dict_append_basic(dict, "Strength",
2158                                         DBUS_TYPE_BYTE, &service->strength);
2159
2160         connman_dbus_dict_append_basic(dict, "Favorite",
2161                                         DBUS_TYPE_BOOLEAN, &service->favorite);
2162
2163         connman_dbus_dict_append_basic(dict, "Immutable",
2164                                         DBUS_TYPE_BOOLEAN, &service->immutable);
2165
2166         if (service->favorite == TRUE)
2167                 connman_dbus_dict_append_basic(dict, "AutoConnect",
2168                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2169         else
2170                 connman_dbus_dict_append_basic(dict, "AutoConnect",
2171                                         DBUS_TYPE_BOOLEAN, &service->favorite);
2172
2173         if (service->name != NULL)
2174                 connman_dbus_dict_append_basic(dict, "Name",
2175                                         DBUS_TYPE_STRING, &service->name);
2176
2177         switch (service->type) {
2178         case CONNMAN_SERVICE_TYPE_UNKNOWN:
2179         case CONNMAN_SERVICE_TYPE_SYSTEM:
2180         case CONNMAN_SERVICE_TYPE_GPS:
2181         case CONNMAN_SERVICE_TYPE_VPN:
2182         case CONNMAN_SERVICE_TYPE_GADGET:
2183                 break;
2184         case CONNMAN_SERVICE_TYPE_CELLULAR:
2185                 connman_dbus_dict_append_basic(dict, "Roaming",
2186                                         DBUS_TYPE_BOOLEAN, &service->roaming);
2187
2188                 connman_dbus_dict_append_dict(dict, "Ethernet",
2189                                                 append_ethernet, service);
2190                 break;
2191         case CONNMAN_SERVICE_TYPE_WIFI:
2192         case CONNMAN_SERVICE_TYPE_ETHERNET:
2193         case CONNMAN_SERVICE_TYPE_WIMAX:
2194         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2195                 connman_dbus_dict_append_dict(dict, "Ethernet",
2196                                                 append_ethernet, service);
2197                 break;
2198         }
2199
2200         connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2201
2202         connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2203                                                 append_ipv4config, service);
2204
2205         connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2206
2207         connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2208                                                 append_ipv6config, service);
2209
2210         connman_dbus_dict_append_array(dict, "Nameservers",
2211                                 DBUS_TYPE_STRING, append_dns, service);
2212
2213         connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2214                                 DBUS_TYPE_STRING, append_dnsconfig, service);
2215
2216         if (service->state == CONNMAN_SERVICE_STATE_READY ||
2217                         service->state == CONNMAN_SERVICE_STATE_ONLINE)
2218                 list = __connman_timeserver_get_all(service);
2219         else
2220                 list = NULL;
2221
2222         connman_dbus_dict_append_array(dict, "Timeservers",
2223                                 DBUS_TYPE_STRING, append_ts, list);
2224
2225         g_slist_free_full(list, g_free);
2226
2227         connman_dbus_dict_append_array(dict, "Timeservers.Configuration",
2228                                 DBUS_TYPE_STRING, append_tsconfig, service);
2229
2230         connman_dbus_dict_append_array(dict, "Domains",
2231                                 DBUS_TYPE_STRING, append_domain, service);
2232
2233         connman_dbus_dict_append_array(dict, "Domains.Configuration",
2234                                 DBUS_TYPE_STRING, append_domainconfig, service);
2235
2236         connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2237
2238         connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2239                                                 append_proxyconfig, service);
2240
2241         connman_dbus_dict_append_dict(dict, "Provider",
2242                                                 append_provider, service);
2243 }
2244
2245 static void append_struct_service(DBusMessageIter *iter,
2246                 connman_dbus_append_cb_t function,
2247                 struct connman_service *service)
2248 {
2249         DBusMessageIter entry, dict;
2250
2251         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2252
2253         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2254                                                         &service->path);
2255
2256         connman_dbus_dict_open(&entry, &dict);
2257         if (function != NULL)
2258                 function(&dict, service);
2259         connman_dbus_dict_close(&entry, &dict);
2260
2261         dbus_message_iter_close_container(iter, &entry);
2262 }
2263
2264 static void append_dict_properties(DBusMessageIter *dict, void *user_data)
2265 {
2266         struct connman_service *service = user_data;
2267
2268         append_properties(dict, TRUE, service);
2269 }
2270
2271 static void append_struct(gpointer value, gpointer user_data)
2272 {
2273         struct connman_service *service = value;
2274         DBusMessageIter *iter = user_data;
2275
2276         if (service->path == NULL)
2277                 return;
2278
2279         append_struct_service(iter, append_dict_properties, service);
2280 }
2281
2282 void __connman_service_list_struct(DBusMessageIter *iter)
2283 {
2284         g_sequence_foreach(service_list, append_struct, iter);
2285 }
2286
2287 connman_bool_t __connman_service_is_hidden(struct connman_service *service)
2288 {
2289         return service->hidden;
2290 }
2291
2292 connman_bool_t
2293 __connman_service_is_split_routing(struct connman_service *service)
2294 {
2295         return service->do_split_routing;
2296 }
2297
2298 int __connman_service_get_index(struct connman_service *service)
2299 {
2300         if (service == NULL)
2301                 return -1;
2302
2303         if (service->network != NULL)
2304                 return connman_network_get_index(service->network);
2305         else if (service->provider != NULL)
2306                 return connman_provider_get_index(service->provider);
2307
2308         return -1;
2309 }
2310
2311 void __connman_service_set_hidden(struct connman_service *service)
2312 {
2313         if (service == NULL || service->hidden == TRUE)
2314                 return;
2315
2316         service->hidden_service = TRUE;
2317 }
2318
2319 void __connman_service_set_domainname(struct connman_service *service,
2320                                                 const char *domainname)
2321 {
2322         if (service == NULL || service->hidden == TRUE)
2323                 return;
2324
2325         g_free(service->domainname);
2326         service->domainname = g_strdup(domainname);
2327
2328         domain_changed(service);
2329 }
2330
2331 const char *connman_service_get_domainname(struct connman_service *service)
2332 {
2333         if (service == NULL)
2334                 return NULL;
2335
2336         if (service->domains != NULL)
2337                 return service->domains[0];
2338         else
2339                 return service->domainname;
2340 }
2341
2342 char **connman_service_get_nameservers(struct connman_service *service)
2343 {
2344         if (service == NULL)
2345                 return NULL;
2346
2347         if (service->nameservers_config != NULL)
2348                 return g_strdupv(service->nameservers_config);
2349         else if (service->nameservers != NULL ||
2350                                         service->nameservers_auto != NULL) {
2351                 int len = 0, len_auto = 0, i;
2352                 char **nameservers;
2353
2354                 if (service->nameservers != NULL)
2355                         len = g_strv_length(service->nameservers);
2356                 if (service->nameservers_auto != NULL)
2357                         len_auto = g_strv_length(service->nameservers_auto);
2358
2359                 nameservers = g_try_new0(char *, len + len_auto + 1);
2360                 if (nameservers == NULL)
2361                         return NULL;
2362
2363                 for (i = 0; i < len; i++)
2364                         nameservers[i] = g_strdup(service->nameservers[i]);
2365
2366                 for (i = 0; i < len_auto; i++)
2367                         nameservers[i + len] =
2368                                 g_strdup(service->nameservers_auto[i]);
2369
2370                 return nameservers;
2371         }
2372
2373         return NULL;
2374 }
2375
2376 char **connman_service_get_timeservers_config(struct connman_service *service)
2377 {
2378         if (service == NULL)
2379                 return NULL;
2380
2381         return service->timeservers_config;
2382 }
2383
2384 char **connman_service_get_timeservers(struct connman_service *service)
2385 {
2386         if (service == NULL)
2387                 return NULL;
2388
2389         if (service->timeservers != NULL)
2390                 return service->timeservers;
2391
2392         return NULL;
2393 }
2394
2395 void connman_service_set_proxy_method(struct connman_service *service,
2396                                         enum connman_service_proxy_method method)
2397 {
2398         if (service == NULL || service->hidden == TRUE)
2399                 return;
2400
2401         service->proxy = method;
2402
2403         proxy_changed(service);
2404
2405         if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2406                 __connman_notifier_proxy_changed(service);
2407 }
2408
2409 enum connman_service_proxy_method connman_service_get_proxy_method(
2410                                         struct connman_service *service)
2411 {
2412         if (service == NULL)
2413                 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2414
2415         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2416                 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2417                                 service->pac == NULL)
2418                         return service->proxy;
2419
2420                 return service->proxy_config;
2421         }
2422
2423         return service->proxy;
2424 }
2425
2426 char **connman_service_get_proxy_servers(struct connman_service *service)
2427 {
2428         return g_strdupv(service->proxies);
2429 }
2430
2431 char **connman_service_get_proxy_excludes(struct connman_service *service)
2432 {
2433         return g_strdupv(service->excludes);
2434 }
2435
2436 const char *connman_service_get_proxy_url(struct connman_service *service)
2437 {
2438         if (service == NULL)
2439                 return NULL;
2440
2441         return service->pac;
2442 }
2443
2444 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2445                                                         const char *url)
2446 {
2447         if (service == NULL || service->hidden == TRUE)
2448                 return;
2449
2450         service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2451
2452         if (service->ipconfig_ipv4) {
2453                 if (__connman_ipconfig_set_proxy_autoconfig(
2454                             service->ipconfig_ipv4, url) < 0)
2455                         return;
2456         } else if (service->ipconfig_ipv6) {
2457                 if (__connman_ipconfig_set_proxy_autoconfig(
2458                             service->ipconfig_ipv6, url) < 0)
2459                         return;
2460         } else
2461                 return;
2462
2463         proxy_changed(service);
2464
2465         __connman_notifier_proxy_changed(service);
2466 }
2467
2468 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2469 {
2470         if (service == NULL)
2471                 return NULL;
2472
2473         if (service->ipconfig_ipv4)
2474                 return __connman_ipconfig_get_proxy_autoconfig(
2475                                                 service->ipconfig_ipv4);
2476         else if (service->ipconfig_ipv6)
2477                 return __connman_ipconfig_get_proxy_autoconfig(
2478                                                 service->ipconfig_ipv6);
2479         return NULL;
2480 }
2481
2482 int __connman_service_timeserver_append(struct connman_service *service,
2483                                                 const char *timeserver)
2484 {
2485         int len;
2486
2487         DBG("service %p timeserver %s", service, timeserver);
2488
2489         if (timeserver == NULL)
2490                 return -EINVAL;
2491
2492         if (service->timeservers != NULL) {
2493                 int i;
2494
2495                 for (i = 0; service->timeservers[i] != NULL; i++)
2496                         if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2497                                 return -EEXIST;
2498
2499                 len = g_strv_length(service->timeservers);
2500                 service->timeservers = g_try_renew(char *, service->timeservers,
2501                                                         len + 2);
2502         } else {
2503                 len = 0;
2504                 service->timeservers = g_try_new0(char *, len + 2);
2505         }
2506
2507         if (service->timeservers == NULL)
2508                 return -ENOMEM;
2509
2510         service->timeservers[len] = g_strdup(timeserver);
2511         service->timeservers[len + 1] = NULL;
2512
2513         return 0;
2514 }
2515
2516 int __connman_service_timeserver_remove(struct connman_service *service,
2517                                                 const char *timeserver)
2518 {
2519         char **servers;
2520         int len, i, j, found = 0;
2521
2522         DBG("service %p timeserver %s", service, timeserver);
2523
2524         if (timeserver == NULL)
2525                 return -EINVAL;
2526
2527         if (service->timeservers == NULL)
2528                 return 0;
2529
2530         for (i = 0; service->timeservers != NULL &&
2531                                         service->timeservers[i] != NULL; i++)
2532                 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2533                         found = 1;
2534                         break;
2535                 }
2536
2537         if (found == 0)
2538                 return 0;
2539
2540         len = g_strv_length(service->timeservers);
2541
2542         if (len == 1) {
2543                 g_strfreev(service->timeservers);
2544                 service->timeservers = NULL;
2545
2546                 return 0;
2547         }
2548
2549         servers = g_try_new0(char *, len);
2550         if (servers == NULL)
2551                 return -ENOMEM;
2552
2553         for (i = 0, j = 0; i < len; i++) {
2554                 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2555                         servers[j] = g_strdup(service->timeservers[i]);
2556                         if (servers[j] == NULL)
2557                                 return -ENOMEM;
2558                         j++;
2559                 }
2560         }
2561         servers[len - 1] = NULL;
2562
2563         g_strfreev(service->timeservers);
2564         service->timeservers = servers;
2565
2566         return 0;
2567 }
2568
2569 void __connman_service_timeserver_changed(struct connman_service *service,
2570                 GSList *ts_list)
2571 {
2572         if (service == NULL)
2573                 return;
2574
2575         connman_dbus_property_changed_array(service->path,
2576                         CONNMAN_SERVICE_INTERFACE, "Timeservers",
2577                         DBUS_TYPE_STRING, append_ts, ts_list);
2578 }
2579
2580 void __connman_service_set_pac(struct connman_service *service,
2581                                         const char *pac)
2582 {
2583         if (service->hidden == TRUE)
2584                 return;
2585         g_free(service->pac);
2586         service->pac = g_strdup(pac);
2587
2588         proxy_changed(service);
2589 }
2590
2591 void __connman_service_set_identity(struct connman_service *service,
2592                                         const char *identity)
2593 {
2594         if (service->immutable || service->hidden == TRUE)
2595                 return;
2596
2597         g_free(service->identity);
2598         service->identity = g_strdup(identity);
2599
2600         if (service->network != NULL)
2601                 connman_network_set_string(service->network,
2602                                         "WiFi.Identity",
2603                                         service->identity);
2604 }
2605
2606 void __connman_service_set_agent_identity(struct connman_service *service,
2607                                                 const char *agent_identity)
2608 {
2609         if (service->hidden == TRUE)
2610                 return;
2611         g_free(service->agent_identity);
2612         service->agent_identity = g_strdup(agent_identity);
2613
2614         if (service->network != NULL)
2615                 connman_network_set_string(service->network,
2616                                         "WiFi.AgentIdentity",
2617                                         service->agent_identity);
2618 }
2619
2620 static int check_passphrase(enum connman_service_security security,
2621                                 const char *passphrase)
2622 {
2623         guint i;
2624         gsize length;
2625
2626         if (passphrase == NULL)
2627                 return 0;
2628
2629         length = strlen(passphrase);
2630
2631         switch (security) {
2632         case CONNMAN_SERVICE_SECURITY_PSK:
2633         case CONNMAN_SERVICE_SECURITY_WPA:
2634         case CONNMAN_SERVICE_SECURITY_RSN:
2635                 /* A raw key is always 64 bytes length,
2636                  * its content is in hex representation.
2637                  * A PSK key must be between [8..63].
2638                  */
2639                 if (length == 64) {
2640                         for (i = 0; i < 64; i++)
2641                                 if (!isxdigit((unsigned char)
2642                                               passphrase[i]))
2643                                         return -ENOKEY;
2644                 } else if (length < 8 || length > 63)
2645                         return -ENOKEY;
2646                 break;
2647         case CONNMAN_SERVICE_SECURITY_WEP:
2648                 /* length of WEP key is 10 or 26
2649                  * length of WEP passphrase is 5 or 13
2650                  */
2651                 if (length == 10 || length == 26) {
2652                         for (i = 0; i < length; i++)
2653                                 if (!isxdigit((unsigned char)
2654                                               passphrase[i]))
2655                                         return -ENOKEY;
2656                 } else if (length != 5 && length != 13)
2657                         return -ENOKEY;
2658                 break;
2659         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2660         case CONNMAN_SERVICE_SECURITY_NONE:
2661         case CONNMAN_SERVICE_SECURITY_8021X:
2662                 break;
2663         }
2664
2665         return 0;
2666 }
2667
2668 int __connman_service_set_passphrase(struct connman_service *service,
2669                                         const char *passphrase)
2670 {
2671         int err = 0;
2672
2673         if (service->immutable == TRUE || service->hidden == TRUE)
2674                 return -EINVAL;
2675
2676         err = check_passphrase(service->security, passphrase);
2677
2678         if (err == 0) {
2679                 g_free(service->passphrase);
2680                 service->passphrase = g_strdup(passphrase);
2681
2682                 if (service->network != NULL)
2683                         connman_network_set_string(service->network,
2684                                                         "WiFi.Passphrase",
2685                                                         service->passphrase);
2686                 service_save(service);
2687         }
2688
2689         return err;
2690 }
2691
2692 void __connman_service_set_agent_passphrase(struct connman_service *service,
2693                                                 const char *agent_passphrase)
2694 {
2695         if (service->hidden == TRUE)
2696                 return;
2697         g_free(service->agent_passphrase);
2698         service->agent_passphrase = g_strdup(agent_passphrase);
2699
2700         if (service->network != NULL)
2701                 connman_network_set_string(service->network,
2702                                         "WiFi.AgentPassphrase",
2703                                         service->agent_passphrase);
2704 }
2705
2706 static DBusMessage *get_properties(DBusConnection *conn,
2707                                         DBusMessage *msg, void *user_data)
2708 {
2709         struct connman_service *service = user_data;
2710         DBusMessage *reply;
2711         DBusMessageIter array, dict;
2712
2713         DBG("service %p", service);
2714
2715         reply = dbus_message_new_method_return(msg);
2716         if (reply == NULL)
2717                 return NULL;
2718
2719         dbus_message_iter_init_append(reply, &array);
2720
2721         connman_dbus_dict_open(&array, &dict);
2722         append_properties(&dict, FALSE, service);
2723         connman_dbus_dict_close(&array, &dict);
2724
2725         return reply;
2726 }
2727
2728 static int update_proxy_configuration(struct connman_service *service,
2729                                 DBusMessageIter *array)
2730 {
2731         DBusMessageIter dict;
2732         enum connman_service_proxy_method method;
2733         GString *servers_str = NULL;
2734         GString *excludes_str = NULL;
2735         const char *url = NULL;
2736
2737         method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2738
2739         dbus_message_iter_recurse(array, &dict);
2740
2741         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2742                 DBusMessageIter entry, variant;
2743                 const char *key;
2744                 int type;
2745
2746                 dbus_message_iter_recurse(&dict, &entry);
2747
2748                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2749                         goto error;
2750
2751                 dbus_message_iter_get_basic(&entry, &key);
2752                 dbus_message_iter_next(&entry);
2753
2754                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2755                         goto error;
2756
2757                 dbus_message_iter_recurse(&entry, &variant);
2758
2759                 type = dbus_message_iter_get_arg_type(&variant);
2760
2761                 if (g_str_equal(key, "Method") == TRUE) {
2762                         const char *val;
2763
2764                         if (type != DBUS_TYPE_STRING)
2765                                 goto error;
2766
2767                         dbus_message_iter_get_basic(&variant, &val);
2768                         method = string2proxymethod(val);
2769                 } else if (g_str_equal(key, "URL") == TRUE) {
2770                         if (type != DBUS_TYPE_STRING)
2771                                 goto error;
2772
2773                         dbus_message_iter_get_basic(&variant, &url);
2774                 } else if (g_str_equal(key, "Servers") == TRUE) {
2775                         DBusMessageIter str_array;
2776
2777                         if (type != DBUS_TYPE_ARRAY)
2778                                 goto error;
2779
2780                         servers_str = g_string_new(NULL);
2781                         if (servers_str == NULL)
2782                                 goto error;
2783
2784                         dbus_message_iter_recurse(&variant, &str_array);
2785
2786                         while (dbus_message_iter_get_arg_type(&str_array) ==
2787                                                         DBUS_TYPE_STRING) {
2788                                 char *val = NULL;
2789
2790                                 dbus_message_iter_get_basic(&str_array, &val);
2791
2792                                 if (servers_str->len > 0)
2793                                         g_string_append_printf(servers_str,
2794                                                         " %s", val);
2795                                 else
2796                                         g_string_append(servers_str, val);
2797
2798                                 dbus_message_iter_next(&str_array);
2799                         }
2800                 } else if (g_str_equal(key, "Excludes") == TRUE) {
2801                         DBusMessageIter str_array;
2802
2803                         if (type != DBUS_TYPE_ARRAY)
2804                                 goto error;
2805
2806                         excludes_str = g_string_new(NULL);
2807                         if (excludes_str == NULL)
2808                                 goto error;
2809
2810                         dbus_message_iter_recurse(&variant, &str_array);
2811
2812                         while (dbus_message_iter_get_arg_type(&str_array) ==
2813                                                         DBUS_TYPE_STRING) {
2814                                 char *val = NULL;
2815
2816                                 dbus_message_iter_get_basic(&str_array, &val);
2817
2818                                 if (excludes_str->len > 0)
2819                                         g_string_append_printf(excludes_str,
2820                                                         " %s", val);
2821                                 else
2822                                         g_string_append(excludes_str, val);
2823
2824                                 dbus_message_iter_next(&str_array);
2825                         }
2826                 }
2827
2828                 dbus_message_iter_next(&dict);
2829         }
2830
2831         switch (method) {
2832         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2833                 break;
2834         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2835                 if (servers_str == NULL && service->proxies == NULL)
2836                         goto error;
2837
2838                 if (servers_str != NULL) {
2839                         g_strfreev(service->proxies);
2840
2841                         if (servers_str->len > 0)
2842                                 service->proxies = g_strsplit_set(
2843                                         servers_str->str, " ", 0);
2844                         else
2845                                 service->proxies = NULL;
2846                 }
2847
2848                 if (excludes_str != NULL) {
2849                         g_strfreev(service->excludes);
2850
2851                         if (excludes_str->len > 0)
2852                                 service->excludes = g_strsplit_set(
2853                                         excludes_str->str, " ", 0);
2854                         else
2855                                 service->excludes = NULL;
2856                 }
2857
2858                 if (service->proxies == NULL)
2859                         method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2860
2861                 break;
2862         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2863                 g_free(service->pac);
2864
2865                 if (url != NULL && strlen(url) > 0)
2866                         service->pac = g_strdup(url);
2867                 else
2868                         service->pac = NULL;
2869
2870                 /* if we are connected:
2871                    - if service->pac == NULL
2872                    - if __connman_ipconfig_get_proxy_autoconfig(
2873                    service->ipconfig) == NULL
2874                    --> We should start WPAD */
2875
2876                 break;
2877         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2878                 goto error;
2879         }
2880
2881         if (servers_str != NULL)
2882                 g_string_free(servers_str, TRUE);
2883
2884         if (excludes_str != NULL)
2885                 g_string_free(excludes_str, TRUE);
2886
2887         service->proxy_config = method;
2888
2889         return 0;
2890
2891 error:
2892         if (servers_str != NULL)
2893                 g_string_free(servers_str, TRUE);
2894
2895         if (excludes_str != NULL)
2896                 g_string_free(excludes_str, TRUE);
2897
2898         return -EINVAL;
2899 }
2900
2901 static int set_ipconfig(struct connman_service *service,
2902                         struct connman_ipconfig *ipconfig,
2903                         DBusMessageIter *array,
2904                         enum connman_service_state state,
2905                         enum connman_service_state *new_state)
2906 {
2907         enum connman_ipconfig_method old_method;
2908         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2909         enum connman_ipconfig_type type;
2910         int err;
2911
2912         if (ipconfig == NULL)
2913                 return -EINVAL;
2914
2915         old_method = __connman_ipconfig_get_method(ipconfig);
2916
2917         if (is_connecting_state(service, state) ||
2918                                         is_connected_state(service, state))
2919                 __connman_network_clear_ipconfig(service->network, ipconfig);
2920
2921         err = __connman_ipconfig_set_config(ipconfig, array);
2922         method = __connman_ipconfig_get_method(ipconfig);
2923         type = __connman_ipconfig_get_config_type(ipconfig);
2924
2925         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2926                 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2927                                 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2928                         *new_state = service->state_ipv4 =
2929                                 CONNMAN_SERVICE_STATE_CONFIGURATION;
2930                         __connman_ipconfig_enable(ipconfig);
2931                 }
2932
2933         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2934                 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2935                                 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2936                         *new_state = service->state_ipv6;
2937                         __connman_ipconfig_enable(ipconfig);
2938                 }
2939         }
2940
2941         DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2942                 type, method, state2string(*new_state));
2943
2944         return err;
2945 }
2946
2947 static DBusMessage *set_property(DBusConnection *conn,
2948                                         DBusMessage *msg, void *user_data)
2949 {
2950         struct connman_service *service = user_data;
2951         DBusMessageIter iter, value;
2952         const char *name;
2953         int type;
2954
2955         DBG("service %p", service);
2956
2957         if (dbus_message_iter_init(msg, &iter) == FALSE)
2958                 return __connman_error_invalid_arguments(msg);
2959
2960         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
2961                 return __connman_error_invalid_arguments(msg);
2962
2963         dbus_message_iter_get_basic(&iter, &name);
2964         dbus_message_iter_next(&iter);
2965
2966         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
2967                 return __connman_error_invalid_arguments(msg);
2968
2969         dbus_message_iter_recurse(&iter, &value);
2970
2971         type = dbus_message_iter_get_arg_type(&value);
2972
2973         if (g_str_equal(name, "AutoConnect") == TRUE) {
2974                 connman_bool_t autoconnect;
2975
2976                 if (type != DBUS_TYPE_BOOLEAN)
2977                         return __connman_error_invalid_arguments(msg);
2978
2979                 if (service->favorite == FALSE)
2980                         return __connman_error_invalid_service(msg);
2981
2982                 dbus_message_iter_get_basic(&value, &autoconnect);
2983
2984                 if (service->autoconnect == autoconnect)
2985                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2986
2987                 service->autoconnect = autoconnect;
2988
2989                 autoconnect_changed(service);
2990
2991                 service_save(service);
2992         } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2993                 DBusMessageIter entry;
2994                 GString *str;
2995                 int index;
2996                 const char *gw;
2997
2998                 if (type != DBUS_TYPE_ARRAY)
2999                         return __connman_error_invalid_arguments(msg);
3000
3001                 str = g_string_new(NULL);
3002                 if (str == NULL)
3003                         return __connman_error_invalid_arguments(msg);
3004
3005                 index = connman_network_get_index(service->network);
3006                 gw = __connman_ipconfig_get_gateway_from_index(index,
3007                         CONNMAN_IPCONFIG_TYPE_ALL);
3008
3009                 if (gw && strlen(gw))
3010                         __connman_service_nameserver_del_routes(service,
3011                                                 CONNMAN_IPCONFIG_TYPE_ALL);
3012
3013                 dbus_message_iter_recurse(&value, &entry);
3014
3015                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3016                         const char *val;
3017                         dbus_message_iter_get_basic(&entry, &val);
3018                         dbus_message_iter_next(&entry);
3019                         if (str->len > 0)
3020                                 g_string_append_printf(str, " %s", val);
3021                         else
3022                                 g_string_append(str, val);
3023                 }
3024
3025                 remove_nameservers(service, NULL, service->nameservers_config);
3026                 g_strfreev(service->nameservers_config);
3027
3028                 if (str->len > 0) {
3029                         service->nameservers_config =
3030                                 g_strsplit_set(str->str, " ", 0);
3031                 } else {
3032                         service->nameservers_config = NULL;
3033                 }
3034
3035                 g_string_free(str, TRUE);
3036
3037                 if (gw && strlen(gw))
3038                         __connman_service_nameserver_add_routes(service, gw);
3039
3040                 update_nameservers(service);
3041                 dns_configuration_changed(service);
3042
3043                 service_save(service);
3044         } else if (g_str_equal(name, "Timeservers.Configuration") == TRUE) {
3045                 DBusMessageIter entry;
3046                 GSList *list = NULL;
3047                 int count = 0;
3048
3049                 if (type != DBUS_TYPE_ARRAY)
3050                         return __connman_error_invalid_arguments(msg);
3051
3052                 dbus_message_iter_recurse(&value, &entry);
3053
3054                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3055                         const char *val;
3056                         GSList *new_head;
3057
3058                         dbus_message_iter_get_basic(&entry, &val);
3059
3060                         new_head = __connman_timeserver_add_list(list, val);
3061                         if (list != new_head) {
3062                                 count++;
3063                                 list = new_head;
3064                         }
3065
3066                         dbus_message_iter_next(&entry);
3067                 }
3068
3069                 g_strfreev(service->timeservers_config);
3070                 service->timeservers_config = NULL;
3071
3072                 if (list != NULL) {
3073                         service->timeservers_config = g_new0(char *, count+1);
3074
3075                         while (list != NULL) {
3076                                 count--;
3077                                 service->timeservers_config[count] = list->data;
3078                                 list = g_slist_delete_link(list, list);
3079                         };
3080                 }
3081
3082                 service_save(service);
3083                 timeservers_configuration_changed(service);
3084
3085                 __connman_timeserver_sync(service);
3086         } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
3087                 DBusMessageIter entry;
3088                 GString *str;
3089
3090                 if (type != DBUS_TYPE_ARRAY)
3091                         return __connman_error_invalid_arguments(msg);
3092
3093                 str = g_string_new(NULL);
3094                 if (str == NULL)
3095                         return __connman_error_invalid_arguments(msg);
3096
3097                 dbus_message_iter_recurse(&value, &entry);
3098
3099                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3100                         const char *val;
3101                         dbus_message_iter_get_basic(&entry, &val);
3102                         dbus_message_iter_next(&entry);
3103                         if (str->len > 0)
3104                                 g_string_append_printf(str, " %s", val);
3105                         else
3106                                 g_string_append(str, val);
3107                 }
3108
3109                 remove_searchdomains(service, NULL, service->domains);
3110                 g_strfreev(service->domains);
3111
3112                 if (str->len > 0)
3113                         service->domains = g_strsplit_set(str->str, " ", 0);
3114                 else
3115                         service->domains = NULL;
3116
3117                 g_string_free(str, TRUE);
3118
3119                 update_nameservers(service);
3120                 domain_configuration_changed(service);
3121
3122                 service_save(service);
3123         } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
3124                 int err;
3125
3126                 if (type != DBUS_TYPE_ARRAY)
3127                         return __connman_error_invalid_arguments(msg);
3128
3129                 err = update_proxy_configuration(service, &value);
3130
3131                 if (err < 0)
3132                         return __connman_error_failed(msg, -err);
3133
3134                 proxy_configuration_changed(service);
3135
3136                 __connman_notifier_proxy_changed(service);
3137
3138                 service_save(service);
3139         } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
3140                         g_str_equal(name, "IPv6.Configuration")) {
3141
3142                 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
3143                 enum connman_service_state state =
3144                                                 CONNMAN_SERVICE_STATE_UNKNOWN;
3145                 int err = 0;
3146
3147                 DBG("%s", name);
3148
3149                 if (service->ipconfig_ipv4 == NULL &&
3150                                         service->ipconfig_ipv6 == NULL)
3151                         return __connman_error_invalid_property(msg);
3152
3153                 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
3154                         ipv4 = service->ipconfig_ipv4;
3155                         err = set_ipconfig(service, ipv4, &value,
3156                                         service->state_ipv4, &state);
3157
3158                 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
3159                         ipv6 = service->ipconfig_ipv6;
3160                         err = set_ipconfig(service, ipv6, &value,
3161                                         service->state_ipv6, &state);
3162                 }
3163
3164                 if (err < 0) {
3165                         if (is_connected_state(service, state) ||
3166                                         is_connecting_state(service, state))
3167                                 __connman_network_set_ipconfig(service->network,
3168                                                                 ipv4, ipv6);
3169                         return __connman_error_failed(msg, -err);
3170                 }
3171
3172                 if (ipv4)
3173                         ipv4_configuration_changed(service);
3174                 else if (ipv6)
3175                         ipv6_configuration_changed(service);
3176
3177                 if (is_connecting(service) || is_connected(service))
3178                         __connman_network_set_ipconfig(service->network,
3179                                                         ipv4, ipv6);
3180
3181                 service_save(service);
3182         } else
3183                 return __connman_error_invalid_property(msg);
3184
3185         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3186 }
3187
3188 static void set_idle(struct connman_service *service)
3189 {
3190         service->state = service->state_ipv4 = service->state_ipv6 =
3191                                                 CONNMAN_SERVICE_STATE_IDLE;
3192         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3193         state_changed(service);
3194 }
3195
3196 static DBusMessage *clear_property(DBusConnection *conn,
3197                                         DBusMessage *msg, void *user_data)
3198 {
3199         struct connman_service *service = user_data;
3200         const char *name;
3201
3202         DBG("service %p", service);
3203
3204         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
3205                                                         DBUS_TYPE_INVALID);
3206
3207         if (g_str_equal(name, "Error") == TRUE) {
3208                 set_idle(service);
3209
3210                 g_get_current_time(&service->modified);
3211                 service_save(service);
3212         } else
3213                 return __connman_error_invalid_property(msg);
3214
3215         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3216 }
3217
3218 static connman_bool_t is_ignore(struct connman_service *service)
3219 {
3220         if (service->autoconnect == FALSE)
3221                 return TRUE;
3222
3223         if (service->roaming == TRUE)
3224                 return TRUE;
3225
3226         if (service->ignore == TRUE)
3227                 return TRUE;
3228
3229         if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3230                 return TRUE;
3231
3232         return FALSE;
3233 }
3234
3235 struct preferred_tech_data {
3236         GSequence *preferred_list;
3237         enum connman_service_type type;
3238 };
3239
3240 static void preferred_tech_add_by_type(gpointer data, gpointer user_data)
3241 {
3242         struct connman_service *service = data;
3243         struct preferred_tech_data *tech_data = user_data;
3244
3245         if (service->type == tech_data->type) {
3246                 g_sequence_append(tech_data->preferred_list, service);
3247
3248                 DBG("type %d service %p %s", tech_data->type, service,
3249                                 service->name);
3250         }
3251 }
3252
3253 static GSequence* preferred_tech_list_get(GSequence *list)
3254 {
3255         unsigned int *tech_array;
3256         struct preferred_tech_data tech_data;
3257         int i;
3258
3259         tech_array = connman_setting_get_uint_list("PreferredTechnologies");
3260         if (tech_array == NULL)
3261                 return NULL;
3262
3263         tech_data.preferred_list = g_sequence_new(NULL);
3264
3265         for (i = 0; tech_array[i] != 0; i += 1) {
3266                 tech_data.type = tech_array[i];
3267                 g_sequence_foreach(service_list, preferred_tech_add_by_type,
3268                                 &tech_data);
3269         }
3270
3271         return tech_data.preferred_list;
3272 }
3273
3274 static connman_bool_t auto_connect_service(GSequenceIter* iter,
3275                 connman_bool_t preferred)
3276 {
3277         struct connman_service *service = NULL;
3278
3279         while (g_sequence_iter_is_end(iter) == FALSE) {
3280                 service = g_sequence_get(iter);
3281
3282                 if (service->pending != NULL)
3283                         return TRUE;
3284
3285                 if (is_connecting(service) == TRUE)
3286                         return TRUE;
3287
3288                 if (service->favorite == FALSE) {
3289                         if (preferred == TRUE)
3290                                 goto next_service;
3291                         return FALSE;
3292                 }
3293
3294                 if (is_connected(service) == TRUE) {
3295                         if (preferred == TRUE && service->state !=
3296                                         CONNMAN_SERVICE_STATE_ONLINE)
3297                                 goto next_service;
3298                         return TRUE;
3299                 }
3300
3301                 if (is_ignore(service) == FALSE && service->state ==
3302                                 CONNMAN_SERVICE_STATE_IDLE)
3303                         break;
3304
3305         next_service:
3306                 service = NULL;
3307
3308                 iter = g_sequence_iter_next(iter);
3309         }
3310
3311         if (service != NULL) {
3312
3313                 DBG("service %p %s %s", service, service->name,
3314                                 (preferred == TRUE)? "preferred": "auto");
3315
3316                 service->userconnect = FALSE;
3317                 __connman_service_connect(service);
3318                 return TRUE;
3319         }
3320         return FALSE;
3321 }
3322
3323 static gboolean run_auto_connect(gpointer data)
3324 {
3325         GSequenceIter *iter = NULL;
3326         GSequence *preferred_tech;
3327
3328         autoconnect_timeout = 0;
3329
3330         DBG("");
3331
3332         preferred_tech = preferred_tech_list_get(service_list);
3333         if (preferred_tech != NULL)
3334                 iter = g_sequence_get_begin_iter(preferred_tech);
3335
3336         if (iter == NULL || auto_connect_service(iter, TRUE) == FALSE)
3337                 iter = g_sequence_get_begin_iter(service_list);
3338
3339         if (iter != NULL)
3340                 auto_connect_service(iter, FALSE);
3341
3342         if (preferred_tech != NULL)
3343                 g_sequence_free(preferred_tech);
3344
3345         return FALSE;
3346 }
3347
3348 void __connman_service_auto_connect(void)
3349 {
3350         DBG("");
3351
3352         if (__connman_session_mode() == TRUE) {
3353                 DBG("Session mode enabled: auto connect disabled");
3354                 return;
3355         }
3356
3357         if (autoconnect_timeout != 0)
3358                 return;
3359
3360         autoconnect_timeout = g_timeout_add_seconds(0, run_auto_connect, NULL);
3361 }
3362
3363 static void remove_timeout(struct connman_service *service)
3364 {
3365         if (service->timeout > 0) {
3366                 g_source_remove(service->timeout);
3367                 service->timeout = 0;
3368         }
3369 }
3370
3371 static void reply_pending(struct connman_service *service, int error)
3372 {
3373         remove_timeout(service);
3374
3375         if (service->pending != NULL) {
3376                 if (error > 0) {
3377                         DBusMessage *reply;
3378
3379                         reply = __connman_error_failed(service->pending,
3380                                                                 error);
3381                         if (reply != NULL)
3382                                 g_dbus_send_message(connection, reply);
3383                 } else {
3384                         const char *sender;
3385
3386                         sender = dbus_message_get_interface(service->pending);
3387
3388                         DBG("sender %s", sender);
3389
3390                         if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
3391                                 g_dbus_send_reply(connection, service->pending,
3392                                         DBUS_TYPE_OBJECT_PATH, &service->path,
3393                                                         DBUS_TYPE_INVALID);
3394                         else
3395                                 g_dbus_send_reply(connection, service->pending,
3396                                                         DBUS_TYPE_INVALID);
3397                 }
3398
3399                 dbus_message_unref(service->pending);
3400                 service->pending = NULL;
3401         }
3402 }
3403
3404 static gboolean connect_timeout(gpointer user_data)
3405 {
3406         struct connman_service *service = user_data;
3407         connman_bool_t autoconnect = FALSE;
3408
3409         DBG("service %p", service);
3410
3411         service->timeout = 0;
3412
3413         if (service->network != NULL)
3414                 __connman_network_disconnect(service->network);
3415
3416         __connman_ipconfig_disable(service->ipconfig_ipv4);
3417         __connman_ipconfig_disable(service->ipconfig_ipv6);
3418
3419         __connman_stats_service_unregister(service);
3420
3421         if (service->pending != NULL) {
3422                 DBusMessage *reply;
3423
3424                 reply = __connman_error_operation_timeout(service->pending);
3425                 if (reply != NULL)
3426                         g_dbus_send_message(connection, reply);
3427
3428                 dbus_message_unref(service->pending);
3429                 service->pending = NULL;
3430         } else
3431                 autoconnect = TRUE;
3432
3433         __connman_service_ipconfig_indicate_state(service,
3434                                         CONNMAN_SERVICE_STATE_FAILURE,
3435                                         CONNMAN_IPCONFIG_TYPE_IPV4);
3436         __connman_service_ipconfig_indicate_state(service,
3437                                         CONNMAN_SERVICE_STATE_FAILURE,
3438                                         CONNMAN_IPCONFIG_TYPE_IPV6);
3439
3440         if (autoconnect == TRUE && service->userconnect == FALSE)
3441                 __connman_service_auto_connect();
3442
3443         return FALSE;
3444 }
3445
3446 static void set_reconnect_state(struct connman_service *service,
3447                                                 connman_bool_t reconnect)
3448 {
3449         struct connman_device *device;
3450
3451         if (service->network == NULL)
3452                 return;
3453
3454         device = connman_network_get_device(service->network);
3455         if (device == NULL)
3456                 return;
3457
3458         __connman_device_set_reconnect(device, reconnect);
3459 }
3460
3461 static connman_bool_t get_reconnect_state(struct connman_service *service)
3462 {
3463         struct connman_device *device;
3464
3465         if (service->network == NULL)
3466                 return FALSE;
3467
3468         device = connman_network_get_device(service->network);
3469         if (device == NULL)
3470                 return FALSE;
3471
3472         return __connman_device_get_reconnect(device);
3473 }
3474
3475 static DBusMessage *connect_service(DBusConnection *conn,
3476                                         DBusMessage *msg, void *user_data)
3477 {
3478         struct connman_service *service = user_data;
3479         GSequenceIter *iter;
3480         int err;
3481
3482         DBG("service %p", service);
3483
3484         if (service->pending != NULL)
3485                 return __connman_error_in_progress(msg);
3486
3487         iter = g_sequence_get_begin_iter(service_list);
3488
3489         while (g_sequence_iter_is_end(iter) == FALSE) {
3490                 struct connman_service *temp = g_sequence_get(iter);
3491
3492                 if (service->type == temp->type && is_connecting(temp) == TRUE)
3493                         return __connman_error_in_progress(msg);
3494
3495                 iter = g_sequence_iter_next(iter);
3496         }
3497
3498         service->ignore = FALSE;
3499
3500         service->userconnect = TRUE;
3501
3502         service->pending = dbus_message_ref(msg);
3503
3504         set_reconnect_state(service, FALSE);
3505
3506         err = __connman_service_connect(service);
3507         if (err < 0) {
3508                 if (service->pending == NULL)
3509                         return NULL;
3510
3511                 if (err != -EINPROGRESS) {
3512                         dbus_message_unref(service->pending);
3513                         service->pending = NULL;
3514
3515                         return __connman_error_failed(msg, -err);
3516                 }
3517
3518                 return NULL;
3519         }
3520
3521         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3522 }
3523
3524 static DBusMessage *disconnect_service(DBusConnection *conn,
3525                                         DBusMessage *msg, void *user_data)
3526 {
3527         struct connman_service *service = user_data;
3528         int err;
3529
3530         DBG("service %p", service);
3531
3532         reply_pending(service, ECONNABORTED);
3533
3534         service->ignore = TRUE;
3535
3536         set_reconnect_state(service, FALSE);
3537
3538         err = __connman_service_disconnect(service);
3539         if (err < 0) {
3540                 if (err != -EINPROGRESS)
3541                         return __connman_error_failed(msg, -err);
3542
3543                 return NULL;
3544         }
3545
3546         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3547 }
3548
3549 static DBusMessage *remove_service(DBusConnection *conn,
3550                                         DBusMessage *msg, void *user_data)
3551 {
3552         struct connman_service *service = user_data;
3553
3554         DBG("service %p", service);
3555
3556         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3557                 return __connman_error_not_supported(msg);
3558
3559         if (service->immutable == TRUE || service->hidden == TRUE)
3560                 return __connman_error_not_supported(msg);
3561
3562         if (service->favorite == FALSE && service->state !=
3563                                                 CONNMAN_SERVICE_STATE_FAILURE)
3564                 return __connman_error_not_supported(msg);
3565
3566         set_reconnect_state(service, FALSE);
3567
3568         __connman_service_disconnect(service);
3569
3570         g_free(service->passphrase);
3571         service->passphrase = NULL;
3572
3573         set_idle(service);
3574
3575         __connman_service_set_favorite(service, FALSE);
3576         service_save(service);
3577
3578         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3579 }
3580
3581 static gboolean check_suitable_state(enum connman_service_state a,
3582                                         enum connman_service_state b)
3583 {
3584         /*
3585          * Special check so that "ready" service can be moved before
3586          * "online" one.
3587          */
3588         if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3589                         b == CONNMAN_SERVICE_STATE_READY) ||
3590                 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3591                         a == CONNMAN_SERVICE_STATE_READY))
3592                 return TRUE;
3593
3594         return a == b;
3595 }
3596
3597 static void downgrade_state(struct connman_service *service)
3598 {
3599         if (service == NULL)
3600                 return;
3601
3602         DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3603                                                 service->state_ipv6);
3604
3605         if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3606                 __connman_service_ipconfig_indicate_state(service,
3607                                                 CONNMAN_SERVICE_STATE_READY,
3608                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
3609
3610         if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3611                 __connman_service_ipconfig_indicate_state(service,
3612                                                 CONNMAN_SERVICE_STATE_READY,
3613                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
3614 }
3615
3616 static void apply_relevant_default_downgrade(struct connman_service *service)
3617 {
3618         struct connman_service *def_service;
3619
3620         def_service = __connman_service_get_default();
3621         if (def_service == NULL)
3622                 return;
3623
3624         if (def_service == service &&
3625                         def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3626                 def_service->state = CONNMAN_SERVICE_STATE_READY;
3627 }
3628
3629 static void switch_default_service(struct connman_service *default_service,
3630                 struct connman_service *downgrade_service)
3631 {
3632         GSequenceIter *src, *dst;
3633
3634         apply_relevant_default_downgrade(default_service);
3635         src = g_hash_table_lookup(service_hash, downgrade_service->identifier);
3636         dst = g_hash_table_lookup(service_hash, default_service->identifier);
3637         g_sequence_move(src, dst);
3638         downgrade_state(downgrade_service);
3639 }
3640
3641 static DBusMessage *move_service(DBusConnection *conn,
3642                                         DBusMessage *msg, void *user_data,
3643                                                                 gboolean before)
3644 {
3645         struct connman_service *service = user_data;
3646         struct connman_service *target;
3647         const char *path;
3648         enum connman_ipconfig_method target4, target6;
3649         enum connman_ipconfig_method service4, service6;
3650
3651         DBG("service %p", service);
3652
3653         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3654                                                         DBUS_TYPE_INVALID);
3655
3656         if (service->favorite == FALSE)
3657                 return __connman_error_not_supported(msg);
3658
3659         target = find_service(path);
3660         if (target == NULL || target->favorite == FALSE || target == service)
3661                 return __connman_error_invalid_service(msg);
3662
3663         if (target->type == CONNMAN_SERVICE_TYPE_VPN) {
3664                 /*
3665                  * We only allow VPN route splitting if there are
3666                  * routes defined for a given VPN.
3667                  */
3668                 if (__connman_provider_check_routes(target->provider)
3669                                                                 == FALSE) {
3670                         connman_info("Cannot move service. "
3671                                 "No routes defined for provider %s",
3672                                 __connman_provider_get_ident(target->provider));
3673                         return __connman_error_invalid_service(msg);
3674                 }
3675
3676                 target->do_split_routing = TRUE;
3677         } else
3678                 target->do_split_routing = FALSE;
3679
3680         service->do_split_routing = FALSE;
3681
3682         target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3683         target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3684         service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3685         service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3686
3687         DBG("target %s method %d/%d state %d/%d split %d", target->identifier,
3688                 target4, target6, target->state_ipv4, target->state_ipv6,
3689                 target->do_split_routing);
3690
3691         DBG("service %s method %d/%d state %d/%d", service->identifier,
3692                                 service4, service6,
3693                                 service->state_ipv4, service->state_ipv6);
3694
3695         /*
3696          * If method is OFF, then we do not need to check the corresponding
3697          * ipconfig state.
3698          */
3699         if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3700                 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3701                         if (check_suitable_state(target->state_ipv6,
3702                                                 service->state_ipv6) == FALSE)
3703                                 return __connman_error_invalid_service(msg);
3704                 }
3705         }
3706
3707         if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3708                 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3709                         if (check_suitable_state(target->state_ipv4,
3710                                                 service->state_ipv4) == FALSE)
3711                                 return __connman_error_invalid_service(msg);
3712                 }
3713         }
3714
3715         if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3716                 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3717                         if (check_suitable_state(target->state_ipv6,
3718                                                 service->state_ipv6) == FALSE)
3719                                 return __connman_error_invalid_service(msg);
3720                 }
3721         }
3722
3723         if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3724                 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3725                         if (check_suitable_state(target->state_ipv4,
3726                                                 service->state_ipv4) == FALSE)
3727                                 return __connman_error_invalid_service(msg);
3728                 }
3729         }
3730
3731         g_get_current_time(&service->modified);
3732         service_save(service);
3733         service_save(target);
3734
3735         /*
3736          * If the service which goes down is the default service and is
3737          * online, we downgrade directly its state to ready so:
3738          * the service which goes up, needs to recompute its state which
3739          * is triggered via downgrading it - if relevant - to state ready.
3740          */
3741         if (before == TRUE)
3742                 switch_default_service(target, service);
3743         else
3744                 switch_default_service(service, target);
3745
3746         __connman_connection_update_gateway();
3747
3748         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3749 }
3750
3751 static DBusMessage *move_before(DBusConnection *conn,
3752                                         DBusMessage *msg, void *user_data)
3753 {
3754         return move_service(conn, msg, user_data, TRUE);
3755 }
3756
3757 static DBusMessage *move_after(DBusConnection *conn,
3758                                         DBusMessage *msg, void *user_data)
3759 {
3760         return move_service(conn, msg, user_data, FALSE);
3761 }
3762
3763 static DBusMessage *reset_counters(DBusConnection *conn,
3764                                         DBusMessage *msg, void *user_data)
3765 {
3766         struct connman_service *service = user_data;
3767
3768         reset_stats(service);
3769
3770         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3771 }
3772
3773 static struct _services_notify {
3774         int id;
3775         GHashTable *add;
3776         GHashTable *remove;
3777 } *services_notify;
3778
3779 static void service_append_added_foreach(gpointer data, gpointer user_data)
3780 {
3781         struct connman_service *service = data;
3782         DBusMessageIter *iter = user_data;
3783
3784         if (service == NULL || service->path == NULL) {
3785                 DBG("service %p or path is NULL", service);
3786                 return;
3787         }
3788
3789         if (g_hash_table_lookup(services_notify->add, service->path) != NULL) {
3790                 DBG("new %s", service->path);
3791
3792                 append_struct(service, iter);
3793                 g_hash_table_remove(services_notify->add, service->path);
3794         } else {
3795                 DBG("changed %s", service->path);
3796
3797                 append_struct_service(iter, NULL, service);
3798         }
3799 }
3800
3801 static void service_append_ordered(DBusMessageIter *iter, void *user_data)
3802 {
3803         if (service_list != NULL)
3804                 g_sequence_foreach(service_list,
3805                                         service_append_added_foreach, iter);
3806 }
3807
3808 static void append_removed(gpointer key, gpointer value, gpointer user_data)
3809 {
3810         char *objpath = key;
3811         DBusMessageIter *iter = user_data;
3812
3813         DBG("removed %s", objpath);
3814         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath);
3815 }
3816
3817 static gboolean service_send_changed(gpointer data)
3818 {
3819         DBusMessage *signal;
3820         DBusMessageIter iter, array;
3821
3822         DBG("");
3823
3824         signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
3825                         CONNMAN_MANAGER_INTERFACE, "ServicesChanged");
3826         if (signal == NULL)
3827                 return FALSE;
3828
3829         __connman_dbus_append_objpath_dict_array(signal,
3830                         service_append_ordered, NULL);
3831
3832         dbus_message_iter_init_append(signal, &iter);
3833         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
3834                         DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
3835
3836         g_hash_table_foreach(services_notify->remove, append_removed, &array);
3837
3838         dbus_message_iter_close_container(&iter, &array);
3839
3840         dbus_connection_send(connection, signal, NULL);
3841         dbus_message_unref(signal);
3842
3843         g_hash_table_remove_all(services_notify->remove);
3844         g_hash_table_remove_all(services_notify->add);
3845
3846         services_notify->id = 0;
3847         return FALSE;
3848 }
3849
3850 static void service_schedule_changed(void)
3851 {
3852         if (services_notify->id != 0)
3853                 return;
3854
3855         services_notify->id = g_timeout_add(100, service_send_changed, NULL);
3856 }
3857
3858 static void service_schedule_added(struct connman_service *service)
3859 {
3860         DBG("service %p", service);
3861
3862         g_hash_table_remove(services_notify->remove, service->path);
3863         g_hash_table_replace(services_notify->add, service->path, service);
3864
3865         service_schedule_changed();
3866 }
3867
3868 static void service_schedule_removed(struct connman_service *service)
3869 {
3870         DBG("service %p %s", service, service->path);
3871
3872         if (service == NULL || service->path == NULL) {
3873                 DBG("service %p or path is NULL", service);
3874                 return;
3875         }
3876
3877         g_hash_table_remove(services_notify->add, service->path);
3878         g_hash_table_replace(services_notify->remove, g_strdup(service->path),
3879                         NULL);
3880
3881         service_schedule_changed();
3882 }
3883
3884 static const GDBusMethodTable service_methods[] = {
3885         { GDBUS_DEPRECATED_METHOD("GetProperties",
3886                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
3887                         get_properties) },
3888         { GDBUS_METHOD("SetProperty",
3889                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
3890                         NULL, set_property) },
3891         { GDBUS_METHOD("ClearProperty",
3892                         GDBUS_ARGS({ "name", "s" }), NULL,
3893                         clear_property) },
3894         { GDBUS_ASYNC_METHOD("Connect", NULL, NULL,
3895                               connect_service) },
3896         { GDBUS_METHOD("Disconnect", NULL, NULL,
3897                         disconnect_service) },
3898         { GDBUS_METHOD("Remove", NULL, NULL, remove_service) },
3899         { GDBUS_METHOD("MoveBefore",
3900                         GDBUS_ARGS({ "service", "o" }), NULL,
3901                         move_before) },
3902         { GDBUS_METHOD("MoveAfter",
3903                         GDBUS_ARGS({ "service", "o" }), NULL,
3904                         move_after) },
3905         { GDBUS_METHOD("ResetCounters", NULL, NULL, reset_counters) },
3906         { },
3907 };
3908
3909 static const GDBusSignalTable service_signals[] = {
3910         { GDBUS_SIGNAL("PropertyChanged",
3911                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
3912         { },
3913 };
3914
3915 static void service_free(gpointer user_data)
3916 {
3917         struct connman_service *service = user_data;
3918         char *path = service->path;
3919
3920         DBG("service %p", service);
3921
3922         reply_pending(service, ENOENT);
3923
3924         g_hash_table_remove(service_hash, service->identifier);
3925
3926         __connman_notifier_service_remove(service);
3927         service_schedule_removed(service);
3928
3929         stats_stop(service);
3930
3931         service->path = NULL;
3932
3933         if (path != NULL) {
3934                 __connman_connection_update_gateway();
3935
3936                 g_dbus_unregister_interface(connection, path,
3937                                                 CONNMAN_SERVICE_INTERFACE);
3938                 g_free(path);
3939         }
3940
3941         g_hash_table_destroy(service->counter_table);
3942
3943         if (service->network != NULL) {
3944                 __connman_network_disconnect(service->network);
3945                 connman_network_unref(service->network);
3946                 service->network = NULL;
3947         }
3948
3949         if (service->provider != NULL)
3950                 connman_provider_unref(service->provider);
3951
3952         if (service->ipconfig_ipv4 != NULL) {
3953                 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3954                 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3955                 __connman_ipconfig_unref(service->ipconfig_ipv4);
3956                 service->ipconfig_ipv4 = NULL;
3957         }
3958
3959         if (service->ipconfig_ipv6 != NULL) {
3960                 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3961                 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3962                 __connman_ipconfig_unref(service->ipconfig_ipv6);
3963                 service->ipconfig_ipv6 = NULL;
3964         }
3965
3966         g_strfreev(service->timeservers);
3967         g_strfreev(service->timeservers_config);
3968         g_strfreev(service->nameservers);
3969         g_strfreev(service->nameservers_config);
3970         g_strfreev(service->nameservers_auto);
3971         g_strfreev(service->domains);
3972         g_strfreev(service->proxies);
3973         g_strfreev(service->excludes);
3974
3975         g_free(service->domainname);
3976         g_free(service->pac);
3977         g_free(service->name);
3978         g_free(service->passphrase);
3979         g_free(service->agent_passphrase);
3980         g_free(service->identifier);
3981         g_free(service->eap);
3982         g_free(service->identity);
3983         g_free(service->agent_identity);
3984         g_free(service->ca_cert_file);
3985         g_free(service->client_cert_file);
3986         g_free(service->private_key_file);
3987         g_free(service->private_key_passphrase);
3988         g_free(service->phase2);
3989
3990         if (service->stats.timer != NULL)
3991                 g_timer_destroy(service->stats.timer);
3992         if (service->stats_roaming.timer != NULL)
3993                 g_timer_destroy(service->stats_roaming.timer);
3994
3995         g_free(service);
3996 }
3997
3998 static void stats_init(struct connman_service *service)
3999 {
4000         /* home */
4001         service->stats.valid = FALSE;
4002         service->stats.enabled = FALSE;
4003         service->stats.timer = g_timer_new();
4004
4005         /* roaming */
4006         service->stats_roaming.valid = FALSE;
4007         service->stats_roaming.enabled = FALSE;
4008         service->stats_roaming.timer = g_timer_new();
4009 }
4010
4011 static void service_initialize(struct connman_service *service)
4012 {
4013         DBG("service %p", service);
4014
4015         service->refcount = 1;
4016         service->session_usage_count = 0;
4017
4018         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
4019         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
4020
4021         service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
4022         service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
4023         service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
4024
4025         service->favorite  = FALSE;
4026         service->immutable = FALSE;
4027         service->hidden = FALSE;
4028
4029         service->ignore = FALSE;
4030
4031         service->userconnect = FALSE;
4032
4033         service->order = 0;
4034
4035         stats_init(service);
4036
4037         service->provider = NULL;
4038
4039         service->wps = FALSE;
4040 }
4041
4042 /**
4043  * connman_service_create:
4044  *
4045  * Allocate a new service.
4046  *
4047  * Returns: a newly-allocated #connman_service structure
4048  */
4049 struct connman_service *connman_service_create(void)
4050 {
4051         GSList *list;
4052         struct connman_stats_counter *counters;
4053         const char *counter;
4054
4055         struct connman_service *service;
4056
4057         service = g_try_new0(struct connman_service, 1);
4058         if (service == NULL)
4059                 return NULL;
4060
4061         DBG("service %p", service);
4062
4063         service->counter_table = g_hash_table_new_full(g_str_hash,
4064                                                 g_str_equal, NULL, g_free);
4065
4066         for (list = counter_list; list; list = list->next) {
4067                 counter = list->data;
4068
4069                 counters = g_try_new0(struct connman_stats_counter, 1);
4070                 if (counters == NULL) {
4071                         g_hash_table_destroy(service->counter_table);
4072                         g_free(service);
4073                         return NULL;
4074                 }
4075
4076                 counters->append_all = TRUE;
4077
4078                 g_hash_table_replace(service->counter_table, (gpointer)counter,
4079                                 counters);
4080         }
4081
4082         service_initialize(service);
4083
4084         return service;
4085 }
4086
4087 /**
4088  * connman_service_ref:
4089  * @service: service structure
4090  *
4091  * Increase reference counter of service
4092  */
4093 struct connman_service *
4094 connman_service_ref_debug(struct connman_service *service,
4095                         const char *file, int line, const char *caller)
4096 {
4097         DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
4098                 file, line, caller);
4099
4100         __sync_fetch_and_add(&service->refcount, 1);
4101
4102         return service;
4103 }
4104
4105 /**
4106  * connman_service_unref:
4107  * @service: service structure
4108  *
4109  * Decrease reference counter of service and release service if no
4110  * longer needed.
4111  */
4112 void connman_service_unref_debug(struct connman_service *service,
4113                         const char *file, int line, const char *caller)
4114 {
4115         GSequenceIter *iter;
4116
4117         DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
4118                 file, line, caller);
4119
4120         if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
4121                 return;
4122
4123         iter = g_hash_table_lookup(service_hash, service->identifier);
4124         if (iter != NULL) {
4125                 reply_pending(service, ECONNABORTED);
4126
4127                 __connman_service_disconnect(service);
4128
4129                 g_sequence_remove(iter);
4130         } else {
4131                 service_free(service);
4132         }
4133 }
4134
4135 static gint service_compare(gconstpointer a, gconstpointer b,
4136                                                         gpointer user_data)
4137 {
4138         struct connman_service *service_a = (void *) a;
4139         struct connman_service *service_b = (void *) b;
4140         enum connman_service_state state_a, state_b;
4141
4142         state_a = service_a->state;
4143         state_b = service_b->state;
4144
4145         if (state_a != state_b) {
4146                 gboolean a_connected = is_connected(service_a);
4147                 gboolean b_connected = is_connected(service_b);
4148
4149                 if (a_connected == TRUE && b_connected == TRUE) {
4150                         /* We prefer online over ready state */
4151                         if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
4152                                 return -1;
4153
4154                         if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
4155                                 return 1;
4156                 }
4157
4158                 if (a_connected == TRUE)
4159                         return -1;
4160                 if (b_connected == TRUE)
4161                         return 1;
4162
4163                 if (is_connecting(service_a) == TRUE)
4164                         return -1;
4165                 if (is_connecting(service_b) == TRUE)
4166                         return 1;
4167         }
4168
4169         if (service_a->order > service_b->order)
4170                 return -1;
4171
4172         if (service_a->order < service_b->order)
4173                 return 1;
4174
4175         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
4176                 return -1;
4177
4178         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
4179                 return 1;
4180
4181         if (service_a->type != service_b->type) {
4182                 switch (service_a->type) {
4183                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4184                 case CONNMAN_SERVICE_TYPE_SYSTEM:
4185                 case CONNMAN_SERVICE_TYPE_ETHERNET:
4186                 case CONNMAN_SERVICE_TYPE_GPS:
4187                 case CONNMAN_SERVICE_TYPE_VPN:
4188                 case CONNMAN_SERVICE_TYPE_GADGET:
4189                         break;
4190                 case CONNMAN_SERVICE_TYPE_WIFI:
4191                         return 1;
4192                 case CONNMAN_SERVICE_TYPE_WIMAX:
4193                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4194                 case CONNMAN_SERVICE_TYPE_CELLULAR:
4195                         return -1;
4196                 }
4197         }
4198
4199         return (gint) service_b->strength - (gint) service_a->strength;
4200 }
4201
4202 /**
4203  * connman_service_get_type:
4204  * @service: service structure
4205  *
4206  * Get the type of service
4207  */
4208 enum connman_service_type connman_service_get_type(struct connman_service *service)
4209 {
4210         if (service == NULL)
4211                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4212
4213         return service->type;
4214 }
4215
4216 /**
4217  * connman_service_get_interface:
4218  * @service: service structure
4219  *
4220  * Get network interface of service
4221  */
4222 char *connman_service_get_interface(struct connman_service *service)
4223 {
4224         int index;
4225
4226         if (service == NULL)
4227                 return NULL;
4228
4229         if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
4230                 if (service->ipconfig_ipv4)
4231                         index = __connman_ipconfig_get_index(
4232                                                 service->ipconfig_ipv4);
4233                 else if (service->ipconfig_ipv6)
4234                         index = __connman_ipconfig_get_index(
4235                                                 service->ipconfig_ipv6);
4236                 else
4237                         return NULL;
4238
4239                 return connman_inet_ifname(index);
4240         }
4241
4242         if (service->network == NULL)
4243                 return NULL;
4244
4245         index = connman_network_get_index(service->network);
4246
4247         return connman_inet_ifname(index);
4248 }
4249
4250 /**
4251  * connman_service_get_network:
4252  * @service: service structure
4253  *
4254  * Get the service network
4255  */
4256 struct connman_network *
4257 __connman_service_get_network(struct connman_service *service)
4258 {
4259         if (service == NULL)
4260                 return NULL;
4261
4262         return service->network;
4263 }
4264
4265 struct connman_ipconfig *
4266 __connman_service_get_ip4config(struct connman_service *service)
4267 {
4268         if (service == NULL)
4269                 return NULL;
4270
4271         return service->ipconfig_ipv4;
4272 }
4273
4274 struct connman_ipconfig *
4275 __connman_service_get_ip6config(struct connman_service *service)
4276 {
4277         if (service == NULL)
4278                 return NULL;
4279
4280         return service->ipconfig_ipv6;
4281 }
4282
4283 struct connman_ipconfig *
4284 __connman_service_get_ipconfig(struct connman_service *service, int family)
4285 {
4286         if (family == AF_INET)
4287                 return __connman_service_get_ip4config(service);
4288         else if (family == AF_INET6)
4289                 return __connman_service_get_ip6config(service);
4290         else
4291                 return NULL;
4292
4293 }
4294
4295 connman_bool_t __connman_service_is_connected_state(struct connman_service *service,
4296                                         enum connman_ipconfig_type type)
4297 {
4298         if (service == NULL)
4299                 return FALSE;
4300
4301         switch (type) {
4302         case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
4303                 break;
4304         case CONNMAN_IPCONFIG_TYPE_IPV4:
4305                 return is_connected_state(service, service->state_ipv4);
4306         case CONNMAN_IPCONFIG_TYPE_IPV6:
4307                 return is_connected_state(service, service->state_ipv6);
4308         }
4309
4310         return FALSE;
4311 }
4312 enum connman_service_security __connman_service_get_security(struct connman_service *service)
4313 {
4314         if (service == NULL)
4315                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4316
4317         return service->security;
4318 }
4319
4320 const char *__connman_service_get_phase2(struct connman_service *service)
4321 {
4322         if (service == NULL)
4323                 return NULL;
4324
4325         return service->phase2;
4326 }
4327
4328 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
4329 {
4330         if (service == NULL)
4331                 return FALSE;
4332
4333         return service->wps;
4334 }
4335
4336 /**
4337  * __connman_service_set_favorite:
4338  * @service: service structure
4339  * @favorite: favorite value
4340  *
4341  * Change the favorite setting of service
4342  */
4343 int __connman_service_set_favorite(struct connman_service *service,
4344                                                 connman_bool_t favorite)
4345 {
4346         GSequenceIter *iter;
4347
4348         if (service->hidden == TRUE)
4349                 return -EOPNOTSUPP;
4350         iter = g_hash_table_lookup(service_hash, service->identifier);
4351         if (iter == NULL)
4352                 return -ENOENT;
4353
4354         if (service->favorite == favorite)
4355                 return -EALREADY;
4356
4357         service->favorite = favorite;
4358         service->order = __connman_service_get_order(service);
4359
4360         favorite_changed(service);
4361
4362         if (g_sequence_get_length(service_list) > 1) {
4363                 g_sequence_sort_changed(iter, service_compare, NULL);
4364                 service_schedule_changed();
4365         }
4366
4367         __connman_connection_update_gateway();
4368
4369         return 0;
4370 }
4371
4372 int __connman_service_set_immutable(struct connman_service *service,
4373                                                 connman_bool_t immutable)
4374 {
4375         if (service->hidden == TRUE)
4376                 return -EOPNOTSUPP;
4377         service->immutable = immutable;
4378
4379         immutable_changed(service);
4380
4381         return 0;
4382 }
4383
4384 void __connman_service_set_string(struct connman_service *service,
4385                                   const char *key, const char *value)
4386 {
4387         if (service->hidden == TRUE)
4388                 return;
4389         if (g_str_equal(key, "EAP") == TRUE) {
4390                 g_free(service->eap);
4391                 service->eap = g_strdup(value);
4392         } else if (g_str_equal(key, "Identity") == TRUE) {
4393                 g_free(service->identity);
4394                 service->identity = g_strdup(value);
4395         } else if (g_str_equal(key, "CACertFile") == TRUE) {
4396                 g_free(service->ca_cert_file);
4397                 service->ca_cert_file = g_strdup(value);
4398         } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
4399                 g_free(service->client_cert_file);
4400                 service->client_cert_file = g_strdup(value);
4401         } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
4402                 g_free(service->private_key_file);
4403                 service->private_key_file = g_strdup(value);
4404         } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
4405                 g_free(service->private_key_passphrase);
4406                 service->private_key_passphrase = g_strdup(value);
4407         } else if (g_str_equal(key, "Phase2") == TRUE) {
4408                 g_free(service->phase2);
4409                 service->phase2 = g_strdup(value);
4410         } else if (g_str_equal(key, "Passphrase") == TRUE) {
4411                 g_free(service->passphrase);
4412                 service->passphrase = g_strdup(value);
4413         }
4414 }
4415
4416 void __connman_service_set_userconnect(struct connman_service *service,
4417                                                 connman_bool_t userconnect)
4418 {
4419         if (service != NULL)
4420                 service->userconnect = userconnect;
4421 }
4422
4423 static void service_complete(struct connman_service *service)
4424 {
4425         reply_pending(service, EIO);
4426
4427         if (service->userconnect == FALSE)
4428                 __connman_service_auto_connect();
4429
4430         g_get_current_time(&service->modified);
4431         service_save(service);
4432 }
4433
4434 static void report_error_cb(struct connman_service *service,
4435                         gboolean retry, void *user_data)
4436 {
4437         if (retry == TRUE)
4438                 __connman_service_connect(service);
4439         else {
4440                 /* It is not relevant to stay on Failure state
4441                  * when failing is due to wrong user input */
4442                 service->state = CONNMAN_SERVICE_STATE_IDLE;
4443
4444                 service_complete(service);
4445                 __connman_connection_update_gateway();
4446         }
4447 }
4448
4449 int __connman_service_add_passphrase(struct connman_service *service,
4450                                 const gchar *passphrase)
4451 {
4452         int err = 0;
4453
4454         switch (service->security) {
4455         case CONNMAN_SERVICE_SECURITY_WEP:
4456         case CONNMAN_SERVICE_SECURITY_PSK:
4457                 err = __connman_service_set_passphrase(service, passphrase);
4458                 break;
4459         case CONNMAN_SERVICE_SECURITY_8021X:
4460                 __connman_service_set_agent_passphrase(service,
4461                                                 passphrase);
4462                 break;
4463         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4464         case CONNMAN_SERVICE_SECURITY_NONE:
4465         case CONNMAN_SERVICE_SECURITY_WPA:
4466         case CONNMAN_SERVICE_SECURITY_RSN:
4467                 DBG("service security '%s' (%d) not handled",
4468                                 security2string(service->security),
4469                                 service->security);
4470                 break;
4471         }
4472
4473         return err;
4474 }
4475
4476 static int check_wpspin(struct connman_service *service, const char *wpspin)
4477 {
4478         int length;
4479         guint i;
4480
4481         if (wpspin == NULL)
4482                 return 0;
4483
4484         length = strlen(wpspin);
4485
4486         /* If 0, it will mean user wants to use PBC method */
4487         if (length == 0) {
4488                 connman_network_set_string(service->network,
4489                                                         "WiFi.PinWPS", NULL);
4490                 return 0;
4491         }
4492
4493         /* A WPS PIN is always 8 chars length,
4494          * its content is in digit representation.
4495          */
4496         if (length != 8)
4497                 return -ENOKEY;
4498
4499         for (i = 0; i < 8; i++)
4500                 if (!isdigit((unsigned char) wpspin[i]))
4501                         return -ENOKEY;
4502
4503         connman_network_set_string(service->network, "WiFi.PinWPS", wpspin);
4504
4505         return 0;
4506 }
4507
4508 static void request_input_cb (struct connman_service *service,
4509                         connman_bool_t values_received,
4510                         const char *name, int name_len,
4511                         const char *identity, const char *passphrase,
4512                         gboolean wps, const char *wpspin,
4513                         const char *error, void *user_data)
4514 {
4515         struct connman_device *device;
4516         int err = 0;
4517
4518         DBG ("RequestInput return, %p", service);
4519
4520         if (error != NULL) {
4521                 DBG("error: %s", error);
4522
4523                 if (g_strcmp0(error,
4524                                 "net.connman.Agent.Error.Canceled") == 0) {
4525                         err = -EINVAL;
4526                         goto done;
4527                 }
4528         }
4529
4530         if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
4531                 device = connman_network_get_device(service->network);
4532                 __connman_device_request_hidden_scan(device,
4533                                                 name, name_len,
4534                                                 identity, passphrase);
4535         }
4536
4537         if (values_received == FALSE || service->hidden == TRUE) {
4538                 err = -EINVAL;
4539                 goto done;
4540         }
4541
4542         if (wps == TRUE && service->network != NULL) {
4543                 err = check_wpspin(service, wpspin);
4544                 if (err < 0)
4545                         goto done;
4546
4547                 connman_network_set_bool(service->network, "WiFi.UseWPS", wps);
4548         }
4549
4550         if (identity != NULL)
4551                 __connman_service_set_agent_identity(service, identity);
4552
4553         if (passphrase != NULL)
4554                 err = __connman_service_add_passphrase(service, passphrase);
4555
4556  done:
4557         if (err >= 0) {
4558                 __connman_service_connect(service);
4559
4560                 /* Never cache agent provided credentials */
4561                 __connman_service_set_agent_identity(service, NULL);
4562                 __connman_service_set_agent_passphrase(service, NULL);
4563         } else if (err == -ENOKEY) {
4564                 __connman_service_indicate_error(service,
4565                                         CONNMAN_SERVICE_ERROR_INVALID_KEY);
4566                 __connman_agent_report_error(service,
4567                                         error2string(service->error),
4568                                         report_error_cb, NULL);
4569         } else {
4570                 /* It is not relevant to stay on Failure state
4571                  * when failing is due to wrong user input */
4572                 service->state = CONNMAN_SERVICE_STATE_IDLE;
4573
4574                 service_complete(service);
4575                 __connman_connection_update_gateway();
4576         }
4577 }
4578
4579 static void downgrade_connected_services(void)
4580 {
4581         struct connman_service *up_service;
4582         GSequenceIter *iter;
4583
4584         iter = g_sequence_get_begin_iter(service_list);
4585         while (g_sequence_iter_is_end(iter) == FALSE) {
4586                 up_service = g_sequence_get(iter);
4587
4588                 if (is_connected(up_service) == FALSE) {
4589                         iter = g_sequence_iter_next(iter);
4590                         continue;
4591                 }
4592
4593                 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4594                         return;
4595
4596                 downgrade_state(up_service);
4597
4598                 iter = g_sequence_iter_next(iter);
4599         }
4600 }
4601
4602 static int service_update_preferred_order(struct connman_service *default_service,
4603                 struct connman_service *new_service,
4604                 enum connman_service_state new_state)
4605 {
4606         unsigned int *tech_array;
4607         int i;
4608
4609         if (default_service == NULL || default_service == new_service ||
4610                         default_service->state != new_state )
4611                 return 0;
4612
4613         tech_array = connman_setting_get_uint_list("PreferredTechnologies");
4614         if (tech_array != NULL) {
4615
4616                 for (i = 0; tech_array[i] != 0; i += 1) {
4617                         if (default_service->type == tech_array[i])
4618                                 return -EALREADY;
4619
4620                         if (new_service->type == tech_array[i]) {
4621                                 switch_default_service(new_service,
4622                                                 default_service);
4623                                 return 0;
4624                         }
4625                 }
4626                 return -EAGAIN;
4627         }
4628
4629         return -EALREADY;
4630 }
4631
4632 static int service_indicate_state(struct connman_service *service)
4633 {
4634         enum connman_service_state old_state, new_state;
4635         struct connman_service *def_service;
4636         int result;
4637         GSequenceIter *iter;
4638
4639         if (service == NULL)
4640                 return -EINVAL;
4641
4642         old_state = service->state;
4643         new_state = combine_state(service->state_ipv4, service->state_ipv6);
4644
4645         DBG("service %p old %s - new %s/%s => %s",
4646                                         service,
4647                                         state2string(old_state),
4648                                         state2string(service->state_ipv4),
4649                                         state2string(service->state_ipv6),
4650                                         state2string(new_state));
4651
4652         if (old_state == new_state)
4653                 return -EALREADY;
4654
4655         def_service = __connman_service_get_default();
4656
4657         if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4658                 result = service_update_preferred_order(def_service,
4659                                 service, new_state);
4660                 if (result == -EALREADY)
4661                         return result;
4662                 if (result == -EAGAIN)
4663                         __connman_service_auto_connect();
4664         }
4665
4666         if (old_state == CONNMAN_SERVICE_STATE_ONLINE)
4667                 __connman_notifier_leave_online(service->type);
4668
4669         service->state = new_state;
4670         state_changed(service);
4671
4672         if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4673                         old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4674                 reply_pending(service, ECONNABORTED);
4675
4676                 __connman_service_disconnect(service);
4677         }
4678
4679         if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4680                 if (service->new_service == FALSE &&
4681                                 __connman_stats_service_register(service) == 0) {
4682                         /*
4683                          * For new services the statistics are updated after
4684                          * we have successfully connected.
4685                          */
4686                         __connman_stats_get(service, FALSE,
4687                                                 &service->stats.data);
4688                         __connman_stats_get(service, TRUE,
4689                                                 &service->stats_roaming.data);
4690                 }
4691         }
4692
4693         if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4694                 connman_bool_t reconnect;
4695
4696                 reconnect = get_reconnect_state(service);
4697                 if (reconnect == TRUE)
4698                         __connman_service_auto_connect();
4699         }
4700
4701         if (new_state == CONNMAN_SERVICE_STATE_READY) {
4702                 enum connman_ipconfig_method method;
4703
4704                 if (service->new_service == TRUE &&
4705                                 __connman_stats_service_register(service) == 0) {
4706                         /*
4707                          * This is normally done after configuring state
4708                          * but for new service do this after we have connected
4709                          * successfully.
4710                          */
4711                         __connman_stats_get(service, FALSE,
4712                                                 &service->stats.data);
4713                         __connman_stats_get(service, TRUE,
4714                                                 &service->stats_roaming.data);
4715                 }
4716
4717                 service->new_service = FALSE;
4718
4719                 service_update_preferred_order(def_service, service, new_state);
4720
4721                 set_reconnect_state(service, TRUE);
4722
4723                 __connman_service_set_favorite(service, TRUE);
4724
4725                 reply_pending(service, 0);
4726
4727                 service->userconnect = FALSE;
4728
4729                 g_get_current_time(&service->modified);
4730                 service_save(service);
4731
4732                 update_nameservers(service);
4733                 dns_changed(service);
4734                 domain_changed(service);
4735
4736                 __connman_notifier_connect(service->type);
4737
4738                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4739                         connman_network_get_bool(service->network,
4740                                                 "WiFi.UseWPS") == TRUE) {
4741                         const char *pass;
4742
4743                         pass = connman_network_get_string(service->network,
4744                                                         "WiFi.Passphrase");
4745
4746                         __connman_service_set_passphrase(service, pass);
4747
4748                         connman_network_set_bool(service->network,
4749                                                         "WiFi.UseWPS", FALSE);
4750                 }
4751
4752                 default_changed();
4753
4754                 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4755                 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4756                         __connman_ipconfig_disable_ipv6(
4757                                                 service->ipconfig_ipv6);
4758
4759         } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4760                 def_service = __connman_service_get_default();
4761
4762                 if (__connman_notifier_is_connected() == FALSE &&
4763                         def_service != NULL &&
4764                                 def_service->provider != NULL)
4765                         __connman_provider_disconnect(def_service->provider);
4766
4767                 default_changed();
4768
4769                 __connman_wispr_stop(service);
4770
4771                 __connman_wpad_stop(service);
4772
4773                 update_nameservers(service);
4774                 dns_changed(service);
4775                 domain_changed(service);
4776
4777                 __connman_notifier_disconnect(service->type);
4778
4779                 /*
4780                  * Previous services which are connected and which states
4781                  * are set to online should reset relevantly ipconfig_state
4782                  * to ready so wispr/portal will be rerun on those
4783                  */
4784                 downgrade_connected_services();
4785         }
4786
4787         if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4788                 if (service->userconnect == TRUE &&
4789                         __connman_agent_report_error(service,
4790                                         error2string(service->error),
4791                                         report_error_cb, NULL) == -EINPROGRESS)
4792                         return 0;
4793                 service_complete(service);
4794         } else
4795                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4796
4797         iter = g_hash_table_lookup(service_hash, service->identifier);
4798         if (iter != NULL && g_sequence_get_length(service_list) > 1) {
4799                 g_sequence_sort_changed(iter, service_compare, NULL);
4800                 service_schedule_changed();
4801         }
4802
4803         __connman_connection_update_gateway();
4804
4805         if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4806                 __connman_notifier_enter_online(service->type);
4807                 default_changed();
4808         }
4809
4810         return 0;
4811 }
4812
4813 int __connman_service_indicate_error(struct connman_service *service,
4814                                         enum connman_service_error error)
4815 {
4816         DBG("service %p error %d", service, error);
4817
4818         if (service == NULL)
4819                 return -EINVAL;
4820
4821         service->error = error;
4822
4823         if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4824                 __connman_service_set_passphrase(service, NULL);
4825
4826         __connman_service_ipconfig_indicate_state(service,
4827                                                 CONNMAN_SERVICE_STATE_FAILURE,
4828                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4829         __connman_service_ipconfig_indicate_state(service,
4830                                                 CONNMAN_SERVICE_STATE_FAILURE,
4831                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4832         return 0;
4833 }
4834
4835 int __connman_service_clear_error(struct connman_service *service)
4836 {
4837         DBG("service %p", service);
4838
4839         if (service == NULL)
4840                 return -EINVAL;
4841
4842         if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4843                 return -EINVAL;
4844
4845         service->state_ipv4 = service->state_ipv6 =
4846                                                 CONNMAN_SERVICE_STATE_UNKNOWN;
4847         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4848
4849         if (service->favorite == TRUE)
4850                 set_reconnect_state(service, TRUE);
4851
4852         __connman_service_ipconfig_indicate_state(service,
4853                                         CONNMAN_SERVICE_STATE_IDLE,
4854                                         CONNMAN_IPCONFIG_TYPE_IPV6);
4855
4856         /*
4857          * Toggling the IPv6 state to IDLE could trigger the auto connect
4858          * machinery and consequently the IPv4 state.
4859          */
4860         if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4861                         service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4862                 return 0;
4863
4864         return __connman_service_ipconfig_indicate_state(service,
4865                                                 CONNMAN_SERVICE_STATE_IDLE,
4866                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4867 }
4868
4869 int __connman_service_indicate_default(struct connman_service *service)
4870 {
4871         DBG("service %p", service);
4872
4873         default_changed();
4874
4875         return 0;
4876 }
4877
4878 enum connman_service_state __connman_service_ipconfig_get_state(
4879                                         struct connman_service *service,
4880                                         enum connman_ipconfig_type type)
4881 {
4882         if (service == NULL)
4883                 return CONNMAN_SERVICE_STATE_UNKNOWN;
4884
4885         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4886                 return service->state_ipv4;
4887
4888         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4889                 return service->state_ipv6;
4890
4891         return CONNMAN_SERVICE_STATE_UNKNOWN;
4892 }
4893
4894 static void check_proxy_setup(struct connman_service *service)
4895 {
4896         /*
4897          * We start WPAD if we haven't got a PAC URL from DHCP and
4898          * if our proxy manual configuration is either empty or set
4899          * to AUTO with an empty URL.
4900          */
4901
4902         if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4903                 goto done;
4904
4905         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4906                 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4907                         service->pac != NULL))
4908                 goto done;
4909
4910         if (__connman_wpad_start(service) < 0) {
4911                 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4912                 __connman_notifier_proxy_changed(service);
4913                 goto done;
4914         }
4915
4916         return;
4917
4918 done:
4919         __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4920 }
4921
4922 /*
4923  * How many networks are connected at the same time. If more than 1,
4924  * then set the rp_filter setting properly (loose mode routing) so that network
4925  * connectivity works ok. This is only done for IPv4 networks as IPv6
4926  * does not have rp_filter knob.
4927  */
4928 static int connected_networks_count;
4929 static int original_rp_filter;
4930
4931 static void service_rp_filter(struct connman_service *service,
4932                                 gboolean connected)
4933 {
4934         enum connman_ipconfig_method method;
4935
4936         method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4937
4938         switch (method) {
4939         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4940         case CONNMAN_IPCONFIG_METHOD_OFF:
4941         case CONNMAN_IPCONFIG_METHOD_AUTO:
4942                 return;
4943         case CONNMAN_IPCONFIG_METHOD_FIXED:
4944         case CONNMAN_IPCONFIG_METHOD_MANUAL:
4945         case CONNMAN_IPCONFIG_METHOD_DHCP:
4946                 break;
4947         }
4948
4949         if (connected == TRUE) {
4950                 if (connected_networks_count == 1) {
4951                         int filter_value;
4952                         filter_value = __connman_ipconfig_set_rp_filter();
4953                         if (filter_value < 0)
4954                                 return;
4955
4956                         original_rp_filter = filter_value;
4957                 }
4958                 connected_networks_count++;
4959
4960         } else {
4961                 if (connected_networks_count == 2)
4962                         __connman_ipconfig_unset_rp_filter(original_rp_filter);
4963
4964                 connected_networks_count--;
4965                 if (connected_networks_count < 0)
4966                         connected_networks_count = 0;
4967         }
4968
4969         DBG("%s %s ipconfig %p method %d count %d filter %d",
4970                 connected ? "connected" : "disconnected", service->identifier,
4971                 service->ipconfig_ipv4, method,
4972                 connected_networks_count, original_rp_filter);
4973 }
4974
4975 static gboolean redo_wispr(gpointer user_data)
4976 {
4977         struct connman_service *service = user_data;
4978
4979         DBG("");
4980
4981         __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
4982
4983         return FALSE;
4984 }
4985
4986 int __connman_service_online_check_failed(struct connman_service *service,
4987                                         enum connman_ipconfig_type type)
4988 {
4989         DBG("service %p type %d count %d", service, type,
4990                                                 service->online_check_count);
4991
4992         /* currently we only retry IPv6 stuff */
4993         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 ||
4994                         service->online_check_count != 1) {
4995                 connman_warn("Online check failed for %p %s", service,
4996                         service->name);
4997                 return 0;
4998         }
4999
5000         service->online_check_count = 0;
5001
5002         /*
5003          * We set the timeout to 1 sec so that we have a chance to get
5004          * necessary IPv6 router advertisement messages that might have
5005          * DNS data etc.
5006          */
5007         g_timeout_add_seconds(1, redo_wispr, service);
5008
5009         return EAGAIN;
5010 }
5011
5012 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
5013                                         enum connman_service_state new_state,
5014                                         enum connman_ipconfig_type type)
5015 {
5016         struct connman_ipconfig *ipconfig = NULL;
5017         enum connman_service_state old_state;
5018         int ret;
5019
5020         if (service == NULL)
5021                 return -EINVAL;
5022
5023         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5024                 old_state = service->state_ipv4;
5025                 ipconfig = service->ipconfig_ipv4;
5026         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
5027                 old_state = service->state_ipv6;
5028                 ipconfig = service->ipconfig_ipv6;
5029         }
5030
5031         if (ipconfig == NULL)
5032                 return -EINVAL;
5033
5034         /* Any change? */
5035         if (old_state == new_state)
5036                 return -EALREADY;
5037
5038         DBG("service %p (%s) state %d (%s) type %d (%s)",
5039                 service, service ? service->identifier : NULL,
5040                 new_state, state2string(new_state),
5041                 type, __connman_ipconfig_type2string(type));
5042
5043         switch (new_state) {
5044         case CONNMAN_SERVICE_STATE_UNKNOWN:
5045         case CONNMAN_SERVICE_STATE_IDLE:
5046                 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
5047                         return -EINVAL;
5048                 break;
5049         case CONNMAN_SERVICE_STATE_ASSOCIATION:
5050                 break;
5051         case CONNMAN_SERVICE_STATE_CONFIGURATION:
5052                 __connman_ipconfig_enable(ipconfig);
5053                 break;
5054         case CONNMAN_SERVICE_STATE_READY:
5055                 update_nameservers(service);
5056
5057                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5058                         check_proxy_setup(service);
5059                         service_rp_filter(service, TRUE);
5060                 } else {
5061                         service->online_check_count = 1;
5062                         __connman_wispr_start(service, type);
5063                 }
5064                 break;
5065         case CONNMAN_SERVICE_STATE_ONLINE:
5066                 break;
5067         case CONNMAN_SERVICE_STATE_DISCONNECT:
5068                 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
5069                         return -EINVAL;
5070
5071                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
5072                         service_rp_filter(service, FALSE);
5073
5074                 break;
5075         case CONNMAN_SERVICE_STATE_FAILURE:
5076                 break;
5077         }
5078
5079         /* We keep that state */
5080         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
5081                 service->state_ipv4 = new_state;
5082         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
5083                 service->state_ipv6 = new_state;
5084
5085         ret = service_indicate_state(service);
5086
5087         /*
5088          * If the ipconfig method is OFF, then we set the state to IDLE
5089          * so that it will not affect the combined state in the future.
5090          */
5091         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5092                 enum connman_ipconfig_method method;
5093                 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
5094                 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
5095                                 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
5096                         service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
5097                         ret = service_indicate_state(service);
5098                 }
5099
5100         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
5101                 enum connman_ipconfig_method method;
5102                 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
5103                 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
5104                                 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
5105                         service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5106                         ret = service_indicate_state(service);
5107                 }
5108         }
5109
5110         return ret;
5111 }
5112
5113 static connman_bool_t prepare_network(struct connman_service *service)
5114 {
5115         enum connman_network_type type;
5116         unsigned int ssid_len;
5117
5118         type = connman_network_get_type(service->network);
5119
5120         switch (type) {
5121         case CONNMAN_NETWORK_TYPE_UNKNOWN:
5122         case CONNMAN_NETWORK_TYPE_VENDOR:
5123                 return FALSE;
5124         case CONNMAN_NETWORK_TYPE_WIFI:
5125                 if (connman_network_get_blob(service->network, "WiFi.SSID",
5126                                                         &ssid_len) == NULL)
5127                         return FALSE;
5128
5129                 if (service->passphrase != NULL)
5130                         connman_network_set_string(service->network,
5131                                 "WiFi.Passphrase", service->passphrase);
5132                 break;
5133         case CONNMAN_NETWORK_TYPE_ETHERNET:
5134         case CONNMAN_NETWORK_TYPE_WIMAX:
5135         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5136         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5137         case CONNMAN_NETWORK_TYPE_CELLULAR:
5138                 break;
5139         }
5140
5141         return TRUE;
5142 }
5143
5144 static void prepare_8021x(struct connman_service *service)
5145 {
5146         if (service->eap != NULL)
5147                 connman_network_set_string(service->network, "WiFi.EAP",
5148                                                                 service->eap);
5149
5150         if (service->identity != NULL)
5151                 connman_network_set_string(service->network, "WiFi.Identity",
5152                                                         service->identity);
5153
5154         if (service->ca_cert_file != NULL)
5155                 connman_network_set_string(service->network, "WiFi.CACertFile",
5156                                                         service->ca_cert_file);
5157
5158         if (service->client_cert_file != NULL)
5159                 connman_network_set_string(service->network,
5160                                                 "WiFi.ClientCertFile",
5161                                                 service->client_cert_file);
5162
5163         if (service->private_key_file != NULL)
5164                 connman_network_set_string(service->network,
5165                                                 "WiFi.PrivateKeyFile",
5166                                                 service->private_key_file);
5167
5168         if (service->private_key_passphrase != NULL)
5169                 connman_network_set_string(service->network,
5170                                         "WiFi.PrivateKeyPassphrase",
5171                                         service->private_key_passphrase);
5172
5173         if (service->phase2 != NULL)
5174                 connman_network_set_string(service->network, "WiFi.Phase2",
5175                                                         service->phase2);
5176 }
5177
5178 static int service_connect(struct connman_service *service)
5179 {
5180         int err;
5181
5182         if (service->hidden == TRUE)
5183                 return -EPERM;
5184
5185         switch (service->type) {
5186         case CONNMAN_SERVICE_TYPE_UNKNOWN:
5187         case CONNMAN_SERVICE_TYPE_SYSTEM:
5188         case CONNMAN_SERVICE_TYPE_GPS:
5189         case CONNMAN_SERVICE_TYPE_GADGET:
5190                 return -EINVAL;
5191         case CONNMAN_SERVICE_TYPE_ETHERNET:
5192         case CONNMAN_SERVICE_TYPE_WIMAX:
5193         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5194         case CONNMAN_SERVICE_TYPE_CELLULAR:
5195         case CONNMAN_SERVICE_TYPE_VPN:
5196                 break;
5197         case CONNMAN_SERVICE_TYPE_WIFI:
5198                 switch (service->security) {
5199                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5200                 case CONNMAN_SERVICE_SECURITY_NONE:
5201                         break;
5202                 case CONNMAN_SERVICE_SECURITY_WEP:
5203                 case CONNMAN_SERVICE_SECURITY_PSK:
5204                 case CONNMAN_SERVICE_SECURITY_WPA:
5205                 case CONNMAN_SERVICE_SECURITY_RSN:
5206                         if (service->passphrase == NULL) {
5207                                 if (service->network == NULL)
5208                                         return -EOPNOTSUPP;
5209
5210                                 if (service->wps == FALSE ||
5211                                         connman_network_get_bool(
5212                                                         service->network,
5213                                                         "WiFi.UseWPS") == FALSE)
5214                                         return -ENOKEY;
5215                         }
5216                         break;
5217                 case CONNMAN_SERVICE_SECURITY_8021X:
5218                         if (service->eap == NULL)
5219                                 return -EINVAL;
5220
5221                         /*
5222                          * never request credentials if using EAP-TLS
5223                          * (EAP-TLS networks need to be fully provisioned)
5224                          */
5225                         if (g_str_equal(service->eap, "tls") == TRUE)
5226                                 break;
5227
5228                         /*
5229                          * Return -ENOKEY if either identity or passphrase is
5230                          * missing. Agent provided credentials can be used as
5231                          * fallback if needed.
5232                          */
5233                         if ((service->identity == NULL &&
5234                                         service->agent_identity == NULL) ||
5235                                         (service->passphrase == NULL &&
5236                                         service->agent_passphrase == NULL))
5237                                 return -ENOKEY;
5238
5239                         break;
5240                 }
5241                 break;
5242         }
5243
5244         if (service->network != NULL) {
5245                 if (prepare_network(service) == FALSE)
5246                         return -EINVAL;
5247
5248                 switch (service->security) {
5249                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5250                 case CONNMAN_SERVICE_SECURITY_NONE:
5251                 case CONNMAN_SERVICE_SECURITY_WEP:
5252                 case CONNMAN_SERVICE_SECURITY_PSK:
5253                 case CONNMAN_SERVICE_SECURITY_WPA:
5254                 case CONNMAN_SERVICE_SECURITY_RSN:
5255                         break;
5256                 case CONNMAN_SERVICE_SECURITY_8021X:
5257                         prepare_8021x(service);
5258                         break;
5259                 }
5260
5261                 if (__connman_stats_service_register(service) == 0) {
5262                         __connman_stats_get(service, FALSE,
5263                                                 &service->stats.data);
5264                         __connman_stats_get(service, TRUE,
5265                                                 &service->stats_roaming.data);
5266                 }
5267
5268                 if (service->ipconfig_ipv4)
5269                         __connman_ipconfig_enable(service->ipconfig_ipv4);
5270                 if (service->ipconfig_ipv6)
5271                         __connman_ipconfig_enable(service->ipconfig_ipv6);
5272
5273                 err = __connman_network_connect(service->network);
5274         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5275                                         service->provider != NULL)
5276                 err = __connman_provider_connect(service->provider);
5277         else
5278                 return -EOPNOTSUPP;
5279
5280         if (err < 0) {
5281                 if (err != -EINPROGRESS) {
5282                         __connman_ipconfig_disable(service->ipconfig_ipv4);
5283                         __connman_ipconfig_disable(service->ipconfig_ipv6);
5284                         __connman_stats_service_unregister(service);
5285                 }
5286         }
5287
5288         return err;
5289 }
5290
5291
5292 int __connman_service_connect(struct connman_service *service)
5293 {
5294         int err;
5295
5296         DBG("service %p state %s", service, state2string(service->state));
5297
5298         if (is_connected(service) == TRUE)
5299                 return -EISCONN;
5300
5301         if (is_connecting(service) == TRUE)
5302                 return -EALREADY;
5303
5304         switch (service->type) {
5305         case CONNMAN_SERVICE_TYPE_UNKNOWN:
5306         case CONNMAN_SERVICE_TYPE_SYSTEM:
5307         case CONNMAN_SERVICE_TYPE_GPS:
5308         case CONNMAN_SERVICE_TYPE_GADGET:
5309                 return -EINVAL;
5310         default:
5311                 err = service_connect(service);
5312         }
5313
5314         if (err >= 0)
5315                 return 0;
5316
5317         if (err == -EINPROGRESS) {
5318                 if (service->timeout == 0)
5319                         service->timeout = g_timeout_add_seconds(
5320                                 CONNECT_TIMEOUT, connect_timeout, service);
5321
5322                 return -EINPROGRESS;
5323         }
5324
5325         if (service->network != NULL)
5326                 __connman_network_disconnect(service->network);
5327         else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5328                                 service->provider != NULL)
5329                         __connman_provider_disconnect(service->provider);
5330
5331         if (service->userconnect == TRUE) {
5332                 if (err == -ENOKEY || err == -EPERM) {
5333                         return __connman_agent_request_passphrase_input(service,
5334                                         request_input_cb, NULL);
5335                 }
5336                 reply_pending(service, -err);
5337         }
5338
5339         return err;
5340 }
5341
5342 int __connman_service_disconnect(struct connman_service *service)
5343 {
5344         int err;
5345
5346         DBG("service %p", service);
5347
5348         if (service->network != NULL) {
5349                 err = __connman_network_disconnect(service->network);
5350         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5351                                         service->provider != NULL)
5352                 err = __connman_provider_disconnect(service->provider);
5353         else
5354                 return -EOPNOTSUPP;
5355
5356         if (err < 0 && err != -EINPROGRESS)
5357                 return err;
5358
5359         __connman_6to4_remove(service->ipconfig_ipv4);
5360
5361         if (service->ipconfig_ipv4)
5362                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
5363                                                         NULL);
5364         else
5365                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
5366                                                         NULL);
5367
5368         __connman_ipconfig_address_remove(service->ipconfig_ipv4);
5369         __connman_ipconfig_address_remove(service->ipconfig_ipv6);
5370
5371         __connman_ipconfig_disable(service->ipconfig_ipv4);
5372         __connman_ipconfig_disable(service->ipconfig_ipv6);
5373
5374         __connman_stats_service_unregister(service);
5375
5376         return err;
5377 }
5378
5379 int __connman_service_disconnect_all(void)
5380 {
5381         GSequenceIter *iter;
5382         GSList *services = NULL, *list;
5383
5384         DBG("");
5385
5386         iter = g_sequence_get_begin_iter(service_list);
5387
5388         while (g_sequence_iter_is_end(iter) == FALSE) {
5389                 struct connman_service *service = g_sequence_get(iter);
5390
5391                 services = g_slist_prepend(services, service);
5392
5393                 iter = g_sequence_iter_next(iter);
5394         }
5395
5396         for (list = services; list != NULL; list = list->next) {
5397                 struct connman_service *service = list->data;
5398
5399                 service->ignore = TRUE;
5400
5401                 set_reconnect_state(service, FALSE);
5402
5403                 __connman_service_disconnect(service);
5404         }
5405
5406         g_slist_free(list);
5407
5408         return 0;
5409
5410 }
5411
5412 /**
5413  * lookup_by_identifier:
5414  * @identifier: service identifier
5415  *
5416  * Look up a service by identifier (reference count will not be increased)
5417  */
5418 static struct connman_service *lookup_by_identifier(const char *identifier)
5419 {
5420         GSequenceIter *iter;
5421
5422         iter = g_hash_table_lookup(service_hash, identifier);
5423         if (iter != NULL)
5424                 return g_sequence_get(iter);
5425
5426         return NULL;
5427 }
5428
5429 static void provision_changed(gpointer value, gpointer user_data)
5430 {
5431         struct connman_service *service = value;
5432         char *path = user_data;
5433
5434         __connman_config_provision_service_ident(service, path);
5435 }
5436
5437 void __connman_service_provision_changed(const char *ident)
5438 {
5439         g_sequence_foreach(service_list, provision_changed, (void *)ident);
5440 }
5441
5442 /**
5443  * __connman_service_get:
5444  * @identifier: service identifier
5445  *
5446  * Look up a service by identifier or create a new one if not found
5447  */
5448 static struct connman_service *service_get(const char *identifier)
5449 {
5450         struct connman_service *service;
5451         GSequenceIter *iter;
5452
5453         iter = g_hash_table_lookup(service_hash, identifier);
5454         if (iter != NULL) {
5455                 service = g_sequence_get(iter);
5456                 if (service != NULL)
5457                         connman_service_ref(service);
5458                 return service;
5459         }
5460
5461         service = connman_service_create();
5462         if (service == NULL)
5463                 return NULL;
5464
5465         DBG("service %p", service);
5466
5467         service->identifier = g_strdup(identifier);
5468
5469         iter = g_sequence_insert_sorted(service_list, service,
5470                                                 service_compare, NULL);
5471
5472         g_hash_table_insert(service_hash, service->identifier, iter);
5473
5474         return service;
5475 }
5476
5477 static int service_register(struct connman_service *service)
5478 {
5479         GSequenceIter *iter;
5480
5481         DBG("service %p", service);
5482
5483         if (service->path != NULL)
5484                 return -EALREADY;
5485
5486         service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
5487                                                 service->identifier);
5488
5489         DBG("path %s", service->path);
5490
5491         __connman_config_provision_service(service);
5492
5493         service_load(service);
5494
5495         g_dbus_register_interface(connection, service->path,
5496                                         CONNMAN_SERVICE_INTERFACE,
5497                                         service_methods, service_signals,
5498                                                         NULL, service, NULL);
5499
5500         iter = g_hash_table_lookup(service_hash, service->identifier);
5501         if (iter != NULL && g_sequence_get_length(service_list) > 1) {
5502                 g_sequence_sort_changed(iter, service_compare, NULL);
5503                 service_schedule_changed();
5504         }
5505
5506         __connman_connection_update_gateway();
5507
5508         return 0;
5509 }
5510
5511 static void service_up(struct connman_ipconfig *ipconfig)
5512 {
5513         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5514
5515         DBG("%s up", __connman_ipconfig_get_ifname(ipconfig));
5516
5517         link_changed(service);
5518
5519         service->stats.valid = FALSE;
5520         service->stats_roaming.valid = FALSE;
5521 }
5522
5523 static void service_down(struct connman_ipconfig *ipconfig)
5524 {
5525         DBG("%s down", __connman_ipconfig_get_ifname(ipconfig));
5526 }
5527
5528 static void service_lower_up(struct connman_ipconfig *ipconfig)
5529 {
5530         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5531
5532         DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig));
5533
5534         stats_start(service);
5535 }
5536
5537 static void service_lower_down(struct connman_ipconfig *ipconfig)
5538 {
5539         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5540
5541         DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig));
5542
5543         if (is_idle_state(service, service->state_ipv4) == FALSE)
5544                 __connman_ipconfig_disable(service->ipconfig_ipv4);
5545
5546         if (is_idle_state(service, service->state_ipv6) == FALSE)
5547                 __connman_ipconfig_disable(service->ipconfig_ipv6);
5548
5549         stats_stop(service);
5550         service_save(service);
5551 }
5552
5553 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5554 {
5555         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5556         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5557         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5558
5559         DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig));
5560
5561         type = __connman_ipconfig_get_config_type(ipconfig);
5562         method = __connman_ipconfig_get_method(ipconfig);
5563
5564         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5565                                                         type, method);
5566
5567         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5568                         method == CONNMAN_IPCONFIG_METHOD_AUTO)
5569                 __connman_service_ipconfig_indicate_state(service,
5570                                                 CONNMAN_SERVICE_STATE_READY,
5571                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
5572
5573         settings_changed(service, ipconfig);
5574 }
5575
5576 static void service_ip_release(struct connman_ipconfig *ipconfig)
5577 {
5578         struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5579         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5580         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5581
5582         DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig));
5583
5584         type = __connman_ipconfig_get_config_type(ipconfig);
5585         method = __connman_ipconfig_get_method(ipconfig);
5586
5587         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5588                                                         type, method);
5589
5590         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5591                         method == CONNMAN_IPCONFIG_METHOD_OFF)
5592                 __connman_service_ipconfig_indicate_state(service,
5593                                         CONNMAN_SERVICE_STATE_DISCONNECT,
5594                                         CONNMAN_IPCONFIG_TYPE_IPV6);
5595
5596         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5597                         method == CONNMAN_IPCONFIG_METHOD_OFF)
5598                 __connman_service_ipconfig_indicate_state(service,
5599                                         CONNMAN_SERVICE_STATE_DISCONNECT,
5600                                         CONNMAN_IPCONFIG_TYPE_IPV4);
5601
5602         settings_changed(service, ipconfig);
5603 }
5604
5605 static const struct connman_ipconfig_ops service_ops = {
5606         .up             = service_up,
5607         .down           = service_down,
5608         .lower_up       = service_lower_up,
5609         .lower_down     = service_lower_down,
5610         .ip_bound       = service_ip_bound,
5611         .ip_release     = service_ip_release,
5612 };
5613
5614 static void setup_ip4config(struct connman_service *service, int index,
5615                         enum connman_ipconfig_method method)
5616 {
5617         service->ipconfig_ipv4 = __connman_ipconfig_create(index,
5618                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
5619         if (service->ipconfig_ipv4 == NULL)
5620                 return;
5621
5622         __connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5623
5624         __connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5625
5626         __connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5627 }
5628
5629 static void setup_ip6config(struct connman_service *service, int index)
5630 {
5631         service->ipconfig_ipv6 = __connman_ipconfig_create(index,
5632                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
5633         if (service->ipconfig_ipv6 == NULL)
5634                 return;
5635
5636         __connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5637
5638         __connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5639 }
5640
5641 void __connman_service_read_ip4config(struct connman_service *service)
5642 {
5643         GKeyFile *keyfile;
5644
5645         if (service->ipconfig_ipv4 == NULL)
5646                 return;
5647
5648         keyfile = connman_storage_load_service(service->identifier);
5649         if (keyfile == NULL)
5650                 return;
5651
5652         __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5653                                 service->identifier, "IPv4.");
5654
5655         g_key_file_free(keyfile);
5656 }
5657
5658 void __connman_service_create_ip4config(struct connman_service *service,
5659                                         int index)
5660 {
5661         DBG("ipv4 %p", service->ipconfig_ipv4);
5662
5663         if (service->ipconfig_ipv4 != NULL)
5664                 return;
5665
5666         setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5667         __connman_service_read_ip4config(service);
5668 }
5669
5670 void __connman_service_read_ip6config(struct connman_service *service)
5671 {
5672         GKeyFile *keyfile;
5673
5674         if (service->ipconfig_ipv6 == NULL)
5675                 return;
5676
5677         keyfile = connman_storage_load_service(service->identifier);
5678         if (keyfile == NULL)
5679                 return;
5680
5681         __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5682                                 service->identifier, "IPv6.");
5683
5684         g_key_file_free(keyfile);
5685 }
5686
5687 void __connman_service_create_ip6config(struct connman_service *service,
5688                                                                 int index)
5689 {
5690         DBG("ipv6 %p", service->ipconfig_ipv6);
5691
5692         if (service->ipconfig_ipv6 != NULL)
5693                 return;
5694
5695         setup_ip6config(service, index);
5696
5697         __connman_service_read_ip6config(service);
5698 }
5699
5700 /**
5701  * __connman_service_lookup_from_network:
5702  * @network: network structure
5703  *
5704  * Look up a service by network (reference count will not be increased)
5705  */
5706 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5707 {
5708         struct connman_service *service;
5709         const char *ident, *group;
5710         char *name;
5711
5712         DBG("network %p", network);
5713
5714         if (network == NULL)
5715                 return NULL;
5716
5717         ident = __connman_network_get_ident(network);
5718         if (ident == NULL)
5719                 return NULL;
5720
5721         group = connman_network_get_group(network);
5722         if (group == NULL)
5723                 return NULL;
5724
5725         name = g_strdup_printf("%s_%s_%s",
5726                         __connman_network_get_type(network), ident, group);
5727         service = lookup_by_identifier(name);
5728         g_free(name);
5729
5730         return service;
5731 }
5732
5733 struct connman_service *__connman_service_lookup_from_index(int index)
5734 {
5735         struct connman_service *service;
5736         GSequenceIter *iter;
5737
5738         iter = g_sequence_get_begin_iter(service_list);
5739
5740         while (g_sequence_iter_is_end(iter) == FALSE) {
5741                 service = g_sequence_get(iter);
5742
5743                 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
5744                                                         == index)
5745                         return service;
5746
5747                 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
5748                                                         == index)
5749                         return service;
5750
5751                 iter = g_sequence_iter_next(iter);
5752         }
5753
5754         return NULL;
5755 }
5756
5757 const char *__connman_service_get_ident(struct connman_service *service)
5758 {
5759         return service->identifier;
5760 }
5761
5762 const char *__connman_service_get_path(struct connman_service *service)
5763 {
5764         return service->path;
5765 }
5766
5767 unsigned int __connman_service_get_order(struct connman_service *service)
5768 {
5769         GSequenceIter *iter;
5770
5771         if (service == NULL)
5772                 return 0;
5773
5774         if (service->favorite == FALSE) {
5775                 service->order = 0;
5776                 goto done;
5777         }
5778
5779         iter = g_hash_table_lookup(service_hash, service->identifier);
5780         if (iter != NULL) {
5781                 if (g_sequence_iter_get_position(iter) == 0)
5782                         service->order = 1;
5783                 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5784                                 service->do_split_routing == FALSE)
5785                         service->order = 10;
5786                 else
5787                         service->order = 0;
5788         }
5789
5790         DBG("service %p name %s order %d split %d", service, service->name,
5791                 service->order, service->do_split_routing);
5792
5793 done:
5794         return service->order;
5795 }
5796
5797 void __connman_service_update_ordering(void)
5798 {
5799         GSequenceIter *iter;
5800
5801         iter = g_sequence_get_begin_iter(service_list);
5802         if (iter != NULL && g_sequence_get_length(service_list) > 1)
5803                 g_sequence_sort_changed(iter, service_compare, NULL);
5804 }
5805
5806 static enum connman_service_type convert_network_type(struct connman_network *network)
5807 {
5808         enum connman_network_type type = connman_network_get_type(network);
5809
5810         switch (type) {
5811         case CONNMAN_NETWORK_TYPE_UNKNOWN:
5812         case CONNMAN_NETWORK_TYPE_VENDOR:
5813                 break;
5814         case CONNMAN_NETWORK_TYPE_ETHERNET:
5815                 return CONNMAN_SERVICE_TYPE_ETHERNET;
5816         case CONNMAN_NETWORK_TYPE_WIFI:
5817                 return CONNMAN_SERVICE_TYPE_WIFI;
5818         case CONNMAN_NETWORK_TYPE_WIMAX:
5819                 return CONNMAN_SERVICE_TYPE_WIMAX;
5820         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5821         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5822                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5823         case CONNMAN_NETWORK_TYPE_CELLULAR:
5824                 return CONNMAN_SERVICE_TYPE_CELLULAR;
5825         }
5826
5827         return CONNMAN_SERVICE_TYPE_UNKNOWN;
5828 }
5829
5830 static enum connman_service_security convert_wifi_security(const char *security)
5831 {
5832         if (security == NULL)
5833                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5834         else if (g_str_equal(security, "none") == TRUE)
5835                 return CONNMAN_SERVICE_SECURITY_NONE;
5836         else if (g_str_equal(security, "wep") == TRUE)
5837                 return CONNMAN_SERVICE_SECURITY_WEP;
5838         else if (g_str_equal(security, "psk") == TRUE)
5839                 return CONNMAN_SERVICE_SECURITY_PSK;
5840         else if (g_str_equal(security, "ieee8021x") == TRUE)
5841                 return CONNMAN_SERVICE_SECURITY_8021X;
5842         else if (g_str_equal(security, "wpa") == TRUE)
5843                 return CONNMAN_SERVICE_SECURITY_WPA;
5844         else if (g_str_equal(security, "rsn") == TRUE)
5845                 return CONNMAN_SERVICE_SECURITY_RSN;
5846         else
5847                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5848 }
5849
5850 static void update_from_network(struct connman_service *service,
5851                                         struct connman_network *network)
5852 {
5853         connman_uint8_t strength = service->strength;
5854         GSequenceIter *iter;
5855         const char *str;
5856
5857         DBG("service %p network %p", service, network);
5858
5859         if (is_connected(service) == TRUE)
5860                 return;
5861
5862         if (is_connecting(service) == TRUE)
5863                 return;
5864
5865         str = connman_network_get_string(network, "Name");
5866         if (str != NULL) {
5867                 g_free(service->name);
5868                 service->name = g_strdup(str);
5869                 service->hidden = FALSE;
5870         } else {
5871                 g_free(service->name);
5872                 service->name = NULL;
5873                 service->hidden = TRUE;
5874         }
5875
5876         service->strength = connman_network_get_strength(network);
5877         service->roaming = connman_network_get_bool(network, "Roaming");
5878
5879         if (service->strength == 0) {
5880                 /*
5881                  * Filter out 0-values; it's unclear what they mean
5882                  * and they cause anomalous sorting of the priority list.
5883                  */
5884                 service->strength = strength;
5885         }
5886
5887         str = connman_network_get_string(network, "WiFi.Security");
5888         service->security = convert_wifi_security(str);
5889
5890         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5891                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5892
5893         if (service->strength > strength && service->network != NULL) {
5894                 connman_network_unref(service->network);
5895                 service->network = connman_network_ref(network);
5896
5897                 strength_changed(service);
5898         }
5899
5900         if (service->network == NULL)
5901                 service->network = connman_network_ref(network);
5902
5903         iter = g_hash_table_lookup(service_hash, service->identifier);
5904         if (iter != NULL && g_sequence_get_length(service_list) > 1) {
5905                 g_sequence_sort_changed(iter, service_compare, NULL);
5906                 service_schedule_changed();
5907         }
5908 }
5909
5910 /**
5911  * __connman_service_create_from_network:
5912  * @network: network structure
5913  *
5914  * Look up service by network and if not found, create one
5915  */
5916 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5917 {
5918         struct connman_service *service;
5919         struct connman_device *device;
5920         const char *ident, *group;
5921         char *name;
5922         unsigned int *auto_connect_types;
5923         int i, index;
5924
5925         DBG("network %p", network);
5926
5927         if (network == NULL)
5928                 return NULL;
5929
5930         ident = __connman_network_get_ident(network);
5931         if (ident == NULL)
5932                 return NULL;
5933
5934         group = connman_network_get_group(network);
5935         if (group == NULL)
5936                 return NULL;
5937
5938         name = g_strdup_printf("%s_%s_%s",
5939                         __connman_network_get_type(network), ident, group);
5940         service = service_get(name);
5941         g_free(name);
5942
5943         if (service == NULL)
5944                 return NULL;
5945
5946         if (__connman_network_get_weakness(network) == TRUE)
5947                 return service;
5948
5949         if (service->path != NULL) {
5950                 update_from_network(service, network);
5951                 __connman_connection_update_gateway();
5952                 return service;
5953         }
5954
5955         service->type = convert_network_type(network);
5956
5957         auto_connect_types = connman_setting_get_uint_list("DefaultAutoConnectTechnologies");
5958         service->autoconnect = FALSE;
5959         for (i = 0; auto_connect_types != NULL &&
5960                      auto_connect_types[i] != 0; i++) {
5961                 if (service->type == auto_connect_types[i]) {
5962                         service->autoconnect = TRUE;
5963                         break;
5964                 }
5965         }
5966
5967         switch (service->type) {
5968         case CONNMAN_SERVICE_TYPE_UNKNOWN:
5969         case CONNMAN_SERVICE_TYPE_SYSTEM:
5970         case CONNMAN_SERVICE_TYPE_WIMAX:
5971         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5972         case CONNMAN_SERVICE_TYPE_GPS:
5973         case CONNMAN_SERVICE_TYPE_VPN:
5974         case CONNMAN_SERVICE_TYPE_GADGET:
5975         case CONNMAN_SERVICE_TYPE_WIFI:
5976         case CONNMAN_SERVICE_TYPE_CELLULAR:
5977                 break;
5978         case CONNMAN_SERVICE_TYPE_ETHERNET:
5979                 service->favorite = TRUE;
5980                 break;
5981         }
5982
5983         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5984         service->state = combine_state(service->state_ipv4, service->state_ipv6);
5985
5986         update_from_network(service, network);
5987
5988         index = connman_network_get_index(network);
5989
5990         if (service->ipconfig_ipv4 == NULL)
5991                 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5992
5993         if (service->ipconfig_ipv6 == NULL)
5994                 setup_ip6config(service, index);
5995
5996         service_register(service);
5997
5998         if (service->favorite == TRUE) {
5999                 device = connman_network_get_device(service->network);
6000                 if (device && connman_device_get_scanning(device) == FALSE)
6001                         __connman_service_auto_connect();
6002         }
6003
6004         __connman_notifier_service_add(service, service->name);
6005         service_schedule_added(service);
6006
6007         return service;
6008 }
6009
6010 void __connman_service_update_from_network(struct connman_network *network)
6011 {
6012         connman_bool_t need_sort = FALSE;
6013         struct connman_service *service;
6014         connman_uint8_t strength;
6015         connman_bool_t roaming;
6016         GSequenceIter *iter;
6017         const char *name;
6018         connman_bool_t stats_enable;
6019
6020         DBG("network %p", network);
6021
6022         service = __connman_service_lookup_from_network(network);
6023         if (service == NULL)
6024                 return;
6025
6026         if (service->network == NULL)
6027                 return;
6028
6029         name = connman_network_get_string(service->network, "Name");
6030         if (g_strcmp0(service->name, name) != 0) {
6031                 g_free(service->name);
6032                 service->name = g_strdup(name);
6033                 connman_dbus_property_changed_basic(service->path,
6034                                 CONNMAN_SERVICE_INTERFACE, "Name",
6035                                 DBUS_TYPE_STRING, &service->name);
6036         }
6037
6038         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
6039                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
6040
6041         strength = connman_network_get_strength(service->network);
6042         if (strength == service->strength)
6043                 goto roaming;
6044
6045         service->strength = strength;
6046         need_sort = TRUE;
6047
6048         strength_changed(service);
6049
6050 roaming:
6051         roaming = connman_network_get_bool(service->network, "Roaming");
6052         if (roaming == service->roaming)
6053                 goto sorting;
6054
6055         stats_enable = stats_enabled(service);
6056         if (stats_enable == TRUE)
6057                 stats_stop(service);
6058
6059         service->roaming = roaming;
6060         need_sort = TRUE;
6061
6062         if (stats_enable == TRUE)
6063                 stats_start(service);
6064
6065         roaming_changed(service);
6066
6067 sorting:
6068         if (need_sort == TRUE) {
6069                 iter = g_hash_table_lookup(service_hash, service->identifier);
6070                 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
6071                         g_sequence_sort_changed(iter, service_compare, NULL);
6072                         service_schedule_changed();
6073                 }
6074         }
6075 }
6076
6077 void __connman_service_remove_from_network(struct connman_network *network)
6078 {
6079         struct connman_service *service;
6080
6081         DBG("network %p", network);
6082
6083         service = __connman_service_lookup_from_network(network);
6084         if (service == NULL)
6085                 return;
6086
6087         service->ignore = TRUE;
6088
6089         __connman_connection_gateway_remove(service,
6090                                         CONNMAN_IPCONFIG_TYPE_ALL);
6091
6092         connman_service_unref(service);
6093 }
6094
6095 /**
6096  * __connman_service_create_from_provider:
6097  * @provider: provider structure
6098  *
6099  * Look up service by provider and if not found, create one
6100  */
6101 struct connman_service *
6102 __connman_service_create_from_provider(struct connman_provider *provider)
6103 {
6104         struct connman_service *service;
6105         const char *ident, *str;
6106         char *name;
6107         int index = connman_provider_get_index(provider);
6108
6109         DBG("provider %p", provider);
6110
6111         ident = __connman_provider_get_ident(provider);
6112         if (ident == NULL)
6113                 return NULL;
6114
6115         name = g_strdup_printf("vpn_%s", ident);
6116         service = service_get(name);
6117         g_free(name);
6118
6119         if (service == NULL)
6120                 return NULL;
6121
6122         service->type = CONNMAN_SERVICE_TYPE_VPN;
6123         service->provider = connman_provider_ref(provider);
6124         service->autoconnect = FALSE;
6125         service->userconnect = TRUE;
6126
6127         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
6128         service->state = combine_state(service->state_ipv4, service->state_ipv6);
6129
6130         str = connman_provider_get_string(provider, "Name");
6131         if (str != NULL) {
6132                 g_free(service->name);
6133                 service->name = g_strdup(str);
6134                 service->hidden = FALSE;
6135         } else {
6136                 g_free(service->name);
6137                 service->name = NULL;
6138                 service->hidden = TRUE;
6139         }
6140
6141         service->strength = 0;
6142
6143         if (service->ipconfig_ipv4 == NULL)
6144                 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
6145
6146         if (service->ipconfig_ipv6 == NULL)
6147                 setup_ip6config(service, index);
6148
6149         service_register(service);
6150
6151         __connman_notifier_service_add(service, service->name);
6152         service_schedule_added(service);
6153
6154         return service;
6155 }
6156
6157 int __connman_service_init(void)
6158 {
6159         DBG("");
6160
6161         connection = connman_dbus_get_connection();
6162
6163         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
6164                                                                 NULL, NULL);
6165
6166         service_list = g_sequence_new(service_free);
6167
6168         services_notify = g_new0(struct _services_notify, 1);
6169         services_notify->remove = g_hash_table_new_full(g_str_hash,
6170                         g_str_equal, g_free, NULL);
6171         services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
6172
6173         return 0;
6174 }
6175
6176 void __connman_service_cleanup(void)
6177 {
6178         GSequence *list;
6179
6180         DBG("");
6181
6182         if (autoconnect_timeout != 0) {
6183                 g_source_remove(autoconnect_timeout);
6184                 autoconnect_timeout = 0;
6185         }
6186
6187         list = service_list;
6188         service_list = NULL;
6189         g_sequence_free(list);
6190
6191         g_hash_table_destroy(service_hash);
6192         service_hash = NULL;
6193
6194         g_slist_free(counter_list);
6195         counter_list = NULL;
6196
6197         if (services_notify->id != 0) {
6198                 g_source_remove(services_notify->id);
6199                 service_send_changed(NULL);
6200                 g_hash_table_destroy(services_notify->remove);
6201                 g_hash_table_destroy(services_notify->add);
6202         }
6203         g_free(services_notify);
6204
6205         dbus_connection_unref(connection);
6206 }