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