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