Creating seperate chains[STC_IN, STC_OUT, STC_FRWD] for STC Framework's rules.
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-monitor.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <linux/netlink.h>
18 #include <vconf.h>
19 #include <vconf-keys.h>
20
21 #include "stc-default-connection.h"
22 #include "helper-nl.h"
23 #include "helper-nfacct-rule.h"
24 #include "helper-net-cls.h"
25 #include "helper-cgroup.h"
26 #include "helper-iptables.h"
27 #include "counter.h"
28 #include "table-statistics.h"
29 #include "table-counters.h"
30 #include "stc-exception.h"
31 #include "stc-monitor.h"
32 #include "stc-manager-plugin.h"
33
34 #define MAX_INT_LENGTH 128
35 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
36
37 typedef struct {
38         stc_app_key_s *app_key;
39         stc_process_key_s *proc_key;
40         gboolean entry_removed;
41 } remove_pid_context_s;
42
43 typedef struct {
44         struct nfacct_rule *counter;
45         int64_t bytes;
46         gboolean data_limit_reached;
47 } classid_bytes_context_s;
48
49 static stc_system_s *g_system = NULL;
50
51 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
52 {
53         if (counter->intend == NFACCT_WARN)
54                 return NFACCT_JUMP_ACCEPT;
55         else if (counter->intend == NFACCT_BLOCK)
56                 return NFACCT_JUMP_REJECT;
57
58         return NFACCT_JUMP_UNKNOWN;
59 }
60
61 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
62 {
63         if (counter == NULL)
64                 return STC_ERROR_INVALID_PARAMETER;
65
66         counter->action = NFACCT_ACTION_INSERT;
67         counter->iotype = NFACCT_COUNTER_IN;
68         counter->jump = __get_jump_by_intend(counter);
69         counter->iptype = NFACCT_TYPE_IPV4;
70         counter->send_limit = 0;
71         counter->rcv_limit = 0;
72
73         return produce_net_rule(counter);
74 }
75
76 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
77 {
78         if (counter == NULL)
79                 return STC_ERROR_INVALID_PARAMETER;
80
81         counter->action = NFACCT_ACTION_INSERT;
82         counter->iotype = NFACCT_COUNTER_OUT;
83         counter->jump = __get_jump_by_intend(counter);
84         counter->iptype = NFACCT_TYPE_IPV4;
85         counter->send_limit = 0;
86         counter->rcv_limit = 0;
87
88         return produce_net_rule(counter);
89 }
90
91 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
92 {
93         if (counter == NULL)
94                 return STC_ERROR_INVALID_PARAMETER;
95
96         counter->action = NFACCT_ACTION_DELETE;
97         counter->iotype = NFACCT_COUNTER_IN;
98         counter->jump = __get_jump_by_intend(counter);
99         counter->iptype = NFACCT_TYPE_IPV4;
100         counter->send_limit = 0;
101         counter->rcv_limit = 0;
102
103         return produce_net_rule(counter);
104 }
105
106 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
107 {
108         if (counter == NULL)
109                 return STC_ERROR_INVALID_PARAMETER;
110
111         counter->action = NFACCT_ACTION_DELETE;
112         counter->iotype = NFACCT_COUNTER_OUT;
113         counter->jump = __get_jump_by_intend(counter);
114         counter->iptype = NFACCT_TYPE_IPV4;
115         counter->send_limit = 0;
116         counter->rcv_limit = 0;
117
118         return produce_net_rule(counter);
119 }
120
121 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
122 {
123         if (counter == NULL)
124                 return STC_ERROR_INVALID_PARAMETER;
125
126         counter->action = NFACCT_ACTION_INSERT;
127         counter->iotype = NFACCT_COUNTER_IN;
128         counter->jump = __get_jump_by_intend(counter);
129         counter->iptype = NFACCT_TYPE_IPV6;
130         counter->send_limit = 0;
131         counter->rcv_limit = 0;
132
133         return produce_net_rule(counter);
134 }
135
136 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
137 {
138         if (counter == NULL)
139                 return STC_ERROR_INVALID_PARAMETER;
140
141         counter->action = NFACCT_ACTION_INSERT;
142         counter->iotype = NFACCT_COUNTER_OUT;
143         counter->jump = __get_jump_by_intend(counter);
144         counter->iptype = NFACCT_TYPE_IPV6;
145         counter->send_limit = 0;
146         counter->rcv_limit = 0;
147
148         return produce_net_rule(counter);
149 }
150
151 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
152 {
153         if (counter == NULL)
154                 return STC_ERROR_INVALID_PARAMETER;
155
156         counter->action = NFACCT_ACTION_DELETE;
157         counter->iotype = NFACCT_COUNTER_IN;
158         counter->jump = __get_jump_by_intend(counter);
159         counter->iptype = NFACCT_TYPE_IPV6;
160         counter->send_limit = 0;
161         counter->rcv_limit = 0;
162
163         return produce_net_rule(counter);
164 }
165
166 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
167 {
168         if (counter == NULL)
169                 return STC_ERROR_INVALID_PARAMETER;
170
171         counter->action = NFACCT_ACTION_DELETE;
172         counter->iotype = NFACCT_COUNTER_OUT;
173         counter->jump = __get_jump_by_intend(counter);
174         counter->iptype = NFACCT_TYPE_IPV6;
175         counter->send_limit = 0;
176         counter->rcv_limit = 0;
177
178         return produce_net_rule(counter);
179 }
180
181 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
182                                         gpointer UNUSED user_data)
183 {
184         stc_process_key_s *key_a = (stc_process_key_s *)a;
185         stc_process_key_s *key_b = (stc_process_key_s *)b;
186
187         return key_a->pid - key_b->pid;
188 }
189
190 static void __processes_tree_value_free(gpointer data)
191 {
192         stc_process_value_s *value = (stc_process_value_s *)data;
193
194         FREE(value);
195 }
196
197 static void __processes_tree_key_free(gpointer data)
198 {
199         stc_process_key_s *key = (stc_process_key_s *)data;
200
201         FREE(key);
202 }
203
204 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
205                                    gpointer UNUSED user_data)
206 {
207         stc_app_key_s *key_a = (stc_app_key_s *)a;
208         stc_app_key_s *key_b = (stc_app_key_s *)b;
209         gint ret;
210
211         ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
212         if (ret)
213                 return ret;
214
215         return g_strcmp0(key_a->app_id, key_b->app_id);
216 }
217
218 static void __apps_tree_value_free(gpointer data)
219 {
220         stc_app_value_s *value = (stc_app_value_s *)data;
221
222         g_tree_destroy(value->processes);
223         value->processes = NULL;
224
225         FREE(value);
226 }
227
228 static void __apps_tree_key_free(gpointer data)
229 {
230         stc_app_key_s *key = (stc_app_key_s *)data;
231
232         g_free(key->pkg_id);
233         g_free(key->app_id);
234         FREE(key);
235 }
236
237 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
238                                     gpointer UNUSED user_data)
239 {
240         stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
241         stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
242         int ret;
243
244         ret = g_strcmp0(key_a->app_id, key_b->app_id);
245         if (ret != 0)
246                 return ret;
247
248         ret = g_strcmp0(key_a->ifname, key_b->ifname);
249         if (ret != 0)
250                 return ret;
251
252         ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
253         if (ret != 0)
254                 return ret;
255
256         ret = key_a->iftype - key_b->iftype;
257         if (ret != 0)
258                 return ret;
259
260         return 0;
261 }
262
263 static void __rstns_tree_value_free(gpointer data)
264 {
265         stc_rstn_value_s *value = (stc_rstn_value_s *)data;
266
267         FREE(value);
268 }
269
270 static void __rstns_tree_key_free(gpointer data)
271 {
272         stc_rstn_key_s *key = (stc_rstn_key_s *)data;
273
274         FREE(key->app_id);
275         FREE(key->ifname);
276         FREE(key->subscriber_id);
277         FREE(key);
278 }
279
280 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
281                                                gpointer data)
282 {
283         stc_process_key_s *proc_key = (stc_process_key_s *)key;
284         stc_process_value_s *proc_value = (stc_process_value_s *)value;
285
286         STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
287                  proc_key->pid, proc_value->ground);
288         return FALSE;
289 }
290
291 static void __processes_tree_printall(GTree *processes)
292 {
293         g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
294 }
295
296 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
297                                           gpointer data)
298 {
299         stc_app_key_s *app_key = (stc_app_key_s *)key;
300         stc_app_value_s *app_value = (stc_app_value_s *)value;
301
302         STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
303                  "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
304                  " counter [ in (%lld), out (%lld)]",
305                  app_key->pkg_id, app_key->app_id,
306                  app_value->type, app_value->classid,
307                  app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
308
309         __processes_tree_printall(app_value->processes);
310         return FALSE;
311 }
312
313 static void __apps_tree_printall(void)
314 {
315         g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
316 }
317
318 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
319                                                gpointer data)
320 {
321         remove_pid_context_s *context = (remove_pid_context_s *)data;
322         stc_app_value_s *app_value = (stc_app_value_s *)value;
323
324         if (!g_tree_remove(app_value->processes, context->proc_key)) {
325                 if (STC_DEBUG_LOG)
326                         STC_LOGD("key not found");
327                 return FALSE;
328         }
329
330         context->entry_removed = TRUE;
331         context->app_key = (stc_app_key_s *)key;
332
333         return TRUE;
334 }
335
336 static stc_app_value_s * __application_lookup(GTree *apps,
337                                               const stc_app_key_s *key)
338 {
339         stc_app_value_s *lookup;
340
341         ret_value_msg_if(apps == NULL, NULL, "apps is null!");
342
343         lookup = g_tree_lookup(apps, key);
344
345         return lookup;
346 }
347
348 static stc_process_value_s * __process_lookup(GTree *processes,
349                                               const stc_process_key_s *key)
350 {
351         stc_process_value_s *lookup;
352
353         ret_value_msg_if(processes == NULL, NULL, "processes is null!");
354
355         lookup = g_tree_lookup(processes, key);
356
357         return lookup;
358 }
359
360 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
361                                              gpointer data)
362 {
363         guint *pid_count = (guint *)data;
364         (*pid_count)++;
365         return TRUE;
366 }
367
368 static gboolean __add_application_monitor(gpointer key, gpointer value,
369                                           gpointer data)
370 {
371         stc_app_value_s *app_value = (stc_app_value_s *)value;
372         default_connection_s *connection = (default_connection_s *)data;
373         stc_s *stc = stc_get_manager();
374
375         if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
376             app_value->classid == STC_TOTAL_WIFI_CLASSID ||
377             app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
378                 return FALSE;
379
380         if (stc && connection && connection->ifname) {
381                 struct nfacct_rule counter;
382
383                 if (!stc->carg) {
384                         stc->carg = MALLOC0(counter_arg_s, 1);
385                         if (stc->carg == NULL)
386                                 return FALSE;
387
388                         stc->carg->sock = stc_monitor_get_counter_socket();
389                 }
390
391                 memset(&counter, 0, sizeof(struct nfacct_rule));
392
393                 counter.carg = stc->carg;
394                 counter.classid = app_value->classid;
395                 counter.intend = NFACCT_COUNTER;
396                 counter.iftype = connection->type;
397                 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
398
399                 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
400                         __add_iptables_in(&counter);
401                         __add_iptables_out(&counter);
402                 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
403                         __add_ip6tables_in(&counter);
404                         __add_ip6tables_out(&counter);
405                 } else {
406                         __add_iptables_in(&counter);
407                         __add_iptables_out(&counter);
408                         __add_ip6tables_in(&counter);
409                         __add_ip6tables_out(&counter);
410                 }
411         }
412
413         return FALSE;
414 }
415
416 static gboolean __remove_application_monitor(gpointer key, gpointer value,
417                                              gpointer data)
418 {
419         stc_app_value_s *app_value = (stc_app_value_s *)value;
420         default_connection_s *connection = (default_connection_s *)data;
421         stc_s *stc = stc_get_manager();
422
423         if (stc && connection && connection->ifname) {
424                 struct nfacct_rule counter;
425
426                 if (!stc->carg) {
427                         stc->carg = MALLOC0(counter_arg_s, 1);
428                         if (stc->carg == NULL)
429                                 return FALSE;
430
431                         stc->carg->sock = stc_monitor_get_counter_socket();
432                 }
433
434                 memset(&counter, 0, sizeof(struct nfacct_rule));
435
436                 counter.carg = stc->carg;
437                 counter.classid = app_value->classid;
438                 counter.intend = NFACCT_COUNTER;
439                 counter.iftype = connection->type;
440                 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
441
442                 __del_iptables_in(&counter);
443                 __del_iptables_out(&counter);
444                 __del_ip6tables_in(&counter);
445                 __del_ip6tables_out(&counter);
446         }
447
448         return FALSE;
449 }
450
451 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
452 {
453         STC_LOGI("rstn info => rstn_id [%llu], "
454                  "app_id [%s], classid [%lu], ifname [%s], "
455                  "iftype [%d], rst_state [%d], "
456                  "limit [ (%lld) bytes], "
457                  "warn_limit [ (%lld) bytes], "
458                  "counter [ (%lld) bytes], "
459                  "roaming [%d], subscriber_id [%s]",
460                  rstn_value->restriction_id,
461                  rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
462                  rstn_key->iftype, rstn_value->rst_state,
463                  rstn_value->data_limit,
464                  rstn_value->data_warn_limit,
465                  rstn_value->data_counter,
466                  rstn_key->roaming, rstn_key->subscriber_id);
467 }
468
469 static void __process_restriction(enum traffic_restriction_type rst_type,
470                                   stc_rstn_key_s *rstn_key,
471                                   stc_rstn_value_s *rstn_value, void *data)
472 {
473         int64_t effective_data_limit, effective_data_warn_limit;
474         default_connection_s *old_connection = (default_connection_s *)data;
475         default_connection_s *connection = NULL;
476
477         if (old_connection != NULL)
478                 connection = old_connection;
479         else
480                 connection = stc_get_default_connection();
481
482         /* no default ifname */
483         if (connection->ifname == NULL)
484                 return;
485
486         /* rstn not applicable for this interface */
487         if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
488             g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
489                 return;
490
491         /* classid is invalid */
492         if (rstn_value->classid == STC_UNKNOWN_CLASSID)
493                 return;
494
495         effective_data_limit = rstn_value->data_limit;
496         effective_data_warn_limit = rstn_value->data_warn_limit;
497
498         if (rst_type == RST_SET) {
499                 /* TODO: Change this to runtime memory */
500                 table_counters_info info;
501
502                 memset(&info, 0, sizeof(table_counters_info));
503                 table_counters_get(rstn_value->restriction_id, &info);
504
505                 effective_data_limit -= info.data_counter;
506                 effective_data_warn_limit -= info.data_counter;
507
508                 if (effective_data_limit < 0) {
509                         effective_data_limit = 0;
510                         rstn_value->data_limit_reached = TRUE;
511                 }
512
513                 if (effective_data_warn_limit < 0)
514                         effective_data_warn_limit = 0;
515
516                 STC_LOGD("datausage [%lld] bytes", info.data_counter);
517         }
518
519         STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
520                  "effective_data_warn_limit [%lld] bytes",
521                  rstn_value->restriction_id, effective_data_limit,
522                  effective_data_warn_limit);
523
524         switch (rst_type) {
525         case RST_SET:
526                 if (effective_data_limit <= 0) {
527                         char *default_ifname = stc_default_connection_get_ifname();
528                         struct nfacct_rule counter;
529                         stc_s *stc = stc_get_manager();
530                         if (!stc) {
531                                 g_free(default_ifname);
532                                 return;
533                         }
534
535                         if (!stc->carg) {
536                                 stc->carg = MALLOC0(counter_arg_s, 1);
537                                 if (stc->carg == NULL) {
538                                         g_free(default_ifname);
539                                         return;
540                                 }
541
542                                 stc->carg->sock =
543                                         stc_monitor_get_counter_socket();
544                         }
545
546                         counter.carg = stc->carg;
547                         counter.classid = rstn_value->classid;
548                         counter.intend = NFACCT_BLOCK;
549                         counter.iftype = rstn_key->iftype;
550                         g_strlcpy(counter.ifname, default_ifname,
551                                   MAX_IFACE_LENGTH);
552
553                         generate_counter_name(&counter);
554                         g_free(default_ifname);
555
556                         /* iptables rule */
557                         __add_iptables_in(&counter);
558                         __add_iptables_out(&counter);
559
560                         /* ip6tables rule */
561                         __add_ip6tables_in(&counter);
562                         __add_ip6tables_out(&counter);
563                 }
564
565                 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
566                 rstn_value->data_limit_reached = FALSE;
567                 break;
568         case RST_EXCLUDE:
569                 ;//Do Nothing
570                 break;
571         case RST_UNSET:
572                 {
573                         char *default_ifname = stc_default_connection_get_ifname();
574                         struct nfacct_rule counter;
575                         stc_s *stc = stc_get_manager();
576                         if (!stc) {
577                                 g_free(default_ifname);
578                                 return;
579                         }
580
581                         if (!stc->carg) {
582                                 stc->carg = MALLOC0(counter_arg_s, 1);
583                                 if (stc->carg == NULL) {
584                                         g_free(default_ifname);
585                                         return;
586                                 }
587
588                                 stc->carg->sock =
589                                         stc_monitor_get_counter_socket();
590                         }
591
592                         counter.carg = stc->carg;
593                         counter.classid = rstn_value->classid;
594                         counter.intend = NFACCT_BLOCK;
595                         counter.iftype = rstn_key->iftype;
596                         g_strlcpy(counter.ifname, default_ifname,
597                                   MAX_IFACE_LENGTH);
598
599                         generate_counter_name(&counter);
600                         g_free(default_ifname);
601
602                         /* iptables rule */
603                         __del_iptables_in(&counter);
604                         __del_iptables_out(&counter);
605
606                         /* ip6tables rule */
607                         __del_ip6tables_in(&counter);
608                         __del_ip6tables_out(&counter);
609
610                         rstn_value->rst_state = STC_RESTRICTION_REMOVED;
611                         rstn_value->data_limit_reached = FALSE;
612                 }
613                 break;
614         default:
615                 ;//Do Nothing
616         }
617 }
618
619 static gboolean __remove_rstns_foreach_application(gpointer key,
620                                                    gpointer value,
621                                                    gpointer data)
622 {
623         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
624         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
625         gchar *app_id = (gchar *)data;
626
627         /* rstn rule is not for applications */
628         if (rstn_key->app_id == NULL)
629                 goto out;
630
631         /* rstn rule is not for this application */
632         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
633                 goto out;
634
635         /* rstn rule is already removed */
636         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
637                 goto out;
638
639         /* remove restriction from system */
640         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
641
642         __print_rstn(rstn_key, rstn_value);
643 out:
644         return FALSE;
645 }
646
647 static void __remove_rstns_for_application(gchar *app_id)
648 {
649         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
650                        app_id);
651 }
652
653 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
654 {
655         stc_error_e ret = STC_ERROR_NONE;
656         guint pid_count = 0;
657         stc_app_value_s *lookup;
658
659         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
660
661         lookup = __application_lookup(g_system->apps, app_key);
662         if (!lookup) {
663                 STC_LOGE("app_key not found");
664                 return STC_ERROR_NO_DATA;
665         }
666
667         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
668                        &pid_count);
669
670         if (!pid_count) {
671                 /* remove nfacct rule for this classid */
672                 __remove_application_monitor((gpointer) app_key, lookup,
673                                              stc_get_default_connection());
674                 __remove_rstns_for_application(app_key->app_id);
675         }
676
677         if (!g_tree_remove(g_system->apps, app_key)) {
678                 ret = STC_ERROR_NO_DATA;
679                 STC_LOGE("key not found");
680         }
681
682         return ret;
683 }
684
685 static stc_error_e __close_contr_sock(stc_system_s *system)
686 {
687         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
688
689         /* close netlink socket for updating kernel counters */
690         if (system->contr_sock != -1) {
691                 close(system->contr_sock);
692                 system->contr_sock = -1;
693         }
694
695         if (system->contr_gsource_id != 0) {
696                 g_source_remove(system->contr_gsource_id);
697                 system->contr_gsource_id = 0;
698         }
699
700         return STC_ERROR_NONE;
701 }
702
703 static gboolean __process_contr_reply(GIOChannel *source,
704                                       GIOCondition condition,
705                                       gpointer user_data);
706
707 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
708 {
709         GIOChannel *gio = NULL;
710         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
711
712         /* close netlink socket for updating kernel counters */
713         if (system->contr_sock != -1) {
714                 close(system->contr_sock);
715                 system->contr_sock = -1;
716         }
717
718         if (system->contr_gsource_id != 0) {
719                 g_source_remove(system->contr_gsource_id);
720                 system->contr_gsource_id = 0;
721         }
722
723         /* create netlink socket for updating kernel counters */
724         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
725         if (system->contr_sock < 0) {
726                 STC_LOGE("failed to open socket");
727                 FREE(system);
728                 return STC_ERROR_FAIL;
729         }
730
731         gio = g_io_channel_unix_new(system->contr_sock);
732         system->contr_gsource_id =
733                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
734                                (GIOFunc) __process_contr_reply,
735                                NULL);
736         g_io_channel_unref(gio);
737
738         return STC_ERROR_NONE;
739 }
740
741 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
742                                       stc_rstn_value_s *rstn_value,
743                                       classid_bytes_context_s *context)
744 {
745         switch (context->counter->iotype) {
746         case NFACCT_COUNTER_IN:
747         case NFACCT_COUNTER_OUT:
748                 rstn_value->data_counter += context->bytes;
749
750                 if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
751                     rstn_value->warn_limit_crossed_notified == FALSE) {
752
753                         gboolean rv;
754                         char iftype[MAX_INT_LENGTH];
755                         char byte[MAX_INT_LENGTH];
756                         stc_s *stc = (stc_s *)stc_get_manager();
757                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
758
759                         /* emit signal */
760                         rv = stc_manager_dbus_emit_signal(stc->connection,
761                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
762                                                           STC_DBUS_INTERFACE_RESTRICTION,
763                                                           "WarnThresholdCrossed",
764                                                           g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
765                         if (rv == TRUE)
766                                 rstn_value->warn_limit_crossed_notified = TRUE;
767
768                         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
769                         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
770                         stc_send_warn_message_to_net_popup("warn threshold crossed",
771                                                            "warning_noti",
772                                                            rstn_key->app_id,
773                                                            iftype, byte);
774                 }
775
776                 if (rstn_value->data_counter >= rstn_value->data_limit &&
777                     rstn_value->rstn_limit_crossed_notified == FALSE) {
778
779                         gboolean rv;
780                         char iftype[MAX_INT_LENGTH];
781                         char byte[MAX_INT_LENGTH];
782                         stc_s *stc = (stc_s *)stc_get_manager();
783                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
784
785                         /* block immediately */
786                         context->counter->intend = NFACCT_BLOCK;
787                         __del_iptables_in(context->counter);
788                         __del_iptables_out(context->counter);
789                         __add_iptables_in(context->counter);
790                         __add_iptables_out(context->counter);
791
792                         __del_ip6tables_in(context->counter);
793                         __del_ip6tables_out(context->counter);
794                         __add_ip6tables_in(context->counter);
795                         __add_ip6tables_out(context->counter);
796                         context->counter->intend = NFACCT_COUNTER;
797
798                         rstn_value->data_limit_reached = TRUE;
799
800                         /* emit signal */
801                         rv = stc_manager_dbus_emit_signal(stc->connection,
802                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
803                                                           STC_DBUS_INTERFACE_RESTRICTION,
804                                                           "RestrictionThresholdCrossed",
805                                                           g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
806                         if (rv == TRUE)
807                                 rstn_value->rstn_limit_crossed_notified = TRUE;
808
809                         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
810                         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
811                         stc_send_restriction_message_to_net_popup("restriction threshold crossed",
812                                                                   "restriction_noti", rstn_key->app_id,
813                                                                   iftype, byte);
814                 }
815
816                 g_system->rstns_tree_updated = TRUE;
817                 __print_rstn(rstn_key, rstn_value);
818                 break;
819         default:
820                 STC_LOGE("unknown iotype");
821         }
822
823         return FALSE;
824 }
825
826 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
827                                                 stc_rstn_value_s *rstn_value,
828                                                 classid_bytes_context_s *context)
829 {
830         if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
831              context->counter->iftype == STC_IFACE_DATACALL) ||
832             (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
833              context->counter->iftype == STC_IFACE_WIFI) ||
834             (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
835              context->counter->iftype == STC_IFACE_BLUETOOTH)) {
836                 context->counter->classid = rstn_value->classid;
837                 return __rstn_counter_update(rstn_key, rstn_value, context);
838         }
839
840         return FALSE;
841 }
842
843 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
844                                                       gpointer value,
845                                                       gpointer data)
846 {
847         gboolean rv = FALSE;
848         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
849         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
850         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
851         uint32_t classid;
852
853         if (context->counter->intend != NFACCT_COUNTER)
854                 goto try_next_callback;
855
856         if (rstn_value->data_limit_reached == TRUE) {
857                 context->data_limit_reached = TRUE;
858                 goto try_next_callback;
859         }
860
861         classid = context->counter->classid;
862         rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
863
864         context->counter->classid = classid;
865         if (rstn_value->classid != context->counter->classid)
866                 goto try_next_callback;
867
868         rv = __rstn_counter_update(rstn_key, rstn_value, context);
869
870 try_next_callback:
871         return rv;
872 }
873
874 static gboolean __update_app_statistics(gpointer key, gpointer value,
875                                         gpointer data)
876 {
877         stc_app_key_s *app_key = (stc_app_key_s *)key;
878         stc_app_value_s *app_value = (stc_app_value_s *)value;
879         time_t *touch_time = (time_t *)data;
880         stc_db_classid_iftype_key stat_key;
881         stc_db_app_stats stat;
882         default_connection_s *default_connection = stc_get_default_connection();
883
884         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
885         memset(&stat, 0 , sizeof(stc_db_app_stats));
886
887         stat_key.classid = app_value->classid;
888         stat_key.iftype = default_connection->type;
889
890         if (STC_IFACE_DATACALL == stat_key.iftype)
891                 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
892         else
893                 stat_key.subscriber_id = g_strdup("none_subid");
894
895         g_strlcpy(stat_key.ifname, default_connection->ifname,
896                   MAX_IFACE_LENGTH);
897
898         stat.app_id = g_strdup(app_key->app_id);
899         stat.snd_count = app_value->counter.out_bytes;
900         stat.rcv_count = app_value->counter.in_bytes;
901         stat.is_roaming = default_connection->roaming;
902
903         if (strstr(stat.app_id, "_BACKGROUND")) {
904                 stat.ground = STC_APP_STATE_BACKGROUND;
905         } else {
906                 if (strstr(stat.app_id, "TOTAL_"))
907                         stat.ground = STC_APP_STATE_UNKNOWN;
908                 else
909                         stat.ground = STC_APP_STATE_FOREGROUND;
910         }
911
912         table_statistics_insert(&stat_key, &stat, *touch_time);
913
914         app_value->counter.out_bytes = 0;
915         app_value->counter.in_bytes = 0;
916
917         FREE(stat.app_id);
918         FREE(stat_key.subscriber_id);
919
920         return FALSE;
921 }
922
923 static gboolean __flush_apps_stats_to_database(gpointer user_data)
924 {
925         time_t current_time = time(0);
926
927         if (g_system->apps_tree_updated == FALSE)
928                 return G_SOURCE_REMOVE;
929
930         g_system->apps_tree_updated = FALSE;
931
932         if (g_system->apps)
933                 g_tree_foreach(g_system->apps,
934                                __update_app_statistics,
935                                &current_time);
936
937         STC_LOGI("Flushed app stats to database");
938         return G_SOURCE_REMOVE;
939 }
940
941 static gboolean __update_counter_statistics(gpointer key, gpointer value,
942                                             gpointer data)
943 {
944         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
945         table_counters_info info = {
946                 .restriction_id = rstn_value->restriction_id,
947                 .data_counter = rstn_value->data_counter
948         };
949
950         table_counters_update_counters(&info);
951
952         return FALSE;
953 }
954
955 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
956 {
957         time_t current_time = time(0);
958
959         if (g_system->rstns_tree_updated == FALSE)
960                 return G_SOURCE_REMOVE;
961
962         g_system->rstns_tree_updated = FALSE;
963
964         if (g_system->rstns)
965                 g_tree_foreach(g_system->rstns,
966                                __update_counter_statistics,
967                                &current_time);
968
969         STC_LOGI("Flushed rstns counters to database");
970         return G_SOURCE_REMOVE;
971 }
972
973 static void __app_counter_update(stc_app_key_s *app_key,
974                                  stc_app_value_s *app_value,
975                                  classid_bytes_context_s *context)
976 {
977         switch (context->counter->iotype) {
978         case NFACCT_COUNTER_IN:
979                 app_value->data_usage.in_bytes += context->bytes;
980                 app_value->counter.in_bytes = context->bytes;
981                 g_system->apps_tree_updated = TRUE;
982
983                 if (STC_DEBUG_LOG)
984                         __apps_tree_foreach_print(app_key, app_value, NULL);
985                 break;
986         case NFACCT_COUNTER_OUT:
987                 app_value->data_usage.out_bytes += context->bytes;
988                 app_value->counter.out_bytes = context->bytes;
989                 g_system->apps_tree_updated = TRUE;
990
991                 if (STC_DEBUG_LOG)
992                         __apps_tree_foreach_print(app_key, app_value, NULL);
993                 break;
994         default:
995                 STC_LOGE("unknown iotype");
996         }
997 }
998
999 static void __interface_counter_update(stc_app_key_s *app_key,
1000                                        stc_app_value_s *app_value,
1001                                        classid_bytes_context_s *context)
1002 {
1003         if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1004              context->counter->iftype == STC_IFACE_DATACALL) ||
1005             (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1006              context->counter->iftype == STC_IFACE_WIFI) ||
1007             (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1008              context->counter->iftype == STC_IFACE_BLUETOOTH))
1009                 __app_counter_update(app_key, app_value, context);
1010 }
1011
1012
1013 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1014                                                       gpointer value,
1015                                                       gpointer data)
1016 {
1017         stc_app_key_s *app_key = (stc_app_key_s *)key;
1018         stc_app_value_s *app_value = (stc_app_value_s *)value;
1019         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1020
1021         if (context->counter->intend != NFACCT_COUNTER)
1022                 goto try_next_callback;
1023
1024         __interface_counter_update(app_key, app_value, context);
1025
1026         if (app_value->classid != context->counter->classid)
1027                 goto try_next_callback;
1028
1029         __app_counter_update(app_key, app_value, context);
1030
1031 try_next_callback:
1032         return FALSE;
1033 }
1034
1035 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1036                                  struct counter_arg *carg)
1037 {
1038         struct nfacct_rule counter = {
1039                 .carg = carg,
1040                 .name = {0},
1041                 .ifname = {0},
1042                 0
1043         };
1044
1045         classid_bytes_context_s context = {
1046                 .counter = &counter,
1047                 .bytes = bytes,
1048                 .data_limit_reached = FALSE,
1049         };
1050
1051         if (STC_DEBUG_LOG)
1052                 STC_LOGD("cnt_name %s", cnt_name);
1053
1054         if (!recreate_counter_by_name(cnt_name, &counter)) {
1055                 STC_LOGE("Can't parse counter name %s", cnt_name);
1056                 return;
1057         }
1058
1059         STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1060                  context.counter->classid, context.counter->iftype,
1061                  context.counter->iotype, context.counter->intend,
1062                  context.counter->ifname, context.bytes);
1063
1064         if (g_system->rstns)
1065                 g_tree_foreach(g_system->rstns,
1066                                __rstn_counter_update_foreach_classid,
1067                                &context);
1068
1069         if (g_system->apps)
1070                 g_tree_foreach(g_system->apps,
1071                                __apps_counter_update_foreach_classid,
1072                                &context);
1073 }
1074
1075 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1076                            void *user_data)
1077 {
1078         struct counter_arg *carg = user_data;
1079         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1080         if (carg->initiate) {
1081                 /**
1082                  * TODO: this will be used when daemon starts to update existing
1083                  * counter data if present.
1084                  *
1085                  populate_counters(cnt_name, carg);
1086                  */
1087         } else {
1088                 int64_t *bytes_p =
1089                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1090                 int bytes = be64toh(*bytes_p);
1091                 if (bytes) {
1092                         ++carg->serialized_counters;
1093                         __fill_nfacct_result(cnt_name, bytes, carg);
1094                 }
1095         }
1096
1097         return 0;
1098 }
1099
1100 static int __post_fill_counters(void *user_data)
1101 {
1102         struct counter_arg *carg = user_data;
1103
1104         if (carg->initiate)
1105                 carg->initiate = 0;
1106
1107         return 0;
1108 }
1109
1110 static void __process_network_counter(struct genl *ans,
1111                                       struct counter_arg *carg)
1112 {
1113         struct netlink_serialization_params ser_params = {
1114                 .carg = carg,
1115                 .ans = ans,
1116                 .eval_attr = __fill_counters,
1117                 .post_eval_attr = __post_fill_counters,
1118         };
1119
1120         netlink_serialization_command *netlink =
1121                 netlink_create_command(&ser_params);
1122         if (!netlink) {
1123                 STC_LOGE("Can not create command");
1124                 return;
1125         }
1126
1127         netlink->deserialize_answer(&(netlink->params));
1128 }
1129
1130 static gboolean __process_contr_reply(GIOChannel *source,
1131                                       GIOCondition condition,
1132                                       gpointer user_data)
1133 {
1134         int sock = g_io_channel_unix_get_fd(source);
1135         struct genl *ans;
1136         int ret;
1137         stc_s *stc = stc_get_manager();
1138
1139         if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1140             (condition & G_IO_NVAL)) {
1141                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1142
1143                 STC_LOGE("Counter socket received G_IO event, closing socket."
1144                          "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
1145                          (condition & G_IO_ERR), (condition & G_IO_HUP),
1146                          (condition & G_IO_NVAL));
1147                 __close_and_reopen_contr_sock(g_system);
1148                 return FALSE;
1149         }
1150
1151         ans = MALLOC0(struct genl, 1);
1152         if (ans == NULL) {
1153                 STC_LOGE("Failed allocate memory to genl reply message");
1154                 return TRUE;
1155         }
1156
1157         if (stc == NULL) {
1158                 STC_LOGE("Can't get stc data");
1159                 goto out;
1160         }
1161
1162         ret = read_netlink(sock, ans, sizeof(struct genl));
1163         /* STC_LOGD("Counter data received ret [%d]", ret); */
1164         if (ret == 0)
1165                 goto out;
1166
1167         stc->carg->ans_len = ret;
1168         __process_network_counter(ans, stc->carg);
1169
1170         g_idle_add(__flush_apps_stats_to_database, NULL);
1171         g_idle_add(__flush_rstns_counter_to_database, NULL);
1172 out:
1173         FREE(ans);
1174         return TRUE;
1175 }
1176
1177 static gboolean __update_contr_cb(void *user_data)
1178 {
1179         /* Here we just sent command, answer we receive in another callback */
1180         stc_s *stc = stc_get_manager();
1181         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1182         if (!stc->carg) {
1183                 stc->carg = MALLOC0(counter_arg_s, 1);
1184                 if (stc->carg == NULL)
1185                         return TRUE;  /* we need to continue the timer */
1186
1187                 stc->carg->sock = stc_monitor_get_counter_socket();
1188         }
1189
1190         /* STC_LOGD("Get all counters"); */
1191         nfacct_send_get_all(stc->carg);
1192
1193         /* we need to continue the timer */
1194         return TRUE;
1195 }
1196 #if 0
1197 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1198                                           gpointer data)
1199 {
1200         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1201         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1202
1203         __print_rstn(rstn_key, rstn_value);
1204         return FALSE;
1205 }
1206
1207 static void __rstn_tree_printall(void)
1208 {
1209         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1210 }
1211 #endif
1212 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1213                                         const stc_rstn_key_s *key)
1214 {
1215         stc_rstn_value_s *lookup;
1216
1217         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1218
1219         lookup = g_tree_lookup(rstns_tree, key);
1220
1221         return lookup;
1222 }
1223
1224 static gboolean __remove_restriction(gpointer key, gpointer value,
1225                                      gpointer data)
1226 {
1227         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1228         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1229
1230         /* rstn rule is already removed */
1231         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
1232                 return FALSE;
1233
1234         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1235         __print_rstn(rstn_key, rstn_value);
1236         return FALSE;
1237 }
1238
1239 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1240                                         gpointer data)
1241 {
1242         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1243         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1244
1245         /* rstn rule is activated */
1246         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1247                 return FALSE;
1248
1249         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1250                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1251         else
1252                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1253
1254         __print_rstn(rstn_key, rstn_value);
1255
1256         return FALSE;
1257 }
1258
1259 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1260 {
1261         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1262         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1263
1264         /* rstn rule is activated */
1265         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1266                 return FALSE;
1267
1268         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1269                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1270         else
1271                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1272
1273         return FALSE;
1274 }
1275
1276 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1277 {
1278         stc_rstn_value_s *lookup_value;
1279
1280         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1281
1282         lookup_value = __rstn_lookup(g_system->rstns, key);
1283         if (!lookup_value) {
1284                 STC_LOGE("key not found");
1285                 return STC_ERROR_NO_DATA;
1286         }
1287
1288         __remove_restriction(key, lookup_value, NULL);
1289
1290         /* remove counter also */
1291         table_counters_delete(lookup_value->restriction_id);
1292
1293         if (!g_tree_remove(g_system->rstns, key)) {
1294                 STC_LOGD("key not found");
1295                 return STC_ERROR_NO_DATA;
1296         }
1297
1298         return STC_ERROR_NONE;
1299 }
1300
1301 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1302                                    stc_rstn_value_s *value, gboolean debug)
1303 {
1304         stc_rstn_value_s *rstn_value;
1305
1306         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1307
1308         rstn_value = __rstn_lookup(g_system->rstns, key);
1309         if (!rstn_value) {
1310                 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1311                 if (!rstn_key) {
1312                         STC_LOGE("rstn_key allocation failed");
1313                         return STC_ERROR_OUT_OF_MEMORY;
1314                 }
1315
1316                 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1317                 if (!rstn_value) {
1318                         STC_LOGE("rstn_value allocation failed");
1319                         FREE(rstn_key);
1320                         return STC_ERROR_OUT_OF_MEMORY;
1321                 }
1322
1323                 rstn_key->app_id = g_strdup(key->app_id);
1324                 rstn_key->ifname = g_strdup(key->ifname);
1325                 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1326                 rstn_key->iftype = key->iftype;
1327                 rstn_key->roaming = key->roaming;
1328
1329                 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1330         }
1331
1332         rstn_value->restriction_id = value->restriction_id;
1333         rstn_value->rst_state = value->rst_state;
1334         rstn_value->classid = value->classid;
1335         rstn_value->data_limit = value->data_limit;
1336         rstn_value->data_warn_limit = value->data_warn_limit;
1337         rstn_value->data_counter = 0;
1338         rstn_value->warn_limit_crossed_notified = FALSE;
1339         rstn_value->rstn_limit_crossed_notified = FALSE;
1340
1341         if (debug == TRUE)
1342                 __add_restriction_debug(key, rstn_value, NULL);
1343         else
1344                 __add_restriction(key, rstn_value, NULL);
1345
1346         return STC_ERROR_NONE;
1347 }
1348
1349 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1350                                             void *user_data)
1351 {
1352         stc_cb_ret_e ret = STC_CONTINUE;
1353
1354         stc_rstn_key_s key;
1355         stc_rstn_value_s value;
1356
1357         memset(&key, 0, sizeof(stc_rstn_key_s));
1358         memset(&value, 0, sizeof(stc_rstn_value_s));
1359
1360         key.app_id = g_strdup(info->app_id);
1361         key.ifname = g_strdup(info->ifname);
1362         key.subscriber_id = g_strdup(info->subscriber_id);
1363         key.iftype = info->iftype;
1364         key.roaming = info->roaming;
1365
1366         value.rst_state = info->rst_state;
1367         value.restriction_id = info->restriction_id;
1368
1369         if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1370                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1371         else
1372                 value.classid = STC_UNKNOWN_CLASSID;
1373
1374         value.data_limit = info->data_limit;
1375         value.data_warn_limit = info->data_warn_limit;
1376
1377         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1378                 ret = STC_CANCEL;
1379
1380         FREE(key.app_id);
1381         FREE(key.ifname);
1382         FREE(key.subscriber_id);
1383         return ret;
1384 }
1385
1386 static void __fill_restritions_list(void)
1387 {
1388         table_restrictions_foreach(__insert_restriction_cb, NULL);
1389         //__rstn_tree_printall();
1390 }
1391
1392 static gboolean __add_rstn_foreach_application(gpointer key,
1393                                                gpointer value,
1394                                                gpointer data)
1395 {
1396         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1397         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1398         gchar *app_id = (gchar *)data;
1399
1400         /* rstn rule is not for applications */
1401         if (rstn_key->app_id == NULL)
1402                 goto out;
1403
1404         /* rstn rule is not for this application */
1405         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1406                 goto out;
1407
1408         /* rstn rule is already applied */
1409         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1410                 goto out;
1411
1412         /* add restriction to system */
1413         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1414                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1415         else
1416                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1417
1418         __print_rstn(rstn_key, rstn_value);
1419 out:
1420         return FALSE;
1421 }
1422
1423 static void __add_rstns_for_application(gchar *app_id)
1424 {
1425         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1426                        app_id);
1427 }
1428
1429 static void __add_application_by_interface(const char *app_id)
1430 {
1431         stc_app_key_s app_key;
1432         stc_app_value_s app_value;
1433
1434         if (app_id == NULL)
1435                 return;
1436
1437         memset(&app_key, 0, sizeof(stc_app_key_s));
1438         memset(&app_value, 0, sizeof(stc_app_value_s));
1439
1440         app_key.pkg_id = g_strdup(app_id);
1441         app_key.app_id = g_strdup(app_id);
1442
1443         app_value.type = STC_APP_TYPE_NONE;
1444         app_value.processes = NULL;
1445         app_value.counter.in_bytes = 0;
1446         app_value.counter.out_bytes = 0;
1447
1448         stc_monitor_application_add(app_key, app_value);
1449
1450         FREE(app_key.pkg_id);
1451         FREE(app_key.app_id);
1452 }
1453
1454 static int __vconf_get_int(const char *key, int *value)
1455 {
1456         int ret = 0;
1457
1458         ret = vconf_get_int(key, value);
1459         if (ret != VCONF_OK) {
1460                 STC_LOGE("Failed to get vconfkey [%s] value", key);
1461                 return -1;
1462         }
1463
1464         return 0;
1465 }
1466
1467 static int __vconf_set_int(const char *key, int value)
1468 {
1469         int ret = 0;
1470
1471         ret = vconf_set_int(key, value);
1472         if (ret != VCONF_OK) {
1473                 STC_LOGE("Failed to set vconfkey [%s] value", key);
1474                 return -1;
1475         }
1476
1477         return 0;
1478 }
1479
1480 static guint __get_background_state(void)
1481 {
1482         return g_system->background_state;;
1483 }
1484
1485 static void __set_background_state(guint state)
1486 {
1487         g_system->background_state = state;
1488 }
1489
1490 static gboolean __processes_tree_foreach_background(gpointer key,
1491                                                     gpointer value,
1492                                                     gpointer data)
1493 {
1494         stc_process_key_s *proc_key = (stc_process_key_s *)key;
1495         stc_app_key_s *app_key = (stc_app_key_s *)data;
1496
1497         if (g_system->background_state)
1498                 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1499         else
1500                 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1501
1502         return FALSE;
1503 }
1504
1505 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1506                                         gpointer data)
1507 {
1508         stc_app_key_s *app_key = (stc_app_key_s *)key;
1509         stc_app_value_s *app_value = (stc_app_value_s *)value;
1510
1511         if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1512                 g_tree_foreach(app_value->processes,
1513                                __processes_tree_foreach_background, app_key);
1514
1515         return FALSE;
1516 }
1517
1518 static stc_error_e __process_update_background(void)
1519 {
1520         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1521
1522         g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1523
1524         return STC_ERROR_NONE;
1525 }
1526
1527 #if 0
1528 static void __excn_hash_foreach_print(gpointer key, gpointer value,
1529                                           gpointer data)
1530 {
1531         const char *process_name = key;
1532         const char *exe_type = value;
1533
1534         STC_LOGI("excn info => process_name [%s] exe_type [%s]",
1535                 process_name, exe_type);
1536 }
1537
1538 static void __excn_hash_printall(void)
1539 {
1540         g_hash_table_foreach(g_system->excns_hash,
1541                 __excn_hash_foreach_print, NULL);
1542 }
1543 #endif
1544
1545 static gboolean __remove_exception_app(gpointer key, gpointer value,
1546                                         gpointer data)
1547 {
1548         const char *exe_type = value;
1549
1550         if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
1551                 return TRUE;
1552
1553         return FALSE;
1554 }
1555
1556 static void __remove_exception_appall(void)
1557 {
1558         g_hash_table_foreach_remove(g_system->excns_hash,
1559                 __remove_exception_app, NULL);
1560 }
1561
1562 static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
1563                                             void *user_data)
1564 {
1565         stc_cb_ret_e ret = STC_CONTINUE;
1566
1567         if (g_hash_table_insert(g_system->excns_hash,
1568                         g_strdup(info->process_name),
1569                         g_strdup(info->exe_type)) != TRUE)
1570                 ret = STC_CANCEL;
1571
1572         return ret;
1573 }
1574
1575 static void __fill_exceptions_list(void)
1576 {
1577         table_exceptions_foreach(__insert_exception_cb, NULL);
1578         pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1579
1580         /* __excn_hash_printall(); */
1581 }
1582
1583 static gboolean __update_exceptions_app_list(void *user_data)
1584 {
1585         __remove_exception_appall();
1586         pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
1587
1588         /* __excn_hash_printall(); */
1589
1590         return TRUE;
1591 }
1592
1593 stc_error_e stc_monitor_init(void)
1594 {
1595         stc_system_s *system = MALLOC0(stc_system_s, 1);
1596         GIOChannel *gio = NULL;
1597
1598         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1599
1600         /* initializing current classid */
1601         init_current_classid();
1602
1603         /* initializing cgroups */
1604         cgroup_init();
1605
1606         /* creating monitored application tree */
1607         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1608                                        __apps_tree_key_free,
1609                                        __apps_tree_value_free);
1610
1611         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1612                                         __rstns_tree_key_free,
1613                                         __rstns_tree_value_free);
1614
1615         system->excns_hash = g_hash_table_new_full(g_str_hash,
1616                                         g_str_equal, g_free, g_free);
1617
1618         /* create netlink socket for updating kernel counters */
1619         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1620         if (system->contr_sock < 0) {
1621                 STC_LOGE("failed to open socket");
1622                 FREE(system);
1623                 return STC_ERROR_FAIL;
1624         }
1625
1626         gio = g_io_channel_unix_new(system->contr_sock);
1627         system->contr_gsource_id =
1628                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1629                                (GIOFunc) __process_contr_reply,
1630                                NULL);
1631         g_io_channel_unref(gio);
1632
1633         g_system = system;
1634
1635         __add_application_by_interface(STC_TOTAL_DATACALL);
1636         __add_application_by_interface(STC_TOTAL_WIFI);
1637         __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1638         __add_application_by_interface(STC_TOTAL_IPV4);
1639         __add_application_by_interface(STC_TOTAL_IPV6);
1640
1641         /* creating restriction rules tree */
1642         __update_contr_cb(NULL);
1643
1644         /* registering periodic kernel counters update callback */
1645         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1646                                                          __update_contr_cb,
1647                                                          NULL);
1648         if (g_system->contr_timer_id == 0) {
1649                 STC_LOGE("Failed to register kernel counters update timer");
1650                 __close_contr_sock(g_system);
1651                 return STC_ERROR_FAIL;
1652         }
1653
1654         __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
1655                         (int *)&g_system->background_state);
1656
1657         __fill_exceptions_list();
1658         __fill_restritions_list();
1659
1660         g_system->excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
1661                                                          __update_exceptions_app_list,
1662                                                          NULL);
1663
1664         return STC_ERROR_NONE;
1665 }
1666
1667 stc_error_e stc_monitor_deinit(void)
1668 {
1669         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1670
1671         /* close netlink socket for updating kernel counters */
1672         __close_contr_sock(g_system);
1673
1674         /* remove kernel counters update timer */
1675         if (g_system->contr_timer_id > 0) {
1676                 g_source_remove(g_system->contr_timer_id);
1677                 g_system->contr_timer_id = 0;
1678         }
1679
1680         /* remove exceptions app list update timer */
1681         if (g_system->excns_timer_id > 0) {
1682                 g_source_remove(g_system->excns_timer_id);
1683                 g_system->excns_timer_id = 0;
1684         }
1685
1686         /* destroy monitored application tree */
1687         g_tree_destroy(g_system->apps);
1688         g_system->apps = NULL;
1689
1690         /* destroy restriction rules tree */
1691         g_tree_destroy(g_system->rstns);
1692         g_system->rstns = NULL;
1693
1694         /* destroy exception hash table */
1695         g_hash_table_destroy(g_system->excns_hash);
1696         g_system->excns_hash = NULL;
1697
1698         FREE(g_system);
1699
1700         return STC_ERROR_NONE;
1701 }
1702
1703 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1704                                         const stc_app_value_s app_value)
1705 {
1706         stc_error_e ret = STC_ERROR_NONE;
1707         stc_app_key_s *key;
1708         stc_app_value_s *value;
1709         stc_app_value_s *lookup;
1710
1711         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1712
1713         lookup = __application_lookup(g_system->apps, &app_key);
1714         if (lookup) {
1715                 STC_LOGD("app_key already present");
1716                 return STC_ERROR_NONE;
1717         }
1718
1719         key = MALLOC0(stc_app_key_s, 1);
1720         if (!key) {
1721                 STC_LOGE("key allocation failed");
1722                 return STC_ERROR_OUT_OF_MEMORY;
1723         }
1724
1725         value = MALLOC0(stc_app_value_s, 1);
1726         if (!value) {
1727                 STC_LOGE("value allocation failed");
1728                 FREE(key);
1729                 return STC_ERROR_OUT_OF_MEMORY;
1730         }
1731
1732         key->app_id = g_strdup(app_key.app_id);
1733         key->pkg_id = g_strdup(app_key.pkg_id);
1734
1735         value->type = app_value.type;
1736         value->data_usage.in_bytes = app_value.data_usage.in_bytes;
1737         value->data_usage.out_bytes = app_value.data_usage.out_bytes;
1738
1739         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1740                                            __processes_tree_key_free,
1741                                            __processes_tree_value_free);
1742
1743         /* create cgroup and update classid */
1744         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1745
1746         g_tree_insert(g_system->apps, key, value);
1747
1748         /* add nfacct rule for this classid */
1749         __add_application_monitor(key, value, stc_get_default_connection());
1750         __add_rstns_for_application(app_key.app_id);
1751
1752         return ret;
1753 }
1754
1755 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1756                                     const stc_process_key_s proc_key,
1757                                     const stc_process_value_s proc_value)
1758 {
1759         stc_error_e ret = STC_ERROR_NONE;
1760         stc_app_value_s *app_lookup;
1761         stc_process_key_s *key;
1762         stc_process_value_s *value;
1763         stc_process_value_s *proc_lookup;
1764
1765         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1766
1767         app_lookup = __application_lookup(g_system->apps, &app_key);
1768         if (!app_lookup) {
1769                 STC_LOGD("app_key not found");
1770                 return STC_ERROR_FAIL;
1771         }
1772
1773         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1774         if (proc_lookup) {
1775                 STC_LOGD("proc_key already present");
1776                 return STC_ERROR_NONE;
1777         }
1778
1779         key = MALLOC0(stc_process_key_s, 1);
1780         if (!key) {
1781                 STC_LOGE("key allocation failed");
1782                 return STC_ERROR_OUT_OF_MEMORY;
1783         }
1784
1785         value = MALLOC0(stc_process_value_s, 1);
1786         if (!value) {
1787                 STC_LOGE("value allocation failed");
1788                 FREE(key);
1789                 return STC_ERROR_OUT_OF_MEMORY;
1790         }
1791
1792         key->pid = proc_key.pid;
1793
1794         value->ground = proc_value.ground;
1795
1796         g_tree_insert(app_lookup->processes, key, value);
1797
1798         /* add pid to application cgroup */
1799         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1800
1801         if (STC_DEBUG_LOG)
1802                 __apps_tree_printall();
1803
1804         return ret;
1805 }
1806
1807 stc_error_e stc_monitor_process_remove(pid_t pid)
1808 {
1809         stc_error_e ret = STC_ERROR_NONE;
1810         stc_process_key_s proc_key = {
1811                 .pid = pid
1812         };
1813
1814         remove_pid_context_s context = {
1815                 .app_key = NULL,
1816                 .proc_key = &proc_key,
1817                 .entry_removed = FALSE,
1818         };
1819
1820         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1821
1822         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1823                        &context);
1824
1825         if (context.entry_removed)
1826                 __application_remove_if_empty(context.app_key);
1827
1828         if (STC_DEBUG_LOG)
1829                 __apps_tree_printall();
1830
1831         return ret;
1832 }
1833
1834 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1835                                               const stc_process_key_s proc_key,
1836                                               stc_app_state_e ground)
1837 {
1838         stc_error_e ret = STC_ERROR_NONE;
1839         stc_app_value_s *app_lookup;
1840         stc_process_value_s *proc_lookup;
1841
1842         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1843
1844         app_lookup = __application_lookup(g_system->apps, &app_key);
1845         if (!app_lookup) {
1846                 STC_LOGD("app_key not found");
1847                 return STC_ERROR_FAIL;
1848         }
1849
1850         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1851         if (!proc_lookup) {
1852                 STC_LOGD("proc_key not found");
1853                 return STC_ERROR_FAIL;
1854         }
1855
1856         if (proc_lookup->ground != ground)
1857                 proc_lookup->ground = ground;
1858
1859         if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
1860                 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
1861         else
1862                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1863
1864         return ret;
1865 }
1866
1867 void stc_monitor_update_rstn_by_default_connection(void *data)
1868 {
1869         static default_connection_s old_connection;
1870         default_connection_s *new_connection = (default_connection_s *)data;
1871
1872         if (old_connection.path != NULL) {
1873                 if (g_system->apps)
1874                         g_tree_foreach(g_system->apps,
1875                                        __remove_application_monitor,
1876                                        (gpointer)&old_connection);
1877
1878                 if (g_system->rstns)
1879                         g_tree_foreach(g_system->rstns,
1880                                        __remove_restriction,
1881                                        (gpointer)&old_connection);
1882
1883                 iptables_flush_chains();
1884         }
1885
1886         FREE(old_connection.path);
1887         FREE(old_connection.ifname);
1888         old_connection.type = 0;
1889         old_connection.roaming = 0;
1890
1891         if (new_connection != NULL && new_connection->path != NULL) {
1892                 if (g_system->apps)
1893                         g_tree_foreach(g_system->apps,
1894                                        __add_application_monitor,
1895                                        (gpointer)new_connection);
1896
1897                 if (g_system->rstns)
1898                         g_tree_foreach(g_system->rstns, __add_restriction,
1899                                        NULL);
1900
1901                 old_connection.path = g_strdup(new_connection->path);
1902                 old_connection.ifname = g_strdup(new_connection->ifname);
1903                 old_connection.type = new_connection->type;
1904                 old_connection.roaming = new_connection->roaming;
1905         }
1906 }
1907
1908 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1909 {
1910         stc_error_e ret;
1911
1912         stc_rstn_key_s key;
1913         stc_rstn_value_s value;
1914
1915         memset(&key, 0, sizeof(stc_rstn_key_s));
1916         memset(&value, 0, sizeof(stc_rstn_value_s));
1917
1918         key.app_id = g_strdup(info->app_id);
1919         key.ifname = g_strdup(info->ifname);
1920         key.subscriber_id = g_strdup(info->subscriber_id);
1921         key.iftype = info->iftype;
1922         key.roaming = info->roaming;
1923
1924         value.rst_state = info->rst_state;
1925         value.restriction_id = info->restriction_id;
1926
1927         if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1928                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1929         else
1930                 value.classid = STC_UNKNOWN_CLASSID;
1931
1932         if (value.classid == STC_BACKGROUND_APP_CLASSID) {
1933                 __set_background_state(TRUE);
1934                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1935                 __process_update_background();
1936         }
1937
1938         value.data_limit = info->data_limit;
1939         value.data_warn_limit = info->data_warn_limit;
1940
1941         ret = __rstn_tree_add(&key, &value, TRUE);
1942
1943         FREE(key.app_id);
1944         FREE(key.ifname);
1945         FREE(key.subscriber_id);
1946         return ret;
1947 }
1948
1949 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1950 {
1951         stc_error_e ret;
1952
1953         stc_rstn_key_s key = {
1954                 .app_id = g_strdup(info->app_id),
1955                 .ifname = g_strdup(info->ifname),
1956                 .subscriber_id = g_strdup(info->subscriber_id),
1957                 .iftype = info->iftype,
1958                 .roaming = info->roaming,
1959         };
1960
1961         if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
1962                 __set_background_state(FALSE);
1963                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
1964                 __process_update_background();
1965         }
1966
1967         ret = __rstn_tree_remove(&key);
1968
1969         FREE(key.app_id);
1970         FREE(key.ifname);
1971         FREE(key.subscriber_id);
1972         return ret;
1973 }
1974
1975 stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
1976 {
1977         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1978
1979         char *exe_type = NULL;
1980
1981         exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline);
1982         if (!exe_type)
1983                 return STC_ERROR_NO_DATA;
1984
1985         return STC_ERROR_NONE;
1986 }
1987
1988 int stc_monitor_get_counter_socket(void)
1989 {
1990         return g_system->contr_sock;
1991 }