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