session: Factor out memory release part of cleanup_session
[platform/upstream/connman.git] / src / session.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2011  BWM CarIT GmbH. All rights reserved.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28
29 #include <gdbus.h>
30
31 #include <connman/session.h>
32
33 #include "connman.h"
34
35 static DBusConnection *connection;
36 static GHashTable *session_hash;
37 static connman_bool_t sessionmode;
38 static struct connman_session *ecall_session;
39 static GSList *policy_list;
40
41 enum connman_session_trigger {
42         CONNMAN_SESSION_TRIGGER_UNKNOWN         = 0,
43         CONNMAN_SESSION_TRIGGER_SETTING         = 1,
44         CONNMAN_SESSION_TRIGGER_CONNECT         = 2,
45         CONNMAN_SESSION_TRIGGER_DISCONNECT      = 3,
46         CONNMAN_SESSION_TRIGGER_SERVICE         = 4,
47         CONNMAN_SESSION_TRIGGER_ECALL           = 5,
48 };
49
50 enum connman_session_reason {
51         CONNMAN_SESSION_REASON_UNKNOWN          = 0,
52         CONNMAN_SESSION_REASON_CONNECT          = 1,
53         CONNMAN_SESSION_REASON_FREE_RIDE        = 2,
54 };
55
56 enum connman_session_state {
57         CONNMAN_SESSION_STATE_DISCONNECTED   = 0,
58         CONNMAN_SESSION_STATE_CONNECTED      = 1,
59         CONNMAN_SESSION_STATE_ONLINE         = 2,
60 };
61
62 struct service_entry {
63         /* track why this service was selected */
64         enum connman_session_reason reason;
65         enum connman_service_state state;
66         const char *name;
67         struct connman_service *service;
68         char *ifname;
69         const char *bearer;
70         GSList *pending_timeouts;
71 };
72
73 struct session_info {
74         struct connman_session_config config;
75         enum connman_session_state state;
76         struct service_entry *entry;
77         enum connman_session_reason reason;
78 };
79
80 struct connman_session {
81         char *owner;
82         char *session_path;
83         char *notify_path;
84         guint notify_watch;
85
86         struct connman_session_policy *policy;
87
88         connman_bool_t append_all;
89         struct session_info *info;
90         struct session_info *info_last;
91         struct connman_session_config *policy_config;
92
93         connman_bool_t ecall;
94
95         GSequence *service_list;
96         GHashTable *service_hash;
97 };
98
99 static const char *trigger2string(enum connman_session_trigger trigger)
100 {
101         switch (trigger) {
102         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
103                 break;
104         case CONNMAN_SESSION_TRIGGER_SETTING:
105                 return "setting";
106         case CONNMAN_SESSION_TRIGGER_CONNECT:
107                 return "connect";
108         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
109                 return "disconnect";
110         case CONNMAN_SESSION_TRIGGER_SERVICE:
111                 return "service";
112         case CONNMAN_SESSION_TRIGGER_ECALL:
113                 return "ecall";
114         }
115
116         return NULL;
117 }
118
119 static const char *reason2string(enum connman_session_reason reason)
120 {
121         switch (reason) {
122         case CONNMAN_SESSION_REASON_UNKNOWN:
123                 return "unknown";
124         case CONNMAN_SESSION_REASON_CONNECT:
125                 return "connect";
126         case CONNMAN_SESSION_REASON_FREE_RIDE:
127                 return "free-ride";
128         }
129
130         return NULL;
131 }
132
133 static const char *state2string(enum connman_session_state state)
134 {
135         switch (state) {
136         case CONNMAN_SESSION_STATE_DISCONNECTED:
137                 return "disconnected";
138         case CONNMAN_SESSION_STATE_CONNECTED:
139                 return "connected";
140         case CONNMAN_SESSION_STATE_ONLINE:
141                 return "online";
142         }
143
144         return NULL;
145 }
146
147 static const char *type2string(enum connman_session_type type)
148 {
149         switch (type) {
150         case CONNMAN_SESSION_TYPE_UNKNOWN:
151                 return "";
152         case CONNMAN_SESSION_TYPE_ANY:
153                 return "any";
154         case CONNMAN_SESSION_TYPE_LOCAL:
155                 return "local";
156         case CONNMAN_SESSION_TYPE_INTERNET:
157                 return "internet";
158         }
159
160         return NULL;
161 }
162
163 static enum connman_session_type string2type(const char *type)
164 {
165         if (g_strcmp0(type, "any") == 0)
166                 return CONNMAN_SESSION_TYPE_ANY;
167         if (g_strcmp0(type, "local") == 0)
168                 return CONNMAN_SESSION_TYPE_LOCAL;
169         else if (g_strcmp0(type, "internet") == 0)
170                 return CONNMAN_SESSION_TYPE_INTERNET;
171
172         return CONNMAN_SESSION_TYPE_UNKNOWN;
173 }
174
175 static enum connman_service_type bearer2service(const char *bearer)
176 {
177         if (bearer == NULL)
178                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
179
180         if (g_strcmp0(bearer, "ethernet") == 0)
181                 return CONNMAN_SERVICE_TYPE_ETHERNET;
182         else if (g_strcmp0(bearer, "wifi") == 0)
183                 return CONNMAN_SERVICE_TYPE_WIFI;
184         else if (g_strcmp0(bearer, "wimax") == 0)
185                 return CONNMAN_SERVICE_TYPE_WIMAX;
186         else if (g_strcmp0(bearer, "bluetooth") == 0)
187                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
188         else if (g_strcmp0(bearer, "cellular") == 0)
189                 return CONNMAN_SERVICE_TYPE_CELLULAR;
190         else if (g_strcmp0(bearer, "vpn") == 0)
191                 return CONNMAN_SERVICE_TYPE_VPN;
192         else
193                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
194 }
195
196 static char *service2bearer(enum connman_service_type type)
197 {
198         switch (type) {
199         case CONNMAN_SERVICE_TYPE_ETHERNET:
200                 return "ethernet";
201         case CONNMAN_SERVICE_TYPE_WIFI:
202                 return "wifi";
203         case CONNMAN_SERVICE_TYPE_WIMAX:
204                 return "wimax";
205         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
206                 return "bluetooth";
207         case CONNMAN_SERVICE_TYPE_CELLULAR:
208                 return "cellular";
209         case CONNMAN_SERVICE_TYPE_VPN:
210                 return "vpn";
211         case CONNMAN_SERVICE_TYPE_UNKNOWN:
212         case CONNMAN_SERVICE_TYPE_SYSTEM:
213         case CONNMAN_SERVICE_TYPE_GPS:
214         case CONNMAN_SERVICE_TYPE_GADGET:
215                 return "";
216         }
217
218         return "";
219 }
220
221 static int assign_policy_plugin(struct connman_session *session)
222 {
223         GSList *list;
224         struct connman_session_policy *policy;
225
226         if (session->policy != NULL)
227                 return -EALREADY;
228
229         for (list = policy_list; list != NULL; list = list->next) {
230                 policy = list->data;
231
232                 session->policy = policy;
233                 break;
234         }
235
236         return 0;
237 }
238
239 static int create_policy_config(struct connman_session *session)
240 {
241         struct connman_session_config *config;
242
243         if (session->policy == NULL)
244                 config = connman_session_create_default_config();
245         else
246                 config = (*session->policy->create)(session);
247
248         if (config == NULL)
249                 return -ENOMEM;
250
251         session->policy_config = config;
252
253         return 0;
254 }
255
256 static void destroy_policy_config(struct connman_session *session)
257 {
258         if (session->policy == NULL) {
259                 connman_session_free_bearers(
260                         session->policy_config->allowed_bearers);
261                 g_free(session->policy_config);
262         } else {
263                 (*session->policy->destroy)(session);
264         }
265 }
266
267 static void probe_policy(struct connman_session_policy *policy)
268 {
269
270         GHashTableIter iter;
271         gpointer key, value;
272         struct connman_session *session;
273
274         DBG("policy %p name %s", policy, policy->name);
275
276         g_hash_table_iter_init(&iter, session_hash);
277
278         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
279                 session = value;
280
281                 if (session->policy != NULL)
282                         continue;
283
284                 assign_policy_plugin(session);
285         }
286 }
287
288 static void remove_policy(struct connman_session_policy *policy)
289 {
290         GHashTableIter iter;
291         gpointer key, value;
292         struct connman_session *session;
293
294         DBG("policy %p name %s", policy, policy->name);
295
296         g_hash_table_iter_init(&iter, session_hash);
297
298         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
299                 session = value;
300
301                 if (session->policy != policy)
302                         continue;
303
304                 session->policy = NULL;
305                 assign_policy_plugin(session);
306         }
307 }
308
309 static gint compare_priority(gconstpointer a, gconstpointer b)
310 {
311         const struct connman_session_policy *policy1 = a;
312         const struct connman_session_policy *policy2 = b;
313
314         return policy2->priority - policy1->priority;
315 }
316
317
318 int connman_session_policy_register(struct connman_session_policy *policy)
319 {
320         DBG("name %s", policy->name);
321
322         policy_list = g_slist_insert_sorted(policy_list, policy,
323                                                 compare_priority);
324
325         probe_policy(policy);
326
327         return 0;
328 }
329
330 void connman_session_policy_unregister(struct connman_session_policy *policy)
331 {
332         DBG("name %s", policy->name);
333
334         policy_list = g_slist_remove(policy_list, policy);
335
336         remove_policy(policy);
337 }
338
339 struct connman_session_config *connman_session_create_default_config(void)
340 {
341         struct connman_session_config *config;
342
343         config = g_try_new0(struct connman_session_config, 1);
344         if (config == NULL)
345                 return NULL;
346
347         config->priority = FALSE;
348         config->roaming_policy = CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
349         config->type = CONNMAN_SESSION_TYPE_ANY;
350         config->ecall = FALSE;
351         config->allowed_bearers = connman_session_allowed_bearers_any();
352         if (config->allowed_bearers == NULL) {
353                 g_free(config);
354                 return NULL;
355         }
356
357         return config;
358 }
359
360 static enum connman_session_type apply_policy_on_type(
361                         enum connman_session_type policy,
362                         enum connman_session_type type)
363 {
364         if (type == CONNMAN_SESSION_TYPE_UNKNOWN)
365                 return CONNMAN_SESSION_TYPE_UNKNOWN;
366
367         if (policy == CONNMAN_SESSION_TYPE_ANY)
368                 return type;
369
370         if (policy == CONNMAN_SESSION_TYPE_LOCAL)
371                 return CONNMAN_SESSION_TYPE_LOCAL;
372
373         return CONNMAN_SESSION_TYPE_INTERNET;
374 }
375
376 static void cleanup_bearer(gpointer data)
377 {
378         struct connman_session_bearer *bearer = data;
379
380         g_free(bearer->name);
381         g_free(bearer);
382 }
383
384 void connman_session_free_bearers(GSList *bearers)
385 {
386         g_slist_free_full(bearers, cleanup_bearer);
387 }
388
389 static int session_parse_allowed_bearers(DBusMessageIter *iter, GSList **list)
390 {
391         struct connman_session_bearer *bearer;
392         DBusMessageIter array;
393
394         dbus_message_iter_recurse(iter, &array);
395
396         *list = NULL;
397
398         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
399                 char *bearer_name = NULL;
400
401                 dbus_message_iter_get_basic(&array, &bearer_name);
402
403                 bearer = g_try_new0(struct connman_session_bearer, 1);
404                 if (bearer == NULL) {
405                         connman_session_free_bearers(*list);
406                         *list = NULL;
407                         return -ENOMEM;
408                 }
409
410                 bearer->name = g_strdup(bearer_name);
411                 bearer->service_type = bearer2service(bearer->name);
412
413                 if (bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
414                                 g_strcmp0(bearer->name, "*") == 0) {
415                         bearer->match_all = TRUE;
416                 } else {
417                         bearer->match_all = FALSE;
418                 }
419
420                 *list = g_slist_append(*list, bearer);
421
422                 dbus_message_iter_next(&array);
423         }
424
425         return 0;
426 }
427
428 static struct connman_session_bearer *clone_bearer(
429                                         struct connman_session_bearer *orig)
430 {
431         struct connman_session_bearer *bearer;
432
433         bearer = g_try_new0(struct connman_session_bearer, 1);
434         if (bearer == NULL)
435                 return NULL;
436
437         bearer->name = g_strdup(orig->name);
438         bearer->match_all = orig->match_all;
439         bearer->service_type = orig->service_type;
440
441         return bearer;
442 }
443
444 static int filter_bearer(GSList *policy_bearers,
445                                 struct connman_session_bearer *bearer,
446                                 GSList **list)
447 {
448         struct connman_session_bearer *policy, *tmp;
449         GSList *it;
450
451         if (policy_bearers == NULL)
452                 goto clone;
453
454         for (it = policy_bearers; it != NULL; it = it->next) {
455                 policy = it->data;
456
457                 if (policy->match_all == FALSE &&
458                                 bearer->service_type != policy->service_type)
459                         continue;
460
461                 goto clone;
462         }
463
464         *list = NULL;
465
466         return 0;
467
468 clone:
469         tmp = clone_bearer(bearer);
470         if (tmp == NULL) {
471                 connman_session_free_bearers(*list);
472                 *list = NULL;
473                 return -ENOMEM;
474         }
475         *list = g_slist_append(*list, tmp);
476
477         return 0;
478 }
479
480 static connman_bool_t is_bearer_valid(struct connman_session_bearer *bearer)
481 {
482         if (bearer->match_all == FALSE &&
483                         bearer->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN)
484                 return FALSE;
485
486         return TRUE;
487 }
488
489 static int apply_policy_on_bearers(GSList *policy_bearers, GSList *bearers,
490                                 GSList **list)
491 {
492         struct connman_session_bearer *bearer;
493         GSList *it;
494         int err;
495
496         *list = NULL;
497
498         for (it = bearers; it != NULL; it = it->next) {
499                 bearer = it->data;
500
501                 if (is_bearer_valid(bearer) == FALSE)
502                         continue;
503
504                 err = filter_bearer(policy_bearers, bearer, list);
505                 if (err < 0)
506                         return err;
507         }
508
509         return 0;
510 }
511
512 GSList *connman_session_allowed_bearers_any(void)
513 {
514         struct connman_session_bearer *bearer;
515         GSList *list = NULL;
516
517         bearer = g_try_new0(struct connman_session_bearer, 1);
518         if (bearer == NULL)
519                 return NULL;
520
521         bearer->name = g_strdup("");
522         bearer->match_all = TRUE;
523         bearer->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
524
525         list = g_slist_append(list, bearer);
526
527         return list;
528 }
529
530 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
531 {
532         struct session_info *info = user_data;
533         GSList *list;
534
535         for (list = info->config.allowed_bearers;
536                         list != NULL; list = list->next) {
537                 struct connman_session_bearer *bearer = list->data;
538
539                 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
540                                                 &bearer->name);
541         }
542 }
543
544 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
545 {
546         struct connman_service *service = user_data;
547         struct connman_ipconfig *ipconfig_ipv4;
548
549         if (service == NULL)
550                 return;
551
552         if (__connman_service_is_connected_state(service,
553                                 CONNMAN_IPCONFIG_TYPE_IPV4) == FALSE) {
554                 return;
555         }
556
557         ipconfig_ipv4 = __connman_service_get_ip4config(service);
558         if (ipconfig_ipv4 == NULL)
559                 return;
560
561         __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
562 }
563
564 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
565 {
566         struct connman_service *service = user_data;
567         struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
568
569         if (service == NULL)
570                 return;
571
572         if (__connman_service_is_connected_state(service,
573                                 CONNMAN_IPCONFIG_TYPE_IPV6) == FALSE) {
574                 return;
575         }
576
577         ipconfig_ipv4 = __connman_service_get_ip4config(service);
578         ipconfig_ipv6 = __connman_service_get_ip6config(service);
579         if (ipconfig_ipv6 == NULL)
580                 return;
581
582         __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
583 }
584
585 static void append_notify(DBusMessageIter *dict,
586                                         struct connman_session *session)
587 {
588         struct session_info *info = session->info;
589         struct session_info *info_last = session->info_last;
590         struct connman_service *service;
591         const char *name, *ifname, *bearer;
592
593         if (session->append_all == TRUE ||
594                         info->state != info_last->state) {
595                 const char *state = state2string(info->state);
596
597                 connman_dbus_dict_append_basic(dict, "State",
598                                                 DBUS_TYPE_STRING,
599                                                 &state);
600                 info_last->state = info->state;
601         }
602
603         if (session->append_all == TRUE ||
604                         info->entry != info_last->entry) {
605                 if (info->entry == NULL) {
606                         name = "";
607                         ifname = "";
608                         service = NULL;
609                         bearer = "";
610                 } else {
611                         name = info->entry->name;
612                         ifname = info->entry->ifname;
613                         service = info->entry->service;
614                         bearer = info->entry->bearer;
615                 }
616
617                 connman_dbus_dict_append_basic(dict, "Name",
618                                                 DBUS_TYPE_STRING,
619                                                 &name);
620
621                 connman_dbus_dict_append_dict(dict, "IPv4",
622                                                 append_ipconfig_ipv4,
623                                                 service);
624
625                 connman_dbus_dict_append_dict(dict, "IPv6",
626                                                 append_ipconfig_ipv6,
627                                                 service);
628
629                 connman_dbus_dict_append_basic(dict, "Interface",
630                                                 DBUS_TYPE_STRING,
631                                                 &ifname);
632
633                 connman_dbus_dict_append_basic(dict, "Bearer",
634                                                 DBUS_TYPE_STRING,
635                                                 &bearer);
636
637                 info_last->entry = info->entry;
638         }
639
640         if (session->append_all == TRUE ||
641                         info->config.type != info_last->config.type) {
642                 const char *type = type2string(info->config.type);
643
644                 connman_dbus_dict_append_basic(dict, "ConnectionType",
645                                                 DBUS_TYPE_STRING,
646                                                 &type);
647                 info_last->config.type = info->config.type;
648         }
649
650         if (session->append_all == TRUE ||
651                         info->config.allowed_bearers != info_last->config.allowed_bearers) {
652                 connman_dbus_dict_append_array(dict, "AllowedBearers",
653                                                 DBUS_TYPE_STRING,
654                                                 append_allowed_bearers,
655                                                 info);
656                 info_last->config.allowed_bearers = info->config.allowed_bearers;
657         }
658
659         session->append_all = FALSE;
660 }
661
662 static connman_bool_t is_type_matching_state(enum connman_session_state *state,
663                                                 enum connman_session_type type)
664 {
665         switch (type) {
666         case CONNMAN_SESSION_TYPE_UNKNOWN:
667                 return FALSE;
668         case CONNMAN_SESSION_TYPE_ANY:
669                 return TRUE;
670         case CONNMAN_SESSION_TYPE_LOCAL:
671                 if (*state >= CONNMAN_SESSION_STATE_CONNECTED) {
672                         *state = CONNMAN_SESSION_STATE_CONNECTED;
673                         return TRUE;
674                 }
675
676                 break;
677         case CONNMAN_SESSION_TYPE_INTERNET:
678                 if (*state == CONNMAN_SESSION_STATE_ONLINE)
679                         return TRUE;
680                 break;
681         }
682
683         return FALSE;
684 }
685
686 static connman_bool_t compute_notifiable_changes(struct connman_session *session)
687 {
688         struct session_info *info_last = session->info_last;
689         struct session_info *info = session->info;
690
691         if (session->append_all == TRUE)
692                 return TRUE;
693
694         if (info->state != info_last->state)
695                 return TRUE;
696
697         if (info->entry != info_last->entry &&
698                         info->state >= CONNMAN_SESSION_STATE_CONNECTED)
699                 return TRUE;
700
701         if (info->config.allowed_bearers != info_last->config.allowed_bearers ||
702                         info->config.type != info_last->config.type)
703                 return TRUE;
704
705         return FALSE;
706 }
707
708 static gboolean session_notify(gpointer user_data)
709 {
710         struct connman_session *session = user_data;
711         DBusMessage *msg;
712         DBusMessageIter array, dict;
713
714         if (compute_notifiable_changes(session) == FALSE)
715                 return FALSE;
716
717         DBG("session %p owner %s notify_path %s", session,
718                 session->owner, session->notify_path);
719
720         msg = dbus_message_new_method_call(session->owner, session->notify_path,
721                                                 CONNMAN_NOTIFICATION_INTERFACE,
722                                                 "Update");
723         if (msg == NULL)
724                 return FALSE;
725
726         dbus_message_iter_init_append(msg, &array);
727         connman_dbus_dict_open(&array, &dict);
728
729         append_notify(&dict, session);
730
731         connman_dbus_dict_close(&array, &dict);
732
733         g_dbus_send_message(connection, msg);
734
735         return FALSE;
736 }
737
738 static void ipconfig_ipv4_changed(struct connman_session *session)
739 {
740         struct session_info *info = session->info;
741
742         connman_dbus_setting_changed_dict(session->owner, session->notify_path,
743                                                 "IPv4", append_ipconfig_ipv4,
744                                                 info->entry->service);
745 }
746
747 static void ipconfig_ipv6_changed(struct connman_session *session)
748 {
749         struct session_info *info = session->info;
750
751         connman_dbus_setting_changed_dict(session->owner, session->notify_path,
752                                                 "IPv6", append_ipconfig_ipv6,
753                                                 info->entry->service);
754 }
755
756 static connman_bool_t service_type_match(struct connman_session *session,
757                                         struct connman_service *service)
758 {
759         struct session_info *info = session->info;
760         GSList *list;
761
762         for (list = info->config.allowed_bearers;
763                         list != NULL; list = list->next) {
764                 struct connman_session_bearer *bearer = list->data;
765                 enum connman_service_type service_type;
766
767                 if (bearer->match_all == TRUE)
768                         return TRUE;
769
770                 service_type = connman_service_get_type(service);
771                 if (bearer->service_type == service_type)
772                         return TRUE;
773         }
774
775         return FALSE;
776 }
777
778 static connman_bool_t service_match(struct connman_session *session,
779                                         struct connman_service *service)
780 {
781         if (service_type_match(session, service) == FALSE)
782                 return FALSE;
783
784         return TRUE;
785 }
786
787 static int service_type_weight(enum connman_service_type type)
788 {
789         /*
790          * The session doesn't care which service
791          * to use. Nevertheless we have to sort them
792          * according their type. The ordering is
793          *
794          * 1. Ethernet
795          * 2. Bluetooth
796          * 3. WiFi/WiMAX
797          * 4. Cellular
798          */
799
800         switch (type) {
801         case CONNMAN_SERVICE_TYPE_ETHERNET:
802                 return 4;
803         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
804                 return 3;
805         case CONNMAN_SERVICE_TYPE_WIFI:
806         case CONNMAN_SERVICE_TYPE_WIMAX:
807                 return 2;
808         case CONNMAN_SERVICE_TYPE_CELLULAR:
809                 return 1;
810         case CONNMAN_SERVICE_TYPE_UNKNOWN:
811         case CONNMAN_SERVICE_TYPE_SYSTEM:
812         case CONNMAN_SERVICE_TYPE_GPS:
813         case CONNMAN_SERVICE_TYPE_VPN:
814         case CONNMAN_SERVICE_TYPE_GADGET:
815                 break;
816         }
817
818         return 0;
819 }
820
821 static gint sort_allowed_bearers(struct connman_service *service_a,
822                                         struct connman_service *service_b,
823                                         struct connman_session *session)
824 {
825         struct session_info *info = session->info;
826         GSList *list;
827         enum connman_service_type type_a, type_b;
828         int weight_a, weight_b;
829
830         type_a = connman_service_get_type(service_a);
831         type_b = connman_service_get_type(service_b);
832
833         for (list = info->config.allowed_bearers;
834                         list != NULL; list = list->next) {
835                 struct connman_session_bearer *bearer = list->data;
836
837                 if (bearer->match_all == TRUE) {
838                         if (type_a != type_b) {
839                                 weight_a = service_type_weight(type_a);
840                                 weight_b = service_type_weight(type_b);
841
842                                 if (weight_a > weight_b)
843                                         return -1;
844
845                                 if (weight_a < weight_b)
846                                         return 1;
847
848                                 return 0;
849                         }
850                 }
851
852                 if (type_a == bearer->service_type &&
853                                 type_b == bearer->service_type) {
854                         return 0;
855                 }
856
857                 if (type_a == bearer->service_type &&
858                                 type_b != bearer->service_type) {
859                         return -1;
860                 }
861
862                 if (type_a != bearer->service_type &&
863                                 type_b == bearer->service_type) {
864                         return 1;
865                 }
866         }
867
868         return 0;
869 }
870
871 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
872 {
873         struct service_entry *entry_a = (void *)a;
874         struct service_entry *entry_b = (void *)b;
875         struct connman_session *session = user_data;
876
877         return sort_allowed_bearers(entry_a->service, entry_b->service,
878                                 session);
879 }
880
881 static void free_session(struct connman_session *session)
882 {
883         destroy_policy_config(session);
884         connman_session_free_bearers(session->info->config.allowed_bearers);
885         g_free(session->owner);
886         g_free(session->session_path);
887         g_free(session->notify_path);
888         g_free(session->info);
889         g_free(session->info_last);
890
891         g_free(session);
892 }
893
894 static void cleanup_session(gpointer user_data)
895 {
896         struct connman_session *session = user_data;
897         struct session_info *info = session->info;
898
899         DBG("remove %s", session->session_path);
900
901         g_hash_table_destroy(session->service_hash);
902         g_sequence_free(session->service_list);
903
904         if (info->entry != NULL &&
905                         info->entry->reason == CONNMAN_SESSION_REASON_CONNECT) {
906                 __connman_service_disconnect(info->entry->service);
907         }
908
909         free_session(session);
910 }
911
912 static enum connman_session_state service_to_session_state(enum connman_service_state state)
913 {
914         switch (state) {
915         case CONNMAN_SERVICE_STATE_UNKNOWN:
916         case CONNMAN_SERVICE_STATE_IDLE:
917         case CONNMAN_SERVICE_STATE_ASSOCIATION:
918         case CONNMAN_SERVICE_STATE_CONFIGURATION:
919         case CONNMAN_SERVICE_STATE_DISCONNECT:
920         case CONNMAN_SERVICE_STATE_FAILURE:
921                 break;
922         case CONNMAN_SERVICE_STATE_READY:
923                 return CONNMAN_SESSION_STATE_CONNECTED;
924         case CONNMAN_SERVICE_STATE_ONLINE:
925                 return CONNMAN_SESSION_STATE_ONLINE;
926         }
927
928         return CONNMAN_SESSION_STATE_DISCONNECTED;
929 }
930
931 static connman_bool_t is_connected(enum connman_service_state state)
932 {
933         switch (state) {
934         case CONNMAN_SERVICE_STATE_UNKNOWN:
935         case CONNMAN_SERVICE_STATE_IDLE:
936         case CONNMAN_SERVICE_STATE_ASSOCIATION:
937         case CONNMAN_SERVICE_STATE_CONFIGURATION:
938         case CONNMAN_SERVICE_STATE_DISCONNECT:
939         case CONNMAN_SERVICE_STATE_FAILURE:
940                 break;
941         case CONNMAN_SERVICE_STATE_READY:
942         case CONNMAN_SERVICE_STATE_ONLINE:
943                 return TRUE;
944         }
945
946         return FALSE;
947 }
948
949 static connman_bool_t is_connecting(enum connman_service_state state)
950 {
951         switch (state) {
952         case CONNMAN_SERVICE_STATE_UNKNOWN:
953         case CONNMAN_SERVICE_STATE_IDLE:
954                 break;
955         case CONNMAN_SERVICE_STATE_ASSOCIATION:
956         case CONNMAN_SERVICE_STATE_CONFIGURATION:
957                 return TRUE;
958         case CONNMAN_SERVICE_STATE_DISCONNECT:
959         case CONNMAN_SERVICE_STATE_FAILURE:
960         case CONNMAN_SERVICE_STATE_READY:
961         case CONNMAN_SERVICE_STATE_ONLINE:
962                 break;
963         }
964
965         return FALSE;
966 }
967
968 static connman_bool_t explicit_connect(enum connman_session_reason reason)
969 {
970         switch (reason) {
971         case CONNMAN_SESSION_REASON_UNKNOWN:
972         case CONNMAN_SESSION_REASON_FREE_RIDE:
973                 break;
974         case CONNMAN_SESSION_REASON_CONNECT:
975                 return TRUE;
976         }
977
978         return FALSE;
979 }
980
981 static connman_bool_t explicit_disconnect(struct session_info *info)
982 {
983         if (info->entry == NULL)
984                 return FALSE;
985
986         DBG("reason %s service %p state %d",
987                 reason2string(info->entry->reason),
988                 info->entry->service, info->entry->state);
989
990         if (info->entry->reason == CONNMAN_SESSION_REASON_UNKNOWN)
991                 return FALSE;
992
993         if (explicit_connect(info->entry->reason) == FALSE)
994                 return FALSE;
995
996         if (__connman_service_session_dec(info->entry->service) == FALSE)
997                 return FALSE;
998
999         return TRUE;
1000 }
1001
1002 struct pending_data {
1003         unsigned int timeout;
1004         struct service_entry *entry;
1005         gboolean (*cb)(gpointer);
1006 };
1007
1008 static void pending_timeout_free(gpointer data, gpointer user_data)
1009 {
1010         struct pending_data *pending = data;
1011
1012         DBG("pending %p timeout %d", pending, pending->timeout);
1013         g_source_remove(pending->timeout);
1014         g_free(pending);
1015 }
1016
1017 static void pending_timeout_remove_all(struct service_entry *entry)
1018 {
1019         DBG("");
1020
1021         g_slist_foreach(entry->pending_timeouts, pending_timeout_free, NULL);
1022         g_slist_free(entry->pending_timeouts);
1023         entry->pending_timeouts = NULL;
1024 }
1025
1026 static gboolean pending_timeout_cb(gpointer data)
1027 {
1028         struct pending_data *pending = data;
1029         struct service_entry *entry = pending->entry;
1030         gboolean ret;
1031
1032         DBG("pending %p timeout %d", pending, pending->timeout);
1033
1034         ret = pending->cb(pending->entry);
1035         if (ret == FALSE) {
1036                 entry->pending_timeouts =
1037                         g_slist_remove(entry->pending_timeouts,
1038                                         pending);
1039                 g_free(pending);
1040         }
1041         return ret;
1042 }
1043
1044 static connman_bool_t pending_timeout_add(unsigned int seconds,
1045                                         gboolean (*cb)(gpointer),
1046                                         struct service_entry *entry)
1047 {
1048         struct pending_data *pending = g_try_new0(struct pending_data, 1);
1049
1050         if (pending == NULL || cb == NULL || entry == NULL) {
1051                 g_free(pending);
1052                 return FALSE;
1053         }
1054
1055         pending->cb = cb;
1056         pending->entry = entry;
1057         pending->timeout = g_timeout_add_seconds(seconds, pending_timeout_cb,
1058                                                 pending);
1059         entry->pending_timeouts = g_slist_prepend(entry->pending_timeouts,
1060                                                 pending);
1061
1062         DBG("pending %p entry %p timeout id %d", pending, entry,
1063                 pending->timeout);
1064
1065         return TRUE;
1066 }
1067
1068 static gboolean call_disconnect(gpointer user_data)
1069 {
1070         struct service_entry *entry = user_data;
1071         struct connman_service *service = entry->service;
1072
1073         /*
1074          * TODO: We should mark this entry as pending work. In case
1075          * disconnect fails we just unassign this session from the
1076          * service and can't do anything later on it
1077          */
1078         DBG("disconnect service %p", service);
1079         __connman_service_disconnect(service);
1080
1081         return FALSE;
1082 }
1083
1084 static gboolean call_connect(gpointer user_data)
1085 {
1086         struct service_entry *entry = user_data;
1087         struct connman_service *service = entry->service;
1088
1089         DBG("connect service %p", service);
1090         __connman_service_connect(service);
1091
1092         return FALSE;
1093 }
1094
1095 static void deselect_service(struct session_info *info)
1096 {
1097         struct service_entry *entry;
1098         connman_bool_t disconnect, connected;
1099
1100         DBG("");
1101
1102         if (info->entry == NULL)
1103                 return;
1104
1105         disconnect = explicit_disconnect(info);
1106
1107         connected = is_connecting(info->entry->state) == TRUE ||
1108                         is_connected(info->entry->state) == TRUE;
1109
1110         info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1111         info->entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1112
1113         entry = info->entry;
1114         info->entry = NULL;
1115
1116         DBG("disconnect %d connected %d", disconnect, connected);
1117
1118         if (disconnect == TRUE && connected == TRUE)
1119                 pending_timeout_add(0, call_disconnect, entry);
1120 }
1121
1122 static void deselect_and_disconnect(struct connman_session *session)
1123 {
1124         struct session_info *info = session->info;
1125
1126         deselect_service(info);
1127
1128         info->reason = CONNMAN_SESSION_REASON_FREE_RIDE;
1129 }
1130
1131 static void select_connected_service(struct session_info *info,
1132                                         struct service_entry *entry)
1133 {
1134         enum connman_session_state state;
1135
1136         state = service_to_session_state(entry->state);
1137         if (is_type_matching_state(&state, info->config.type) == FALSE)
1138                 return;
1139
1140         info->state = state;
1141
1142         info->entry = entry;
1143         info->entry->reason = info->reason;
1144
1145         if (explicit_connect(info->reason) == FALSE)
1146                 return;
1147
1148         __connman_service_session_inc(info->entry->service);
1149 }
1150
1151 static void select_offline_service(struct session_info *info,
1152                                         struct service_entry *entry)
1153 {
1154         if (explicit_connect(info->reason) == FALSE)
1155                 return;
1156
1157         info->state = service_to_session_state(entry->state);
1158
1159         info->entry = entry;
1160         info->entry->reason = info->reason;
1161
1162         __connman_service_session_inc(info->entry->service);
1163         pending_timeout_add(0, call_connect, entry);
1164 }
1165
1166 static void select_service(struct session_info *info,
1167                                 struct service_entry *entry)
1168 {
1169         DBG("service %p", entry->service);
1170
1171         if (is_connected(entry->state) == TRUE)
1172                 select_connected_service(info, entry);
1173         else
1174                 select_offline_service(info, entry);
1175 }
1176
1177 static void select_and_connect(struct connman_session *session,
1178                                 enum connman_session_reason reason)
1179 {
1180         struct session_info *info = session->info;
1181         struct service_entry *entry = NULL;
1182         GSequenceIter *iter;
1183
1184         DBG("session %p reason %s", session, reason2string(reason));
1185
1186         info->reason = reason;
1187
1188         iter = g_sequence_get_begin_iter(session->service_list);
1189
1190         while (g_sequence_iter_is_end(iter) == FALSE) {
1191                 entry = g_sequence_get(iter);
1192
1193                 switch (entry->state) {
1194                 case CONNMAN_SERVICE_STATE_ASSOCIATION:
1195                 case CONNMAN_SERVICE_STATE_CONFIGURATION:
1196                 case CONNMAN_SERVICE_STATE_READY:
1197                 case CONNMAN_SERVICE_STATE_ONLINE:
1198                 case CONNMAN_SERVICE_STATE_IDLE:
1199                 case CONNMAN_SERVICE_STATE_DISCONNECT:
1200                         select_service(info, entry);
1201                         return;
1202                 case CONNMAN_SERVICE_STATE_UNKNOWN:
1203                 case CONNMAN_SERVICE_STATE_FAILURE:
1204                         break;
1205                 }
1206
1207                 iter = g_sequence_iter_next(iter);
1208         }
1209 }
1210
1211 static struct service_entry *create_service_entry(struct connman_service *service,
1212                                         const char *name,
1213                                         enum connman_service_state state)
1214 {
1215         struct service_entry *entry;
1216         enum connman_service_type type;
1217         int idx;
1218
1219         entry = g_try_new0(struct service_entry, 1);
1220         if (entry == NULL)
1221                 return entry;
1222
1223         entry->reason = CONNMAN_SESSION_REASON_UNKNOWN;
1224         entry->state = state;
1225         if (name != NULL)
1226                 entry->name = name;
1227         else
1228                 entry->name = "";
1229         entry->service = service;
1230
1231         idx = __connman_service_get_index(entry->service);
1232         entry->ifname = connman_inet_ifname(idx);
1233         if (entry->ifname == NULL)
1234                 entry->ifname = g_strdup("");
1235
1236         type = connman_service_get_type(entry->service);
1237         entry->bearer = service2bearer(type);
1238
1239         return entry;
1240 }
1241
1242 static void destroy_service_entry(gpointer data)
1243 {
1244         struct service_entry *entry = data;
1245
1246         pending_timeout_remove_all(entry);
1247         g_free(entry->ifname);
1248
1249         g_free(entry);
1250 }
1251
1252 static void populate_service_list(struct connman_session *session)
1253 {
1254         struct service_entry *entry;
1255         GSequenceIter *iter;
1256
1257         session->service_hash =
1258                 g_hash_table_new_full(g_direct_hash, g_direct_equal,
1259                                         NULL, NULL);
1260         session->service_list = __connman_service_get_list(session,
1261                                                         service_match,
1262                                                         create_service_entry,
1263                                                         destroy_service_entry);
1264
1265         g_sequence_sort(session->service_list, sort_services, session);
1266
1267         iter = g_sequence_get_begin_iter(session->service_list);
1268
1269         while (g_sequence_iter_is_end(iter) == FALSE) {
1270                 entry = g_sequence_get(iter);
1271
1272                 DBG("service %p type %s name %s", entry->service,
1273                         service2bearer(connman_service_get_type(entry->service)),
1274                         entry->name);
1275
1276                 g_hash_table_replace(session->service_hash,
1277                                         entry->service, iter);
1278
1279                 iter = g_sequence_iter_next(iter);
1280         }
1281 }
1282
1283 static void session_changed(struct connman_session *session,
1284                                 enum connman_session_trigger trigger)
1285 {
1286         struct session_info *info = session->info;
1287         struct session_info *info_last = session->info_last;
1288         GSequenceIter *service_iter = NULL, *service_iter_last = NULL;
1289         GSequence *service_list_last;
1290         GHashTable *service_hash_last;
1291
1292         /*
1293          * TODO: This only a placeholder for the 'real' algorithm to
1294          * play a bit around. So we are going to improve it step by step.
1295          */
1296
1297         DBG("session %p trigger %s reason %s", session, trigger2string(trigger),
1298                                                 reason2string(info->reason));
1299
1300         if (info->entry != NULL) {
1301                 enum connman_session_state state;
1302
1303                 state = service_to_session_state(info->entry->state);
1304
1305                 if (is_type_matching_state(&state, info->config.type) == TRUE)
1306                         info->state = state;
1307         }
1308
1309         switch (trigger) {
1310         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
1311                 DBG("ignore session changed event");
1312                 return;
1313         case CONNMAN_SESSION_TRIGGER_SETTING:
1314                 if (info->config.allowed_bearers != info_last->config.allowed_bearers) {
1315
1316                         service_hash_last = session->service_hash;
1317                         service_list_last = session->service_list;
1318
1319                         populate_service_list(session);
1320
1321                         if (info->entry != NULL) {
1322                                 service_iter_last = g_hash_table_lookup(
1323                                                         service_hash_last,
1324                                                         info->entry->service);
1325                                 service_iter = g_hash_table_lookup(
1326                                                         session->service_hash,
1327                                                         info->entry->service);
1328                         }
1329
1330                         if (service_iter == NULL && service_iter_last != NULL) {
1331                                 /*
1332                                  * The currently selected service is
1333                                  * not part of this session anymore.
1334                                  */
1335                                 deselect_and_disconnect(session);
1336                         }
1337
1338                         g_hash_table_remove_all(service_hash_last);
1339                         g_sequence_free(service_list_last);
1340                 }
1341
1342                 if (info->config.type != info_last->config.type) {
1343                         if (info->state >= CONNMAN_SESSION_STATE_CONNECTED &&
1344                                         is_type_matching_state(&info->state,
1345                                                         info->config.type) == FALSE)
1346                                 deselect_and_disconnect(session);
1347                 }
1348
1349                 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED) {
1350                         select_and_connect(session,
1351                                         CONNMAN_SESSION_REASON_FREE_RIDE);
1352                 }
1353
1354                 break;
1355         case CONNMAN_SESSION_TRIGGER_ECALL:
1356                 /*
1357                  * For the time beeing we fallback to normal connect
1358                  * strategy.
1359                  */
1360         case CONNMAN_SESSION_TRIGGER_CONNECT:
1361                 if (info->state >= CONNMAN_SESSION_STATE_CONNECTED) {
1362                         if (info->entry->reason == CONNMAN_SESSION_REASON_CONNECT)
1363                                 break;
1364                         info->entry->reason = CONNMAN_SESSION_REASON_CONNECT;
1365                         __connman_service_session_inc(info->entry->service);
1366                         break;
1367                 }
1368
1369                 if (info->entry != NULL &&
1370                                 is_connecting(info->entry->state) == TRUE) {
1371                         break;
1372                 }
1373
1374                 select_and_connect(session,
1375                                 CONNMAN_SESSION_REASON_CONNECT);
1376
1377                 break;
1378         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
1379                 deselect_and_disconnect(session);
1380
1381                 break;
1382         case CONNMAN_SESSION_TRIGGER_SERVICE:
1383                 if (info->entry != NULL &&
1384                         (is_connecting(info->entry->state) == TRUE ||
1385                                 is_connected(info->entry->state) == TRUE)) {
1386                         break;
1387                 }
1388
1389                 deselect_and_disconnect(session);
1390
1391                 if (info->reason == CONNMAN_SESSION_REASON_FREE_RIDE) {
1392                         select_and_connect(session, info->reason);
1393                 }
1394
1395                 break;
1396         }
1397
1398         session_notify(session);
1399 }
1400
1401 static DBusMessage *connect_session(DBusConnection *conn,
1402                                         DBusMessage *msg, void *user_data)
1403 {
1404         struct connman_session *session = user_data;
1405
1406         DBG("session %p", session);
1407
1408         if (ecall_session != NULL) {
1409                 if (ecall_session->ecall == TRUE && ecall_session != session)
1410                         return __connman_error_failed(msg, EBUSY);
1411
1412                 session->ecall = TRUE;
1413                 session_changed(session, CONNMAN_SESSION_TRIGGER_ECALL);
1414         } else
1415                 session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
1416
1417         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1418 }
1419
1420 static DBusMessage *disconnect_session(DBusConnection *conn,
1421                                         DBusMessage *msg, void *user_data)
1422 {
1423         struct connman_session *session = user_data;
1424
1425         DBG("session %p", session);
1426
1427         if (ecall_session != NULL) {
1428                 if (ecall_session->ecall == TRUE && ecall_session != session)
1429                         return __connman_error_failed(msg, EBUSY);
1430
1431                 session->ecall = FALSE;
1432         }
1433
1434         session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
1435
1436         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1437 }
1438
1439 static DBusMessage *change_session(DBusConnection *conn,
1440                                         DBusMessage *msg, void *user_data)
1441 {
1442         struct connman_session *session = user_data;
1443         struct session_info *info = session->info;
1444         DBusMessageIter iter, value;
1445         const char *name;
1446         const char *val;
1447         GSList *allowed_bearers;
1448         int err;
1449
1450         DBG("session %p", session);
1451         if (dbus_message_iter_init(msg, &iter) == FALSE)
1452                 return __connman_error_invalid_arguments(msg);
1453
1454         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1455                 return __connman_error_invalid_arguments(msg);
1456
1457         dbus_message_iter_get_basic(&iter, &name);
1458         dbus_message_iter_next(&iter);
1459
1460         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1461                 return __connman_error_invalid_arguments(msg);
1462
1463         dbus_message_iter_recurse(&iter, &value);
1464
1465         switch (dbus_message_iter_get_arg_type(&value)) {
1466         case DBUS_TYPE_ARRAY:
1467                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1468                         err = session_parse_allowed_bearers(&value,
1469                                                         &allowed_bearers);
1470                         if (err < 0)
1471                                 return __connman_error_failed(msg, err);
1472
1473                         connman_session_free_bearers(info->config.allowed_bearers);
1474                         err = apply_policy_on_bearers(
1475                                         session->policy_config->allowed_bearers,
1476                                         allowed_bearers,
1477                                         &info->config.allowed_bearers);
1478
1479                         connman_session_free_bearers(allowed_bearers);
1480                         if (err < 0)
1481                                 return __connman_error_failed(msg, err);
1482                 } else {
1483                         goto err;
1484                 }
1485                 break;
1486         case DBUS_TYPE_STRING:
1487                 if (g_str_equal(name, "ConnectionType") == TRUE) {
1488                         dbus_message_iter_get_basic(&value, &val);
1489                         info->config.type = apply_policy_on_type(
1490                                 session->policy_config->type,
1491                                 string2type(val));
1492                 } else {
1493                         goto err;
1494                 }
1495                 break;
1496         default:
1497                 goto err;
1498         }
1499
1500         session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1501
1502         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1503
1504 err:
1505         return __connman_error_invalid_arguments(msg);
1506 }
1507
1508 static void release_session(gpointer key, gpointer value, gpointer user_data)
1509 {
1510         struct connman_session *session = value;
1511         DBusMessage *message;
1512
1513         DBG("owner %s path %s", session->owner, session->notify_path);
1514
1515         if (session->notify_watch > 0)
1516                 g_dbus_remove_watch(connection, session->notify_watch);
1517
1518         g_dbus_unregister_interface(connection, session->session_path,
1519                                                 CONNMAN_SESSION_INTERFACE);
1520
1521         message = dbus_message_new_method_call(session->owner,
1522                                                 session->notify_path,
1523                                                 CONNMAN_NOTIFICATION_INTERFACE,
1524                                                 "Release");
1525         if (message == NULL)
1526                 return;
1527
1528         dbus_message_set_no_reply(message, TRUE);
1529
1530         g_dbus_send_message(connection, message);
1531 }
1532
1533 static int session_disconnect(struct connman_session *session)
1534 {
1535         DBG("session %p, %s", session, session->owner);
1536
1537         if (session->notify_watch > 0)
1538                 g_dbus_remove_watch(connection, session->notify_watch);
1539
1540         g_dbus_unregister_interface(connection, session->session_path,
1541                                                 CONNMAN_SESSION_INTERFACE);
1542
1543         deselect_and_disconnect(session);
1544
1545         g_hash_table_remove(session_hash, session->session_path);
1546
1547         return 0;
1548 }
1549
1550 static void owner_disconnect(DBusConnection *conn, void *user_data)
1551 {
1552         struct connman_session *session = user_data;
1553
1554         DBG("session %p, %s died", session, session->owner);
1555
1556         session_disconnect(session);
1557 }
1558
1559 static DBusMessage *destroy_session(DBusConnection *conn,
1560                                         DBusMessage *msg, void *user_data)
1561 {
1562         struct connman_session *session = user_data;
1563
1564         DBG("session %p", session);
1565
1566         if (ecall_session != NULL && ecall_session != session)
1567                 return __connman_error_failed(msg, EBUSY);
1568
1569         session_disconnect(session);
1570
1571         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1572 }
1573
1574 static const GDBusMethodTable session_methods[] = {
1575         { GDBUS_METHOD("Destroy", NULL, NULL, destroy_session) },
1576         { GDBUS_METHOD("Connect", NULL, NULL, connect_session) },
1577         { GDBUS_METHOD("Disconnect", NULL, NULL,
1578                         disconnect_session ) },
1579         { GDBUS_METHOD("Change",
1580                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
1581                         NULL, change_session) },
1582         { },
1583 };
1584
1585 int __connman_session_create(DBusMessage *msg)
1586 {
1587         const char *owner, *notify_path;
1588         char *session_path = NULL;
1589         DBusMessageIter iter, array;
1590         struct connman_session *session = NULL;
1591         struct session_info *info, *info_last;
1592         enum connman_session_type type = CONNMAN_SESSION_TYPE_ANY;
1593         GSList *allowed_bearers = NULL;
1594         connman_bool_t allowed_bearers_valid = FALSE;
1595         connman_bool_t type_valid = FALSE;
1596         int err;
1597
1598         owner = dbus_message_get_sender(msg);
1599
1600         DBG("owner %s", owner);
1601
1602         if (ecall_session != NULL && ecall_session->ecall == TRUE) {
1603                 /*
1604                  * If there is an emergency call already going on,
1605                  * ignore session creation attempt
1606                  */
1607                 err = -EBUSY;
1608                 goto err;
1609         }
1610
1611         dbus_message_iter_init(msg, &iter);
1612         dbus_message_iter_recurse(&iter, &array);
1613
1614         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1615                 DBusMessageIter entry, value;
1616                 const char *key, *val;
1617
1618                 dbus_message_iter_recurse(&array, &entry);
1619                 dbus_message_iter_get_basic(&entry, &key);
1620
1621                 dbus_message_iter_next(&entry);
1622                 dbus_message_iter_recurse(&entry, &value);
1623
1624                 switch (dbus_message_iter_get_arg_type(&value)) {
1625                 case DBUS_TYPE_ARRAY:
1626                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
1627                                 err = session_parse_allowed_bearers(&value,
1628                                                         &allowed_bearers);
1629                                 if (err < 0)
1630                                         goto err;
1631
1632                                 allowed_bearers_valid = TRUE;
1633                         } else {
1634                                 return -EINVAL;
1635                         }
1636                         break;
1637                 case DBUS_TYPE_STRING:
1638                         if (g_str_equal(key, "ConnectionType") == TRUE) {
1639                                 dbus_message_iter_get_basic(&value, &val);
1640                                 type = string2type(val);
1641                                 type_valid = TRUE;
1642                         } else {
1643                                 return -EINVAL;
1644                         }
1645                 }
1646                 dbus_message_iter_next(&array);
1647         }
1648
1649         dbus_message_iter_next(&iter);
1650         dbus_message_iter_get_basic(&iter, &notify_path);
1651
1652         if (notify_path == NULL) {
1653                 err = -EINVAL;
1654                 goto err;
1655         }
1656
1657         session_path = g_strdup_printf("/sessions%s", notify_path);
1658         if (session_path == NULL) {
1659                 err = -ENOMEM;
1660                 goto err;
1661         }
1662
1663         session = g_hash_table_lookup(session_hash, session_path);
1664         if (session != NULL) {
1665                 session = NULL;
1666                 err = -EEXIST;
1667                 goto err;
1668         }
1669
1670         session = g_try_new0(struct connman_session, 1);
1671         if (session == NULL) {
1672                 err = -ENOMEM;
1673                 goto err;
1674         }
1675
1676         session->info = g_try_new0(struct session_info, 1);
1677         if (session->info == NULL) {
1678                 err = -ENOMEM;
1679                 goto err;
1680         }
1681
1682         session->info_last = g_try_new0(struct session_info, 1);
1683         if (session->info_last == NULL) {
1684                 err = -ENOMEM;
1685                 goto err;
1686         }
1687
1688         info = session->info;
1689         info_last = session->info_last;
1690
1691         session->owner = g_strdup(owner);
1692         session->session_path = session_path;
1693         session->notify_path = g_strdup(notify_path);
1694         session->notify_watch =
1695                 g_dbus_add_disconnect_watch(connection, session->owner,
1696                                         owner_disconnect, session, NULL);
1697
1698         err = assign_policy_plugin(session);
1699         if (err < 0)
1700                 goto err;
1701         err = create_policy_config(session);
1702         if (err < 0)
1703                 goto err;
1704
1705         if (session->policy_config->ecall == TRUE)
1706                 ecall_session = session;
1707
1708         info->state = CONNMAN_SESSION_STATE_DISCONNECTED;
1709         if (type_valid == FALSE)
1710                 type = CONNMAN_SESSION_TYPE_ANY;
1711         info->config.type = apply_policy_on_type(
1712                                 session->policy_config->type,
1713                                 type);
1714         info->config.priority = session->policy_config->priority;
1715         info->config.roaming_policy = session->policy_config->roaming_policy;
1716         info->entry = NULL;
1717
1718         if (allowed_bearers_valid == FALSE) {
1719                 allowed_bearers = connman_session_allowed_bearers_any();
1720                 if (allowed_bearers == NULL) {
1721                         err = -ENOMEM;
1722                         goto err;
1723                 }
1724         }
1725
1726         err = apply_policy_on_bearers(
1727                         session->policy_config->allowed_bearers,
1728                         allowed_bearers,
1729                         &info->config.allowed_bearers);
1730         if (err < 0)
1731                 goto err;
1732
1733         g_hash_table_replace(session_hash, session->session_path, session);
1734
1735         DBG("add %s", session->session_path);
1736
1737         if (g_dbus_register_interface(connection, session->session_path,
1738                                         CONNMAN_SESSION_INTERFACE,
1739                                         session_methods, NULL,
1740                                         NULL, session, NULL) == FALSE) {
1741                 connman_error("Failed to register %s", session->session_path);
1742                 g_hash_table_remove(session_hash, session->session_path);
1743                 session = NULL;
1744
1745                 err = -EINVAL;
1746                 goto err;
1747         }
1748
1749         g_dbus_send_reply(connection, msg,
1750                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1751                                 DBUS_TYPE_INVALID);
1752
1753
1754         populate_service_list(session);
1755
1756         info_last->state = info->state;
1757         info_last->config.priority = info->config.priority;
1758         info_last->config.roaming_policy = info->config.roaming_policy;
1759         info_last->entry = info->entry;
1760         info_last->config.allowed_bearers = info->config.allowed_bearers;
1761
1762         session->append_all = TRUE;
1763
1764         session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1765
1766         return 0;
1767
1768 err:
1769         connman_error("Failed to create session");
1770
1771         if (session != NULL) {
1772                 if (session->info_last != NULL)
1773                         g_free(session->info_last);
1774                 if (session->info != NULL)
1775                         g_free(session->info);
1776                 g_free(session);
1777         }
1778
1779         g_free(session_path);
1780
1781         connman_session_free_bearers(allowed_bearers);
1782
1783         return err;
1784 }
1785
1786 int __connman_session_destroy(DBusMessage *msg)
1787 {
1788         const char *owner, *session_path;
1789         struct connman_session *session;
1790
1791         owner = dbus_message_get_sender(msg);
1792
1793         DBG("owner %s", owner);
1794
1795         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1796                                                         DBUS_TYPE_INVALID);
1797         if (session_path == NULL)
1798                 return -EINVAL;
1799
1800         session = g_hash_table_lookup(session_hash, session_path);
1801         if (session == NULL)
1802                 return -EINVAL;
1803
1804         if (g_strcmp0(owner, session->owner) != 0)
1805                 return -EACCES;
1806
1807         session_disconnect(session);
1808
1809         return 0;
1810 }
1811
1812 connman_bool_t __connman_session_mode()
1813 {
1814         return sessionmode;
1815 }
1816
1817 void __connman_session_set_mode(connman_bool_t enable)
1818 {
1819         DBG("enable %d", enable);
1820
1821         if (sessionmode != enable) {
1822                 sessionmode = enable;
1823
1824                 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
1825                                 CONNMAN_MANAGER_INTERFACE, "SessionMode",
1826                                 DBUS_TYPE_BOOLEAN, &sessionmode);
1827         }
1828
1829         if (sessionmode == TRUE)
1830                 __connman_service_disconnect_all();
1831 }
1832
1833 static void service_add(struct connman_service *service,
1834                         const char *name)
1835 {
1836         GHashTableIter iter;
1837         GSequenceIter *iter_service_list;
1838         gpointer key, value;
1839         struct connman_session *session;
1840         struct service_entry *entry;
1841
1842         DBG("service %p", service);
1843
1844         g_hash_table_iter_init(&iter, session_hash);
1845
1846         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1847                 session = value;
1848
1849                 if (service_match(session, service) == FALSE)
1850                         continue;
1851
1852                 entry = create_service_entry(service, name,
1853                                                 CONNMAN_SERVICE_STATE_IDLE);
1854                 if (entry == NULL)
1855                         continue;
1856
1857                 iter_service_list =
1858                         g_sequence_insert_sorted(session->service_list,
1859                                                         entry, sort_services,
1860                                                         session);
1861
1862                 g_hash_table_replace(session->service_hash, service,
1863                                         iter_service_list);
1864
1865                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1866         }
1867 }
1868
1869 static void service_remove(struct connman_service *service)
1870 {
1871
1872         GHashTableIter iter;
1873         gpointer key, value;
1874         struct connman_session *session;
1875         struct session_info *info;
1876
1877         DBG("service %p", service);
1878
1879         g_hash_table_iter_init(&iter, session_hash);
1880
1881         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1882                 GSequenceIter *seq_iter;
1883                 session = value;
1884                 info = session->info;
1885
1886                 seq_iter = g_hash_table_lookup(session->service_hash, service);
1887                 if (seq_iter == NULL)
1888                         continue;
1889
1890                 g_sequence_remove(seq_iter);
1891
1892                 if (info->entry != NULL && info->entry->service == service)
1893                         info->entry = NULL;
1894                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1895         }
1896 }
1897
1898 static void service_state_changed(struct connman_service *service,
1899                                         enum connman_service_state state)
1900 {
1901         GHashTableIter iter;
1902         gpointer key, value;
1903
1904         DBG("service %p state %d", service, state);
1905
1906         g_hash_table_iter_init(&iter, session_hash);
1907
1908         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1909                 struct connman_session *session = value;
1910                 GSequenceIter *service_iter;
1911
1912                 service_iter = g_hash_table_lookup(session->service_hash, service);
1913                 if (service_iter != NULL) {
1914                         struct service_entry *entry;
1915
1916                         entry = g_sequence_get(service_iter);
1917                         entry->state = state;
1918                 }
1919
1920                 session_changed(session,
1921                                 CONNMAN_SESSION_TRIGGER_SERVICE);
1922         }
1923 }
1924
1925 static void ipconfig_changed(struct connman_service *service,
1926                                 struct connman_ipconfig *ipconfig)
1927 {
1928         GHashTableIter iter;
1929         gpointer key, value;
1930         struct connman_session *session;
1931         struct session_info *info;
1932         enum connman_ipconfig_type type;
1933
1934         DBG("service %p ipconfig %p", service, ipconfig);
1935
1936         type = __connman_ipconfig_get_config_type(ipconfig);
1937
1938         g_hash_table_iter_init(&iter, session_hash);
1939
1940         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1941                 session = value;
1942                 info = session->info;
1943
1944                 if (info->state == CONNMAN_SESSION_STATE_DISCONNECTED)
1945                         continue;
1946
1947                 if (info->entry != NULL && info->entry->service == service) {
1948                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1949                                 ipconfig_ipv4_changed(session);
1950                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1951                                 ipconfig_ipv6_changed(session);
1952                 }
1953         }
1954 }
1955
1956 static struct connman_notifier session_notifier = {
1957         .name                   = "session",
1958         .service_add            = service_add,
1959         .service_remove         = service_remove,
1960         .service_state_changed  = service_state_changed,
1961         .ipconfig_changed       = ipconfig_changed,
1962 };
1963
1964 int __connman_session_init(void)
1965 {
1966         int err;
1967
1968         DBG("");
1969
1970         connection = connman_dbus_get_connection();
1971         if (connection == NULL)
1972                 return -1;
1973
1974         err = connman_notifier_register(&session_notifier);
1975         if (err < 0) {
1976                 dbus_connection_unref(connection);
1977                 return err;
1978         }
1979
1980         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1981                                                 NULL, cleanup_session);
1982
1983         sessionmode = FALSE;
1984         return 0;
1985 }
1986
1987 void __connman_session_cleanup(void)
1988 {
1989         DBG("");
1990
1991         if (connection == NULL)
1992                 return;
1993
1994         connman_notifier_unregister(&session_notifier);
1995
1996         g_hash_table_foreach(session_hash, release_session, NULL);
1997         g_hash_table_destroy(session_hash);
1998         session_hash = NULL;
1999
2000         dbus_connection_unref(connection);
2001 }