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