4f1129cb74de0ec388dc2df3b95828d3e15a38ef
[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-time.h"
32 #include "stc-manager-plugin-appstatus.h"
33 #include "stc-manager-plugin-exception.h"
34 #include "stc-manager-plugin-tether.h"
35
36 #define GRANULARITY 10
37 #define MAX_INT_LENGTH 128
38
39 #ifndef VCONFKEY_STC_BACKGROUND_STATE
40 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
41 #endif
42
43 #ifndef VCONFKEY_SETAPPL_DATA_RESTRICTION_INT
44 #define VCONFKEY_SETAPPL_DATA_RESTRICTION_INT "db/setting/data_restriction"
45 #endif
46
47 typedef struct {
48         time_t now;
49         time_t month_start_ts;
50         time_t week_start_ts;
51         time_t day_start_ts;
52         int is_updated;
53 } reset_time_limits_context_s;
54
55 typedef struct {
56         stc_app_key_s *app_key;
57         stc_process_key_s *proc_key;
58         gboolean entry_removed;
59 } remove_pid_context_s;
60
61 typedef struct {
62         struct nfacct_rule *counter;
63         int64_t bytes;
64         gboolean data_limit_exceeded;
65 } classid_bytes_context_s;
66
67 static stc_system_s *g_system = NULL;
68
69 //LCOV_EXCL_START
70 static int __vconf_get_int(const char *key, int *value)
71 {
72         int ret = 0;
73
74         ret = vconf_get_int(key, value);
75         if (ret != VCONF_OK) {
76                 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
77                 return -1; //LCOV_EXCL_LINE
78         }
79
80         return 0;
81 }
82
83 static int __vconf_set_int(const char *key, int value)
84 {
85         int ret = 0;
86
87         ret = vconf_set_int(key, value);
88         if (ret != VCONF_OK) {
89                 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
90                 return -1; //LCOV_EXCL_LINE
91         }
92
93         return 0;
94 }
95 //LCOV_EXCL_STOP
96
97 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
98 {
99         if (counter->intend == NFACCT_WARN)
100                 return NFACCT_JUMP_ACCEPT;
101         else if (counter->intend == NFACCT_BLOCK)
102                 return NFACCT_JUMP_REJECT;
103         else if (counter->intend == NFACCT_ALLOW)
104                 return NFACCT_JUMP_ACCEPT;
105         else if (counter->intend == NFACCT_TETH_BLOCK)
106                 return NFACCT_JUMP_REJECT;
107         else if (counter->intend == NFACCT_TETH_ALLOW)
108                 return NFACCT_JUMP_ACCEPT;
109
110         return NFACCT_JUMP_UNKNOWN;
111 }
112
113 static stc_error_e __add_iptables_tether_in(struct nfacct_rule *counter,
114                 const gchar *ipaddr)
115 {
116         int ret;
117
118         if (counter == NULL || ipaddr == NULL)
119                 return STC_ERROR_INVALID_PARAMETER;
120
121         counter->action = NFACCT_ACTION_INSERT;
122         counter->iotype = NFACCT_COUNTER_IN;
123         counter->jump = __get_jump_by_intend(counter);
124         counter->iptype = NFACCT_TYPE_IPV4;
125         counter->send_limit = 0;
126         counter->rcv_limit = 0;
127         counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
128         counter->src_ip1 = g_strdup(ipaddr);
129
130         ret = produce_net_rule(counter);
131
132         FREE(counter->src_ip1);
133         counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
134         return ret;
135 }
136
137 static stc_error_e __add_iptables_tether_out(struct nfacct_rule *counter,
138                 const gchar *ipaddr)
139 {
140         int ret;
141
142         if (counter == NULL || ipaddr == NULL)
143                 return STC_ERROR_INVALID_PARAMETER;
144
145         counter->action = NFACCT_ACTION_INSERT;
146         counter->iotype = NFACCT_COUNTER_OUT;
147         counter->jump = __get_jump_by_intend(counter);
148         counter->iptype = NFACCT_TYPE_IPV4;
149         counter->send_limit = 0;
150         counter->rcv_limit = 0;
151         counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
152         counter->dst_ip1 = g_strdup(ipaddr);
153
154         ret = produce_net_rule(counter);
155
156         FREE(counter->dst_ip1);
157         counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
158         return ret;
159 }
160
161 static stc_error_e __del_iptables_tether_in(struct nfacct_rule *counter,
162                 const gchar *ipaddr)
163 {
164         int ret;
165
166         if (counter == NULL || ipaddr == NULL)
167                 return STC_ERROR_INVALID_PARAMETER;
168
169         counter->action = NFACCT_ACTION_DELETE;
170         counter->iotype = NFACCT_COUNTER_IN;
171         counter->jump = __get_jump_by_intend(counter);
172         counter->iptype = NFACCT_TYPE_IPV4;
173         counter->send_limit = 0;
174         counter->rcv_limit = 0;
175         counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
176         counter->src_ip1 = g_strdup(ipaddr);
177
178         ret = produce_net_rule(counter);
179
180         FREE(counter->src_ip1);
181         counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
182         return ret;
183 }
184
185 static stc_error_e __del_iptables_tether_out(struct nfacct_rule *counter,
186                 const gchar *ipaddr)
187 {
188         int ret;
189
190         if (counter == NULL || ipaddr == NULL)
191                 return STC_ERROR_INVALID_PARAMETER;
192
193         counter->action = NFACCT_ACTION_DELETE;
194         counter->iotype = NFACCT_COUNTER_OUT;
195         counter->jump = __get_jump_by_intend(counter);
196         counter->iptype = NFACCT_TYPE_IPV4;
197         counter->send_limit = 0;
198         counter->rcv_limit = 0;
199         counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
200         counter->dst_ip1 = g_strdup(ipaddr);
201
202         ret = produce_net_rule(counter);
203
204         FREE(counter->dst_ip1);
205         counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
206         return ret;
207 }
208
209 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
210 {
211         if (counter == NULL)
212                 return STC_ERROR_INVALID_PARAMETER;
213
214         counter->action = NFACCT_ACTION_INSERT;
215         counter->iotype = NFACCT_COUNTER_IN;
216         counter->jump = __get_jump_by_intend(counter);
217         counter->iptype = NFACCT_TYPE_IPV4;
218         counter->send_limit = 0;
219         counter->rcv_limit = 0;
220
221         return produce_net_rule(counter);
222 }
223
224 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
225 {
226         if (counter == NULL)
227                 return STC_ERROR_INVALID_PARAMETER;
228
229         counter->action = NFACCT_ACTION_INSERT;
230         counter->iotype = NFACCT_COUNTER_OUT;
231         counter->jump = __get_jump_by_intend(counter);
232         counter->iptype = NFACCT_TYPE_IPV4;
233         counter->send_limit = 0;
234         counter->rcv_limit = 0;
235
236         return produce_net_rule(counter);
237 }
238
239 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
240 {
241         if (counter == NULL)
242                 return STC_ERROR_INVALID_PARAMETER;
243
244         counter->action = NFACCT_ACTION_DELETE;
245         counter->iotype = NFACCT_COUNTER_IN;
246         counter->jump = __get_jump_by_intend(counter);
247         counter->iptype = NFACCT_TYPE_IPV4;
248         counter->send_limit = 0;
249         counter->rcv_limit = 0;
250
251         return produce_net_rule(counter);
252 }
253
254 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
255 {
256         if (counter == NULL)
257                 return STC_ERROR_INVALID_PARAMETER;
258
259         counter->action = NFACCT_ACTION_DELETE;
260         counter->iotype = NFACCT_COUNTER_OUT;
261         counter->jump = __get_jump_by_intend(counter);
262         counter->iptype = NFACCT_TYPE_IPV4;
263         counter->send_limit = 0;
264         counter->rcv_limit = 0;
265
266         return produce_net_rule(counter);
267 }
268
269 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
270 {
271         if (counter == NULL)
272                 return STC_ERROR_INVALID_PARAMETER;
273
274         counter->action = NFACCT_ACTION_INSERT;
275         counter->iotype = NFACCT_COUNTER_IN;
276         counter->jump = __get_jump_by_intend(counter);
277         counter->iptype = NFACCT_TYPE_IPV6;
278         counter->send_limit = 0;
279         counter->rcv_limit = 0;
280
281         return produce_net_rule(counter);
282 }
283
284 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
285 {
286         if (counter == NULL)
287                 return STC_ERROR_INVALID_PARAMETER;
288
289         counter->action = NFACCT_ACTION_INSERT;
290         counter->iotype = NFACCT_COUNTER_OUT;
291         counter->jump = __get_jump_by_intend(counter);
292         counter->iptype = NFACCT_TYPE_IPV6;
293         counter->send_limit = 0;
294         counter->rcv_limit = 0;
295
296         return produce_net_rule(counter);
297 }
298
299 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
300 {
301         if (counter == NULL)
302                 return STC_ERROR_INVALID_PARAMETER;
303
304         counter->action = NFACCT_ACTION_DELETE;
305         counter->iotype = NFACCT_COUNTER_IN;
306         counter->jump = __get_jump_by_intend(counter);
307         counter->iptype = NFACCT_TYPE_IPV6;
308         counter->send_limit = 0;
309         counter->rcv_limit = 0;
310
311         return produce_net_rule(counter);
312 }
313
314 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
315 {
316         if (counter == NULL)
317                 return STC_ERROR_INVALID_PARAMETER;
318
319         counter->action = NFACCT_ACTION_DELETE;
320         counter->iotype = NFACCT_COUNTER_OUT;
321         counter->jump = __get_jump_by_intend(counter);
322         counter->iptype = NFACCT_TYPE_IPV6;
323         counter->send_limit = 0;
324         counter->rcv_limit = 0;
325
326         return produce_net_rule(counter);
327 }
328
329 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
330                                         gpointer UNUSED user_data)
331 {
332         stc_process_key_s *key_a = (stc_process_key_s *)a;
333         stc_process_key_s *key_b = (stc_process_key_s *)b;
334
335         return key_a->pid - key_b->pid;
336 }
337
338 static void __processes_tree_value_free(gpointer data)
339 {
340         stc_process_value_s *value = (stc_process_value_s *)data;
341
342         FREE(value);
343 }
344
345 static void __processes_tree_key_free(gpointer data)
346 {
347         stc_process_key_s *key = (stc_process_key_s *)data;
348
349         FREE(key);
350 }
351
352 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
353                                    gpointer UNUSED user_data)
354 {
355         stc_app_key_s *key_a = (stc_app_key_s *)a;
356         stc_app_key_s *key_b = (stc_app_key_s *)b;
357         gint ret;
358
359         ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
360         if (ret)
361                 return ret;
362
363         return g_strcmp0(key_a->app_id, key_b->app_id);
364 }
365
366 static void __apps_tree_value_free(gpointer data)
367 {
368         stc_app_value_s *value = (stc_app_value_s *)data;
369
370         g_tree_destroy(value->processes);
371         value->processes = NULL;
372
373         FREE(value);
374 }
375
376 static void __apps_tree_key_free(gpointer data)
377 {
378         stc_app_key_s *key = (stc_app_key_s *)data;
379
380         g_free(key->pkg_id);
381         g_free(key->app_id);
382         FREE(key);
383 }
384
385 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
386                                     gpointer UNUSED user_data)
387 {
388         stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
389         stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
390         int ret;
391
392         ret = g_strcmp0(key_a->app_id, key_b->app_id);
393         if (ret != 0)
394                 return ret;
395
396         ret = g_strcmp0(key_a->ifname, key_b->ifname);
397         if (ret != 0)
398                 return ret;
399
400         ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
401         if (ret != 0)
402                 return ret;
403
404         ret = key_a->iftype - key_b->iftype;
405         if (ret != 0)
406                 return ret;
407
408         ret = key_a->roaming - key_b->roaming;
409         if (ret != 0)
410                 return ret;
411
412         return 0;
413 }
414
415 static void __rstns_tree_value_free(gpointer data)
416 {
417         stc_rstn_value_s *value = (stc_rstn_value_s *)data;
418
419         FREE(value);
420 }
421
422 static void __rstns_tree_key_free(gpointer data)
423 {
424         stc_rstn_key_s *key = (stc_rstn_key_s *)data;
425
426         FREE(key->app_id);
427         FREE(key->ifname);
428         FREE(key->subscriber_id);
429         FREE(key);
430 }
431
432 /*
433 //LCOV_EXCL_START
434 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
435                                                gpointer data)
436 {
437         stc_process_key_s *proc_key = (stc_process_key_s *)key;
438         stc_process_value_s *proc_value = (stc_process_value_s *)value;
439
440         STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
441                  proc_key->pid, proc_value->ground);
442         return FALSE;
443 }
444
445 static void __processes_tree_printall(GTree *processes)
446 {
447         g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
448 }
449
450 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
451                                           gpointer data)
452 {
453         stc_app_key_s *app_key = (stc_app_key_s *)key;
454         stc_app_value_s *app_value = (stc_app_value_s *)value;
455
456         STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
457                  "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
458                  " counter [ in (%lld), out (%lld)]",
459                  app_key->pkg_id, app_key->app_id,
460                  app_value->type, app_value->classid,
461                  app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
462
463         __processes_tree_printall(app_value->processes);
464         return FALSE;
465 }
466
467 static void __apps_tree_printall(void)
468 {
469         g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
470 }
471 //LCOV_EXCL_STOP
472 */
473
474 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
475                                                gpointer data)
476 {
477         remove_pid_context_s *context = (remove_pid_context_s *)data;
478         stc_app_value_s *app_value = (stc_app_value_s *)value;
479
480         if (!g_tree_remove(app_value->processes, context->proc_key))
481                 return FALSE;
482
483         context->entry_removed = TRUE;
484         context->app_key = (stc_app_key_s *)key;
485
486         return TRUE;
487 }
488
489 static stc_app_value_s * __application_lookup(GTree *apps,
490                                               const stc_app_key_s *key)
491 {
492         stc_app_value_s *lookup;
493
494         ret_value_msg_if(apps == NULL, NULL, "apps is null!");
495
496         lookup = g_tree_lookup(apps, key);
497
498         return lookup;
499 }
500
501 static stc_process_value_s * __process_lookup(GTree *processes,
502                                               const stc_process_key_s *key)
503 {
504         stc_process_value_s *lookup;
505
506         ret_value_msg_if(processes == NULL, NULL, "processes is null!");
507
508         lookup = g_tree_lookup(processes, key);
509
510         return lookup;
511 }
512
513 //LCOV_EXCL_START
514 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
515                                              gpointer data)
516 {
517         guint *pid_count = (guint *)data;
518         (*pid_count)++;
519         return TRUE;
520 }
521 //LCOV_EXCL_STOP
522
523 static gboolean __add_application_monitor_for_tethering(gpointer key,
524                                     gpointer value, gpointer data)
525 {
526         stc_app_value_s *app_value = (stc_app_value_s *)value;
527         stc_app_key_s *app_key = (stc_app_key_s *)key;
528         default_connection_s *connection = (default_connection_s *)data;
529         stc_s *stc = stc_get_manager();
530         struct nfacct_rule counter;
531         char *ipaddr = NULL;
532         int ret;
533
534         STC_LOGI("add appid(%s) classid(%d)", app_key->app_id,
535                         app_value->classid);
536
537         if (stc == NULL || connection == NULL)
538                 return FALSE;
539
540         if (!stc->carg) {
541                 stc->carg = MALLOC0(counter_arg_s, 1);
542                 if (stc->carg == NULL)
543                         return FALSE;
544
545                 stc->carg->sock = stc_monitor_get_counter_socket();
546         }
547
548         memset(&counter, 0, sizeof(struct nfacct_rule));
549
550         counter.carg = stc->carg;
551         counter.classid = app_value->classid;
552         counter.intend = NFACCT_TETH_COUNTER;
553
554         if (connection->tether_state != TRUE ||
555                         connection->tether_iface.ifname == NULL)
556                 return FALSE;
557
558         counter.iftype = connection->tether_iface.type;
559         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
560
561         /* get the ip address of the station based on its mac address */
562         ret = stc_plugin_tether_get_station_ip(app_value->mac, &ipaddr);
563         if (ret != STC_ERROR_NONE)
564                 return FALSE;
565
566         /* tethering iptables rule */
567         __add_iptables_tether_in(&counter, ipaddr);
568         __add_iptables_tether_out(&counter, ipaddr);
569
570         g_free(ipaddr);
571         return FALSE;
572 }
573
574 static gboolean __remove_application_monitor_for_tethering(gpointer key, gpointer value,
575                                     gpointer data)
576 {
577         stc_app_value_s *app_value = (stc_app_value_s *)value;
578         stc_app_key_s *app_key = (stc_app_key_s *)key;
579         default_connection_s *connection = (default_connection_s *)data;
580         stc_s *stc = stc_get_manager();
581         struct nfacct_rule counter;
582         char *ipaddr = NULL;
583         int ret;
584
585         STC_LOGI("remove appid(%s) classid(%d)", app_key->app_id,
586                         app_value->classid);
587
588         if (stc == NULL || connection == NULL)
589                 return FALSE;
590
591         if (!stc->carg) {
592                 stc->carg = MALLOC0(counter_arg_s, 1);
593                 if (stc->carg == NULL)
594                         return FALSE;
595
596                 stc->carg->sock = stc_monitor_get_counter_socket();
597         }
598
599         memset(&counter, 0, sizeof(struct nfacct_rule));
600
601         counter.carg = stc->carg;
602         counter.classid = app_value->classid;
603         counter.intend = NFACCT_TETH_COUNTER;
604
605         if (connection->tether_state != TRUE ||
606                         connection->tether_iface.ifname == NULL)
607                 return FALSE;
608
609         counter.iftype = connection->tether_iface.type;
610         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
611
612         /* get the ip address of the station based on its mac address */
613         ret = stc_plugin_tether_get_station_ip(app_value->mac, &ipaddr);
614         if (ret != STC_ERROR_NONE)
615                 return FALSE;
616
617         __del_iptables_tether_in(&counter, ipaddr);
618         __del_iptables_tether_out(&counter, ipaddr);
619
620         g_free(ipaddr);
621         return FALSE;
622 }
623
624 static gboolean __add_application_monitor(gpointer key, gpointer value,
625                                           gpointer data)
626 {
627         stc_app_value_s *app_value = (stc_app_value_s *)value;
628         stc_app_key_s *app_key = (stc_app_key_s *)key;
629         default_connection_s *connection = (default_connection_s *)data;
630         stc_s *stc = stc_get_manager();
631
632         if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
633             app_value->classid == STC_TOTAL_WIFI_CLASSID ||
634             app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
635                 return FALSE;
636
637         if (stc && connection && connection->ifname) {
638                 struct nfacct_rule counter;
639
640                 if (!stc->carg) {
641                         stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
642                         if (stc->carg == NULL) //LCOV_EXCL_LINE
643                                 return FALSE; //LCOV_EXCL_LINE
644
645                         stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
646                 }
647
648                 memset(&counter, 0, sizeof(struct nfacct_rule));
649
650                 counter.carg = stc->carg;
651                 counter.classid = app_value->classid;
652                 counter.intend = NFACCT_COUNTER;
653
654                 if (connection->tether_state == TRUE &&
655                         connection->tether_iface.ifname != NULL &&
656                         app_value->classid == STC_TETHERING_APP_CLASSID) {
657                         counter.iftype = connection->tether_iface.type;
658                         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
659                 } else {
660                         counter.iftype = connection->type;
661                         g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
662                 }
663
664                 if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX) &&
665                                 app_value->classid != STC_TETHERING_APP_CLASSID) {
666                         __add_application_monitor_for_tethering(key, value, data);
667                 } else if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
668                         __add_iptables_in(&counter);
669                         __add_iptables_out(&counter);
670                 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
671                         __add_ip6tables_in(&counter);
672                         __add_ip6tables_out(&counter);
673                 } else {
674                         __add_iptables_in(&counter);
675                         __add_iptables_out(&counter);
676                         __add_ip6tables_in(&counter);
677                         __add_ip6tables_out(&counter);
678                 }
679         }
680
681         return FALSE;
682 }
683
684 static gboolean __remove_application_monitor(gpointer key, gpointer value,
685                                              gpointer data)
686 {
687         stc_app_value_s *app_value = (stc_app_value_s *)value;
688         stc_app_key_s *app_key = (stc_app_key_s *)key;
689         default_connection_s *connection = (default_connection_s *)data;
690         stc_s *stc = stc_get_manager();
691
692         if (stc && connection && connection->ifname) {
693                 struct nfacct_rule counter;
694
695                 if (!stc->carg) {
696                         stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
697                         if (stc->carg == NULL) //LCOV_EXCL_LINE
698                                 return FALSE; //LCOV_EXCL_LINE
699
700                         stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
701                 }
702
703                 memset(&counter, 0, sizeof(struct nfacct_rule));
704
705                 counter.carg = stc->carg;
706                 counter.classid = app_value->classid;
707                 counter.intend = NFACCT_COUNTER;
708
709                 if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX) &&
710                                 app_value->classid != STC_TETHERING_APP_CLASSID) {
711                         __remove_application_monitor_for_tethering(key, value, data);
712                         return FALSE;
713                 } else if (connection->tether_state == FALSE &&
714                         connection->tether_iface.ifname != NULL &&
715                         app_value->classid == STC_TETHERING_APP_CLASSID) {
716                         counter.iftype = connection->tether_iface.type;
717                         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
718                 } else {
719                         counter.iftype = connection->type;
720                         g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
721                 }
722
723                 __del_iptables_in(&counter);
724                 __del_iptables_out(&counter);
725                 __del_ip6tables_in(&counter);
726                 __del_ip6tables_out(&counter);
727         }
728
729         return FALSE;
730 }
731
732 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
733 {
734         STC_LOGI("rstn info => rstn_id [%llu], "
735                  "app_id [%s], classid [%u], ifname [%s], "
736                  "iftype [%d], rstn_state [%d], rstn_type [%d], "
737                  "month_start_date [%d], limit [ (%lld) bytes], "
738                  "warn_limit [ (%lld) bytes], "
739                  "monthly_limit [ (%lld) bytes], "
740                  "weekly_limit [ (%lld) bytes], "
741                  "daily_limit [ (%lld) bytes], "
742                  "data_counter [ (%lld) bytes], "
743                  "warn_counter [ (%lld) bytes], "
744                  "monthly_counter [ (%lld) bytes], "
745                  "weekly_counter [ (%lld) bytes], "
746                  "daily_counter [ (%lld) bytes], "
747                  "roaming [%d], subscriber_id [%s]",
748                  rstn_value->restriction_id,
749                  rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
750                  rstn_key->iftype, rstn_value->rstn_state, rstn_value->rstn_type,
751                  rstn_value->month_start_date,
752                  rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA],
753                  rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN],
754                  rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY],
755                  rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY],
756                  rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY],
757                  rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
758                  rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
759                  rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
760                  rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
761                  rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY],
762                  rstn_key->roaming, rstn_key->subscriber_id);
763 }
764
765 static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
766                                 stc_iface_type_e iftype)
767 {
768         char *default_ifname = stc_default_connection_get_ifname();
769         default_connection_s *connection = stc_get_default_connection();
770         struct nfacct_rule counter;
771         stc_s *stc = stc_get_manager();
772         if (!stc) {
773                 g_free(default_ifname); //LCOV_EXCL_LINE
774                 return; //LCOV_EXCL_LINE
775         }
776
777         if (!stc->carg) {
778                 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
779                 if (stc->carg == NULL) { //LCOV_EXCL_LINE
780                         g_free(default_ifname); //LCOV_EXCL_LINE
781                         return; //LCOV_EXCL_LINE
782                 }
783
784                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
785         }
786
787         memset(&counter, 0, sizeof(struct nfacct_rule));
788
789         counter.carg = stc->carg;
790         counter.classid = classid;
791         counter.intend = intend;
792
793         if (connection && connection->tether_iface.ifname != NULL &&
794                 classid == STC_TETHERING_APP_CLASSID) {
795                 counter.iftype = connection->tether_iface.type;
796                 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
797         } else {
798                 counter.iftype = iftype;
799                 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
800         }
801
802         g_free(default_ifname);
803
804         /* iptables rule */
805         __add_iptables_in(&counter);
806         __add_iptables_out(&counter);
807
808         /* ip6tables rule */
809         __add_ip6tables_in(&counter);
810         __add_ip6tables_out(&counter);
811 }
812
813 static void __add_tethering_iptables_rule(int64_t classid, gchar *mac,
814                 nfacct_rule_intend intend, stc_iface_type_e iftype)
815 {
816         default_connection_s *connection = stc_get_default_connection();
817         struct nfacct_rule counter;
818         stc_s *stc = stc_get_manager();
819         char *ipaddr = NULL;
820         int ret;
821
822         if (!stc || !mac)
823                 return;
824
825         if (!stc->carg) {
826                 stc->carg = MALLOC0(counter_arg_s, 1);
827                 if (stc->carg == NULL)
828                         return;
829
830                 stc->carg->sock = stc_monitor_get_counter_socket();
831         }
832
833         memset(&counter, 0, sizeof(struct nfacct_rule));
834
835         counter.carg = stc->carg;
836         counter.classid = classid;
837         counter.intend = intend;
838
839         if (connection->tether_state != TRUE ||
840                 connection->tether_iface.ifname == NULL)
841         return;
842
843         counter.iftype = connection->tether_iface.type;
844         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
845
846         /* get connected station ip based on its mac */
847         ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
848         if (ret != STC_ERROR_NONE)
849                 return;
850
851         /* tethering iptables rule */
852         __add_iptables_tether_in(&counter, ipaddr);
853         __add_iptables_tether_out(&counter, ipaddr);
854         g_free(ipaddr);
855 }
856
857 static void __del_tethering_iptables_rule(int64_t classid, gchar *mac,
858                 nfacct_rule_intend intend, stc_iface_type_e iftype)
859 {
860         default_connection_s *connection = stc_get_default_connection();
861         struct nfacct_rule counter;
862         stc_s *stc = stc_get_manager();
863         char *ipaddr = NULL;
864         int ret;
865
866         if (!stc || !mac)
867                 return;
868
869         if (!stc->carg) {
870                 stc->carg = MALLOC0(counter_arg_s, 1);
871                 if (stc->carg == NULL)
872                         return;
873
874                 stc->carg->sock = stc_monitor_get_counter_socket();
875         }
876
877         memset(&counter, 0, sizeof(struct nfacct_rule));
878
879         counter.carg = stc->carg;
880         counter.classid = classid;
881         counter.intend = intend;
882
883         if (connection->tether_state != TRUE ||
884                 connection->tether_iface.ifname == NULL)
885         return;
886
887         counter.iftype = connection->tether_iface.type;
888         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
889
890         /* get connected station ip based on its mac */
891         ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
892         if (ret != STC_ERROR_NONE) {
893                 STC_LOGE("Error: no IP found for station mac(%s)", mac);
894                 return;
895         }
896
897         /* tethering iptables rule */
898         __del_iptables_tether_in(&counter, ipaddr);
899         __del_iptables_tether_out(&counter, ipaddr);
900         g_free(ipaddr);
901 }
902
903 static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
904                                 stc_iface_type_e iftype)
905 {
906         char *default_ifname = stc_default_connection_get_ifname();
907         default_connection_s *connection = stc_get_default_connection();
908         struct nfacct_rule counter;
909         stc_s *stc = stc_get_manager();
910         if (!stc) {
911                 g_free(default_ifname); //LCOV_EXCL_LINE
912                 return; //LCOV_EXCL_LINE
913         }
914
915         if (!stc->carg) {
916                 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
917                 if (stc->carg == NULL) { //LCOV_EXCL_LINE
918                         g_free(default_ifname); //LCOV_EXCL_LINE
919                         return; //LCOV_EXCL_LINE
920                 }
921
922                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
923         }
924
925         memset(&counter, 0, sizeof(struct nfacct_rule));
926
927         counter.carg = stc->carg;
928         counter.classid = classid;
929         counter.intend = intend;
930
931         if (connection && connection->tether_iface.ifname != NULL &&
932                 classid == STC_TETHERING_APP_CLASSID) {
933                 counter.iftype = connection->tether_iface.type;
934                 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
935         } else {
936                 counter.iftype = iftype;
937                 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
938         }
939
940         g_free(default_ifname);
941
942         /* iptables rule */
943         __del_iptables_in(&counter);
944         __del_iptables_out(&counter);
945
946         /* ip6tables rule */
947         __del_ip6tables_in(&counter);
948         __del_ip6tables_out(&counter);
949 }
950
951 static void __set_rstn_noti_state(int value)
952 {
953         int state = STC_RSTN_STATE_INIT;
954
955         if (__vconf_get_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, &state))
956                 return;
957
958         if (state == value) {
959                 STC_LOGI("No need to change a restriction status: %d", state);
960                 return;
961         }
962
963         vconf_set_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, value);
964         return;
965 }
966
967 typedef struct {
968         time_t month_start_ts;
969         time_t week_start_ts;
970         time_t day_start_ts;
971         int64_t monthly_stat;
972         int64_t weekly_stat;
973         int64_t daily_stat;
974 } cumulative_data_s;
975
976 static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info,
977                                         void *user_data)
978 {
979         cumulative_data_s *stat = (cumulative_data_s *)user_data;
980         int64_t counters = 0;
981
982         counters = info->cnt.in_bytes + info->cnt.out_bytes;
983
984         stat->monthly_stat += counters;
985         if (stat->week_start_ts <= info->interval->from)
986                 stat->weekly_stat += counters;
987         if (stat->day_start_ts <= info->interval->from)
988                 stat->daily_stat += counters;
989
990         return STC_CONTINUE;
991 }
992
993 static void __process_tethering_restriction(enum traffic_restriction_type rstn_type,
994                 stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value, void *data)
995 {
996         default_connection_s *old_connection = (default_connection_s *)data;
997         default_connection_s *connection = NULL;
998         char *mac_str = NULL;
999
1000         if (old_connection != NULL)
1001                 connection = old_connection;
1002         else
1003                 connection = stc_get_default_connection();
1004
1005         /* in case tethering is not active */
1006         if (connection->tether_state == FALSE)
1007                 return;
1008
1009         /* rstn not applicable for this interface */
1010         if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
1011                         (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
1012                 return;
1013
1014         /* in case appid not a tethering app */
1015         if (!g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX))
1016                 return;
1017
1018         /* Ignore TOTAL_TETHERING,
1019          * Process only station appids */
1020         if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
1021                 return;
1022
1023         /* get the station mac based on classid */
1024         stc_plugin_tether_get_station_by_classid(rstn_value->classid, &mac_str);
1025         if (!mac_str) {
1026                 STC_LOGE("station not found for classid(%d)", rstn_value->classid);
1027                 return;
1028         }
1029
1030         switch (rstn_type) {
1031         case RST_SET:
1032         {
1033                 int i;
1034                 table_counters_info info;
1035                 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
1036
1037                         memset(&info, 0, sizeof(table_counters_info));
1038                         rstn_value->limit_exceeded = 0;
1039
1040                         if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1041                                                 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1042                                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1043                                          rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1044                                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1045                                          rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1046                                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1047                                          rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1048                                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1049                                          rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1050                                 table_counters_get(rstn_value->restriction_id, &info);
1051
1052                                 time_t current_time = 0;
1053                                 cumulative_data_s stat;
1054                                 table_statistics_select_rule rule;
1055
1056                                 memset(&stat, 0, sizeof(cumulative_data_s));
1057                                 stat.month_start_ts = rstn_value->month_start_ts;
1058                                 stat.week_start_ts = g_system->last_week_ts;
1059                                 stat.day_start_ts = g_system->last_day_ts;
1060
1061                                 memset(&rule, 0, sizeof(table_statistics_select_rule));
1062                                 rule.from = rstn_value->month_start_ts;
1063                                 time(&current_time);
1064                                 rule.to = current_time;
1065                                 rule.iftype = rstn_key->iftype;
1066                                 rule.granularity = GRANULARITY;
1067
1068                                 table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
1069
1070                                 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1071                                 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1072                                 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
1073                                 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
1074                                 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
1075                         }
1076
1077                         for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1078                                 if (rstn_value->limit[i] >= 0) {
1079                                         effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
1080
1081                                         if (effective_limit[i] < 0)
1082                                                 rstn_value->limit_exceeded |= (1 << i);
1083                                 }
1084                         }
1085
1086                         STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
1087                                         rstn_value->restriction_id, info.data_counter);
1088
1089                         if (rstn_value->limit_exceeded != 0 &&
1090                                         rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
1091                                 __add_tethering_iptables_rule(rstn_value->classid, mac_str,
1092                                                 NFACCT_TETH_BLOCK, rstn_key->iftype);
1093                         }
1094
1095                         rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1096         }
1097         break;
1098         case RST_EXCLUDE:
1099         {
1100                 __add_tethering_iptables_rule(rstn_value->classid, mac_str,
1101                                         NFACCT_TETH_ALLOW, rstn_key->iftype);
1102
1103                         rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1104                         rstn_value->limit_exceeded = 0;
1105                         rstn_value->limit_notified = 0;
1106         }
1107         break;
1108         case RST_UNSET:
1109         {
1110                         int i;
1111                         __del_tethering_iptables_rule(rstn_value->classid, mac_str,
1112                                         NFACCT_TETH_BLOCK, rstn_key->iftype);
1113
1114                         rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1115                         rstn_value->limit_exceeded = 0;
1116                         rstn_value->limit_notified = 0;
1117
1118                         for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
1119                                 if (rstn_value->limit[i] >= 0)
1120                                         rstn_value->counter[i] = 0;
1121         }
1122         break;
1123         default:
1124                         ;//Do Nothing
1125         }
1126         FREE(mac_str);
1127 }
1128
1129 static void __process_restriction(enum traffic_restriction_type rstn_type,
1130                                   stc_rstn_key_s *rstn_key,
1131                                   stc_rstn_value_s *rstn_value, void *data)
1132 {
1133         default_connection_s *old_connection = (default_connection_s *)data;
1134         default_connection_s *connection = NULL;
1135
1136         if (old_connection != NULL)
1137                 connection = old_connection;
1138         else
1139                 connection = stc_get_default_connection();
1140
1141         /* no default ifname */
1142         if (connection->ifname == NULL)
1143                 return;
1144
1145         /* rstn not applicable for this interface */
1146         if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
1147             (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
1148                 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
1149                 return;
1150
1151         /* classid is invalid */
1152         if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
1153                 return;
1154
1155         /* Do not proceed for tethering station appid if found here,
1156          * for tethering station apps __process_tethering_restriction() call
1157          * will handle it */
1158         if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) &&
1159                         rstn_value->classid != STC_TETHERING_APP_CLASSID)
1160                 return;
1161
1162         switch (rstn_type) {
1163         case RST_SET:
1164         {
1165                 int i;
1166                 table_counters_info info;
1167                 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
1168
1169                 memset(&info, 0, sizeof(table_counters_info));
1170                 rstn_value->limit_exceeded = 0;
1171
1172                 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1173                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1174                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1175                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1176                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1177                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1178                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1179                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1180                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1181                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1182                         table_counters_get(rstn_value->restriction_id, &info);
1183
1184                         time_t current_time = 0;
1185                         cumulative_data_s stat;
1186                         table_statistics_select_rule rule;
1187
1188                         memset(&stat, 0, sizeof(cumulative_data_s));
1189                         stat.month_start_ts = rstn_value->month_start_ts;
1190                         stat.week_start_ts = g_system->last_week_ts;
1191                         stat.day_start_ts = g_system->last_day_ts;
1192
1193                         memset(&rule, 0, sizeof(table_statistics_select_rule));
1194                         rule.from = rstn_value->month_start_ts;
1195                         time(&current_time);
1196                         rule.to = current_time;
1197                         rule.iftype = rstn_key->iftype;
1198                         rule.granularity = GRANULARITY;
1199
1200                         table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
1201
1202                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1203                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1204                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
1205                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
1206                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
1207                 }
1208
1209                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1210                         if (rstn_value->limit[i] >= 0) {
1211                                 effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
1212
1213                                 if (effective_limit[i] < 0)
1214                                         rstn_value->limit_exceeded |= (1 << i);
1215                         }
1216                 }
1217
1218                 STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
1219                         rstn_value->restriction_id, info.data_counter);
1220
1221                 if (rstn_value->limit_exceeded != 0 &&
1222                         rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
1223                         __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK, rstn_key->iftype);
1224                 }
1225
1226                 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1227         }
1228         break;
1229         case RST_EXCLUDE:
1230                 __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
1231                                     rstn_key->iftype);
1232
1233                 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1234                 rstn_value->limit_exceeded = 0;
1235                 rstn_value->limit_notified = 0;
1236                 break;
1237         case RST_UNSET:
1238         {
1239                 int i;
1240
1241                 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
1242                         __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
1243                                             rstn_key->iftype);
1244                 else
1245                         __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
1246                                             rstn_key->iftype);
1247
1248                 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1249                 rstn_value->limit_exceeded = 0;
1250                 rstn_value->limit_notified = 0;
1251
1252                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
1253                         if (rstn_value->limit[i] >= 0)
1254                                 rstn_value->counter[i] = 0;
1255
1256                 __set_rstn_noti_state(STC_RSTN_STATE_UNSET);
1257         }
1258         break;
1259         default:
1260                 ;//Do Nothing
1261         }
1262 }
1263
1264 //LCOV_EXCL_START
1265 static gboolean __remove_rstns_foreach_application(gpointer key,
1266                                                    gpointer value,
1267                                                    gpointer data)
1268 {
1269         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1270         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1271         gchar *app_id = (gchar *)data;
1272
1273         /* rstn rule is not for applications */
1274         if (rstn_key->app_id == NULL)
1275                 goto out;
1276
1277         /* rstn rule is not for this application */
1278         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1279                 goto out;
1280
1281         /* rstn rule is already removed */
1282         if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
1283                 goto out;
1284
1285         /* remove restriction from system */
1286         __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
1287
1288         /* remove tethering restriction from system*/
1289         __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
1290
1291         __print_rstn(rstn_key, rstn_value);
1292 out:
1293         return FALSE;
1294 }
1295 //LCOV_EXCL_STOP
1296
1297 static void __remove_rstns_for_application(gchar *app_id)
1298 {
1299         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
1300                        app_id);
1301 }
1302
1303 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
1304 {
1305         stc_error_e ret = STC_ERROR_NONE;
1306         guint pid_count = 0;
1307         stc_app_value_s *lookup;
1308
1309         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1310
1311         lookup = __application_lookup(g_system->apps, app_key);
1312         if (!lookup) {
1313                 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
1314                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1315         }
1316
1317         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
1318                        &pid_count);
1319
1320         if (!pid_count) {
1321                 /* remove nfacct rule for this classid */
1322                 __remove_application_monitor((gpointer) app_key, lookup,
1323                                              stc_get_default_connection());
1324                 __remove_rstns_for_application(app_key->app_id);
1325         }
1326
1327         if (!g_tree_remove(g_system->apps, app_key)) {
1328                 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1329                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1330         }
1331
1332         return ret;
1333 }
1334
1335 static stc_error_e __close_contr_sock(stc_system_s *system)
1336 {
1337         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
1338
1339         /* close netlink socket for updating kernel counters */
1340         if (system->contr_sock != -1) {
1341                 close(system->contr_sock);
1342                 system->contr_sock = -1;
1343         }
1344
1345         if (system->contr_gsource_id != 0) {
1346                 g_source_remove(system->contr_gsource_id);
1347                 system->contr_gsource_id = 0;
1348         }
1349
1350         return STC_ERROR_NONE;
1351 }
1352
1353 static gboolean __process_contr_reply(GIOChannel *source,
1354                                       GIOCondition condition,
1355                                       gpointer user_data);
1356
1357 //LCOV_EXCL_START
1358 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
1359 {
1360         GIOChannel *gio = NULL;
1361         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
1362
1363         /* close netlink socket for updating kernel counters */
1364         if (system->contr_sock != -1) {
1365                 close(system->contr_sock);
1366                 system->contr_sock = -1;
1367         }
1368
1369         if (system->contr_gsource_id != 0) {
1370                 g_source_remove(system->contr_gsource_id);
1371                 system->contr_gsource_id = 0;
1372         }
1373
1374         /* create netlink socket for updating kernel counters */
1375         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1376         if (system->contr_sock < 0) {
1377                 STC_LOGE("failed to open socket");
1378                 FREE(system);
1379                 return STC_ERROR_FAIL;
1380         }
1381
1382         gio = g_io_channel_unix_new(system->contr_sock);
1383         system->contr_gsource_id =
1384                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1385                                (GIOFunc) __process_contr_reply,
1386                                NULL);
1387         g_io_channel_unref(gio);
1388
1389         return STC_ERROR_NONE;
1390 }
1391
1392 static void __action_when_rstn_limit_exceeded_tethering(stc_rstn_key_s *rstn_key,
1393                 stc_rstn_value_s *rstn_value, classid_bytes_context_s *context)
1394 {
1395         char *mac_str = NULL;
1396         struct nfacct_rule *counter = context->counter;
1397
1398         /* get the station mac based on classid */
1399         stc_plugin_tether_get_station_by_classid(counter->classid, &mac_str);
1400         if (!mac_str) {
1401                 STC_LOGE("station not found for classid(%d)", counter->classid);
1402                 return;
1403         }
1404
1405         STC_LOGI("station mac %s, classid %u, iftype %u, iotype %d, \
1406                         intend %d, ifname %s, bytes %lld", mac_str,
1407                         counter->classid, counter->iftype, counter->iotype,
1408                         counter->intend, counter->ifname, context->bytes);
1409
1410         /* Block tethering station immediately */
1411         counter->intend = NFACCT_TETH_BLOCK;
1412         __del_tethering_iptables_rule(counter->classid, mac_str,
1413                         NFACCT_TETH_BLOCK, rstn_key->iftype);
1414
1415         __add_tethering_iptables_rule(counter->classid, mac_str,
1416                         NFACCT_TETH_BLOCK, rstn_key->iftype);
1417         counter->intend = NFACCT_TETH_COUNTER;
1418
1419         g_free(mac_str);
1420 }
1421
1422 static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type,
1423                                                 stc_rstn_key_s *rstn_key,
1424                                                 stc_rstn_value_s *rstn_value,
1425                                                 classid_bytes_context_s *context)
1426 {
1427         gboolean rv;
1428         char iftype[MAX_INT_LENGTH] = { 0, };
1429         char byte[MAX_INT_LENGTH] = { 0, };
1430         const char *signal_name = NULL;
1431         const char *net_popup_content = NULL;
1432         const char *net_popup_type = NULL;
1433         stc_s *stc = (stc_s *)stc_get_manager();
1434
1435         if (stc == NULL) {
1436                 STC_LOGE("Failed to get stc data");
1437                 return;
1438         }
1439
1440         switch (limit_type) {
1441         case STC_RSTN_LIMIT_TYPE_DATA_WARN:
1442         {
1443                 signal_name = "WarnThresholdCrossed";
1444                 net_popup_content = "warn threshold crossed";
1445                 net_popup_type = "warning_noti";
1446         }
1447         break;
1448         case STC_RSTN_LIMIT_TYPE_DATA:
1449         case STC_RSTN_LIMIT_TYPE_MONTHLY:
1450         case STC_RSTN_LIMIT_TYPE_WEEKLY:
1451         case STC_RSTN_LIMIT_TYPE_DAILY:
1452         {
1453                 signal_name = "RestrictionThresholdCrossed";
1454                 net_popup_content = "restriction threshold crossed";
1455                 net_popup_type = "restriction_noti";
1456
1457                 /* Apply restriction for tethering apps if app_id is of tethering client
1458                  * otherwise do the normal iptables rule */
1459                 if (context->counter->intend == NFACCT_TETH_COUNTER) {
1460
1461                         if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) &&
1462                                         rstn_value->classid != STC_TETHERING_APP_CLASSID) {
1463                                 __action_when_rstn_limit_exceeded_tethering(rstn_key, rstn_value,
1464                                                 context);
1465                         }
1466
1467                 } else {
1468                         /* block immediately */
1469                         context->counter->intend = NFACCT_BLOCK;
1470                         __del_iptables_in(context->counter);
1471                         __del_iptables_out(context->counter);
1472                         __add_iptables_in(context->counter);
1473                         __add_iptables_out(context->counter);
1474
1475                         __del_ip6tables_in(context->counter);
1476                         __del_ip6tables_out(context->counter);
1477                         __add_ip6tables_in(context->counter);
1478                         __add_ip6tables_out(context->counter);
1479                         context->counter->intend = NFACCT_COUNTER;
1480                 }
1481
1482                 rstn_value->limit_exceeded |= (1 << limit_type);
1483
1484                 __set_rstn_noti_state(STC_RSTN_STATE_SET);
1485         }
1486         break;
1487         default:
1488                 break;
1489         }
1490
1491         if (signal_name == NULL) {
1492                 STC_LOGE("Invalid parameter: limit_type");
1493                 return;
1494         }
1495
1496         /* emit signal */
1497         rv = stc_manager_dbus_emit_signal(stc->connection,
1498                                                   STC_DBUS_SERVICE_RESTRICTION_PATH,
1499                                                   STC_DBUS_INTERFACE_RESTRICTION,
1500                                                   signal_name,
1501                                                   g_variant_new("(si)",
1502                                                   rstn_key->app_id,
1503                                                   rstn_key->iftype));
1504
1505         if (rv == TRUE)
1506                 rstn_value->limit_notified |= (1 << limit_type);
1507
1508         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
1509         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
1510         stc_plugin_appstatus_send_message(net_popup_content,
1511                         net_popup_type, rstn_key->app_id, iftype, byte);
1512 }
1513
1514 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
1515                                       stc_rstn_value_s *rstn_value,
1516                                       classid_bytes_context_s *context)
1517 {
1518         int i;
1519         switch (context->counter->iotype) {
1520         case NFACCT_COUNTER_IN:
1521         case NFACCT_COUNTER_OUT:
1522                 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1523                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1524                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1525                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1526                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1527                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1528                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1529                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1530                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1531                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1532                         table_counters_info info;
1533                         memset(&info, 0, sizeof(table_counters_info));
1534                         table_counters_get(rstn_value->restriction_id, &info);
1535
1536                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1537                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1538                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
1539                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
1540                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
1541                 }
1542
1543                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1544                         if (rstn_value->limit[i] >= 0 &&
1545                                 !(rstn_value->limit_notified & (1 << i))) {
1546                                 rstn_value->counter[i] += context->bytes;
1547                                 if (rstn_value->limit[i] <= rstn_value->counter[i])
1548                                         __action_when_rstn_limit_exceeded(i,
1549                                                                                 rstn_key,
1550                                                                                 rstn_value,
1551                                                                                 context);
1552                         }
1553                 }
1554
1555                 g_system->rstns_tree_updated = TRUE;
1556                 __print_rstn(rstn_key, rstn_value);
1557                 break;
1558         default:
1559                 STC_LOGE("unknown iotype");
1560         }
1561
1562         return FALSE;
1563 }
1564
1565 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
1566                                                 stc_rstn_value_s *rstn_value,
1567                                                 classid_bytes_context_s *context)
1568 {
1569         if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1570                 context->counter->iftype == STC_IFACE_DATACALL) ||
1571                 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
1572                 context->counter->iftype == STC_IFACE_WIFI) ||
1573                 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1574                 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1575                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1576                  context->counter->iftype == STC_IFACE_WIFI) ||
1577                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1578                  context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1579                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1580                  context->counter->iftype == STC_IFACE_USB) ||
1581                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1582                  context->counter->iftype == STC_IFACE_P2P)) {
1583                 context->counter->classid = rstn_value->classid;
1584                 return __rstn_counter_update(rstn_key, rstn_value, context);
1585         }
1586
1587         return FALSE;
1588 }
1589
1590 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
1591                                                       gpointer value,
1592                                                       gpointer data)
1593 {
1594         gboolean rv = FALSE;
1595         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1596         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1597         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1598         uint32_t classid;
1599
1600         if (context->counter->intend != NFACCT_COUNTER &&
1601                         context->counter->intend != NFACCT_TETH_COUNTER)
1602                 goto try_next_callback;
1603
1604         if (rstn_value->limit_exceeded == TRUE) {
1605                 context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE
1606                 goto try_next_callback; //LCOV_EXCL_LINE
1607         }
1608
1609         classid = context->counter->classid;
1610         rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
1611
1612         context->counter->classid = classid;
1613         if (rstn_value->classid != context->counter->classid)
1614                 goto try_next_callback;
1615
1616         rv = __rstn_counter_update(rstn_key, rstn_value, context);
1617
1618 try_next_callback:
1619         return rv;
1620 }
1621 //LCOV_EXCL_STOP
1622
1623 static gboolean __update_app_statistics(gpointer key, gpointer value,
1624                                         gpointer data)
1625 {
1626         stc_app_key_s *app_key = (stc_app_key_s *)key;
1627         stc_app_value_s *app_value = (stc_app_value_s *)value;
1628         time_t *touch_time = (time_t *)data;
1629         stc_db_classid_iftype_key stat_key;
1630         stc_db_app_stats stat;
1631         default_connection_s *default_connection = stc_get_default_connection();
1632
1633         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
1634         memset(&stat, 0 , sizeof(stc_db_app_stats));
1635
1636         /* Do not update statistics for Tethering
1637          * if tethering is in-active found */
1638         if (default_connection &&
1639                 default_connection->tether_state == FALSE &&
1640                 !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
1641                 return FALSE;
1642
1643         /* Do not update statistics for Wi-Fi
1644          * if tethering is active on wlan0 iface */
1645         if (default_connection && default_connection->tether_state &&
1646                 default_connection->tether_iface.type == STC_IFACE_WIFI &&
1647                 !strcmp(app_key->app_id, STC_TOTAL_WIFI))
1648                 return FALSE;
1649
1650         stat_key.classid = app_value->classid;
1651
1652         if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1653                 default_connection->tether_state == TRUE)
1654                 stat_key.iftype = default_connection->tether_iface.type;
1655         else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
1656                 stat_key.iftype = default_connection->tether_iface.type;
1657         else
1658                 stat_key.iftype = default_connection->type;
1659
1660         if (STC_IFACE_DATACALL == stat_key.iftype)
1661                 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
1662         else
1663                 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
1664
1665         if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1666                 default_connection->tether_state == TRUE)
1667                 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1668                           MAX_IFACE_LENGTH);
1669         else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
1670                 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1671                           MAX_IFACE_LENGTH);
1672         else
1673                 g_strlcpy(stat_key.ifname, default_connection->ifname,
1674                           MAX_IFACE_LENGTH);
1675
1676         stat.app_id = g_strdup(app_key->app_id);
1677         stat.snd_count = app_value->counter.out_bytes;
1678         stat.rcv_count = app_value->counter.in_bytes;
1679         stat.is_roaming = default_connection->roaming;
1680
1681         if (strstr(stat.app_id, "_BACKGROUND")) {
1682                 stat.ground = STC_APP_STATE_BACKGROUND;
1683         } else {
1684                 if (strstr(stat.app_id, "TOTAL_"))
1685                         stat.ground = STC_APP_STATE_UNKNOWN;
1686                 else
1687                         stat.ground = STC_APP_STATE_FOREGROUND;
1688         }
1689
1690         table_statistics_insert(&stat_key, &stat, *touch_time);
1691
1692         app_value->counter.out_bytes = 0;
1693         app_value->counter.in_bytes = 0;
1694
1695         FREE(stat.app_id);
1696         FREE(stat_key.subscriber_id);
1697
1698         return FALSE;
1699 }
1700
1701 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1702 {
1703         time_t current_time = 0;
1704         stc_s *stc = stc_get_manager();
1705
1706         if (stc && stc->carg)
1707                 current_time = stc->carg->last_run_time;
1708
1709         if (g_system->apps_tree_updated == FALSE)
1710                 return G_SOURCE_REMOVE;
1711
1712         g_system->apps_tree_updated = FALSE;
1713
1714         if (g_system->apps)
1715                 g_tree_foreach(g_system->apps,
1716                                __update_app_statistics,
1717                                &current_time);
1718
1719         STC_LOGI("Flushed app stats to database");
1720         return G_SOURCE_REMOVE;
1721 }
1722
1723 //LCOV_EXCL_START
1724 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1725                                             gpointer data)
1726 {
1727         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1728         table_counters_info info = {
1729                 .restriction_id = rstn_value->restriction_id,
1730                 .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
1731                 .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
1732                 .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
1733                 .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
1734                 .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY]
1735         };
1736
1737         table_counters_update_counters(&info);
1738
1739         return FALSE;
1740 }
1741
1742 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1743 {
1744         time_t current_time = 0;
1745         stc_s *stc = stc_get_manager();
1746
1747         if (stc && stc->carg)
1748                 current_time = stc->carg->last_run_time;
1749
1750         if (g_system->rstns_tree_updated == FALSE)
1751                 return G_SOURCE_REMOVE;
1752
1753         g_system->rstns_tree_updated = FALSE;
1754
1755         if (g_system->rstns)
1756                 g_tree_foreach(g_system->rstns,
1757                                __update_counter_statistics,
1758                                &current_time);
1759
1760         STC_LOGI("Flushed rstns counters to database");
1761         return G_SOURCE_REMOVE;
1762 }
1763 //LCOV_EXCL_STOP
1764
1765 static void __app_counter_update(stc_app_key_s *app_key,
1766                                  stc_app_value_s *app_value,
1767                                  classid_bytes_context_s *context)
1768 {
1769         switch (context->counter->iotype) {
1770         case NFACCT_COUNTER_IN:
1771                 app_value->data_usage.in_bytes += context->bytes;
1772                 app_value->counter.in_bytes = context->bytes;
1773                 g_system->apps_tree_updated = TRUE;
1774
1775                 /*
1776                 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1777                 */
1778                 break;
1779         case NFACCT_COUNTER_OUT:
1780                 app_value->data_usage.out_bytes += context->bytes;
1781                 app_value->counter.out_bytes = context->bytes;
1782                 g_system->apps_tree_updated = TRUE;
1783
1784                 /*
1785                 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1786                 */
1787                 break;
1788         default:
1789                 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1790         }
1791 }
1792
1793 static void __interface_counter_update(stc_app_key_s *app_key,
1794                                        stc_app_value_s *app_value,
1795                                        classid_bytes_context_s *context)
1796 {
1797         if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1798              context->counter->iftype == STC_IFACE_DATACALL) ||
1799             (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1800              context->counter->iftype == STC_IFACE_WIFI) ||
1801             (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1802              context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1803                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1804                  context->counter->iftype == STC_IFACE_WIFI) ||
1805                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1806                  context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1807                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1808                  context->counter->iftype == STC_IFACE_USB) ||
1809                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1810                  context->counter->iftype == STC_IFACE_P2P))
1811                  __app_counter_update(app_key, app_value, context);
1812 }
1813
1814
1815 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1816                                                       gpointer value,
1817                                                       gpointer data)
1818 {
1819         stc_app_key_s *app_key = (stc_app_key_s *)key;
1820         stc_app_value_s *app_value = (stc_app_value_s *)value;
1821         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1822
1823         if (context->counter->intend != NFACCT_COUNTER &&
1824                         context->counter->intend != NFACCT_TETH_COUNTER)
1825                 goto try_next_callback;
1826
1827         __interface_counter_update(app_key, app_value, context);
1828
1829         if (app_value->classid != context->counter->classid)
1830                 goto try_next_callback;
1831
1832         __app_counter_update(app_key, app_value, context);
1833
1834 try_next_callback:
1835         return FALSE;
1836 }
1837
1838 static gboolean __reset_time_counter_foreach_rstn(gpointer key,
1839                                                   gpointer value,
1840                                                   gpointer data)
1841 {
1842         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1843         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1844         reset_time_limits_context_s *context = (reset_time_limits_context_s *)data;
1845         int i;
1846         time_t now_month_start_ts;
1847
1848         if (rstn_value->month_start_date == 0) {
1849                 table_counters_info info;
1850                 memset(&info, 0, sizeof(table_counters_info));
1851                 table_counters_get_timestamps(rstn_value->restriction_id, &info);
1852
1853                 if (info.month_start_date == 0)
1854                         rstn_value->month_start_date = 1;
1855                 else
1856                         rstn_value->month_start_date = info.month_start_date;
1857                 rstn_value->month_start_ts = info.month_start_ts;
1858         }
1859
1860         now_month_start_ts =
1861                 stc_time_get_month_start(context->now,
1862                                          rstn_value->month_start_date);
1863
1864         if (rstn_value->month_start_ts != now_month_start_ts) {
1865                 rstn_value->month_start_ts = now_month_start_ts;
1866                 context->month_start_ts = now_month_start_ts;
1867                 context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
1868         }
1869
1870         if (context->is_updated) {
1871                 table_counters_info info;
1872                 memset(&info, 0, sizeof(table_counters_info));
1873
1874                 info.restriction_id = rstn_value->restriction_id;
1875                 info.month_start_date = rstn_value->month_start_date;
1876                 info.month_start_ts = rstn_value->month_start_ts;
1877                 info.week_start_ts = context->week_start_ts;
1878                 info.day_start_ts = context->day_start_ts;
1879
1880                 table_counters_update_timestamps(&info);
1881         }
1882
1883         for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1884
1885                 if ((context->is_updated) & (1 << i)) {
1886                         /* reset limit */
1887                         rstn_value->counter[i] = 0;
1888
1889                         if (rstn_value->limit_exceeded & (1 << i)) {
1890                                 /* remove iptables rule */
1891                                 char *default_ifname = stc_default_connection_get_ifname();
1892                                 struct nfacct_rule counter;
1893                                 stc_s *stc = stc_get_manager();
1894                                 if (stc == NULL) {
1895                                         STC_LOGE("Can't get stc data");
1896                                         g_free(default_ifname);
1897                                         goto try_next_callback;
1898                                 }
1899
1900                                 if (!stc->carg) {
1901                                         stc->carg = MALLOC0(counter_arg_s, 1);
1902                                         if (stc->carg == NULL) {
1903                                                 g_free(default_ifname);
1904                                                 goto try_next_callback;
1905                                         }
1906
1907                                         stc->carg->sock =
1908                                                 stc_monitor_get_counter_socket();
1909                                 }
1910
1911                                 memset(&counter, 0, sizeof(struct nfacct_rule));
1912
1913                                 counter.carg = stc->carg;
1914                                 counter.classid = rstn_value->classid;
1915                                 counter.intend = NFACCT_BLOCK;
1916                                 counter.iftype = rstn_key->iftype;
1917                                 g_strlcpy(counter.ifname, default_ifname,
1918                                           MAX_IFACE_LENGTH);
1919
1920                                 g_free(default_ifname);
1921
1922                                 /* iptables rule */
1923                                 __del_iptables_in(&counter);
1924                                 __del_iptables_out(&counter);
1925
1926                                 /* ip6tables rule */
1927                                 __del_ip6tables_in(&counter);
1928                                 __del_ip6tables_out(&counter);
1929
1930                                 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1931                                 rstn_value->limit_exceeded &= ~(1 << i);
1932                                 rstn_value->limit_notified &= ~(1 << i);
1933                         }
1934                 }
1935         }
1936
1937 try_next_callback:
1938         return FALSE;
1939 }
1940
1941 static void __reset_time_counters_if_required(void)
1942 {
1943         reset_time_limits_context_s context;
1944
1945         if (g_system == NULL) {
1946                 STC_LOGE("stc monitor not initialized!");
1947                 return;
1948         }
1949
1950         context.now = time(NULL);
1951         context.week_start_ts = stc_time_get_week_start(context.now);
1952         context.day_start_ts = stc_time_get_day_start(context.now);
1953         context.is_updated = 0;
1954
1955         if (g_system->last_week_ts != context.week_start_ts) {
1956                 g_system->last_week_ts = context.week_start_ts;
1957                 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
1958         }
1959
1960         if (g_system->last_day_ts != context.day_start_ts) {
1961                 g_system->last_day_ts = context.day_start_ts;
1962                 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
1963         }
1964
1965         if (g_system->rstns) {
1966                 g_tree_foreach(g_system->rstns,
1967                                __reset_time_counter_foreach_rstn,
1968                                &context);
1969                 if (context.is_updated)
1970                         STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]",
1971                                  context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts);
1972         }
1973 }
1974
1975 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1976                                  struct counter_arg *carg)
1977 {
1978         __reset_time_counters_if_required();
1979
1980         struct nfacct_rule counter = {
1981                 .carg = carg,
1982                 .name = {0},
1983                 .ifname = {0},
1984                 0
1985         };
1986
1987         classid_bytes_context_s context = {
1988                 .counter = &counter,
1989                 .bytes = bytes,
1990                 .data_limit_exceeded = FALSE,
1991         };
1992
1993         if (!recreate_counter_by_name(cnt_name, &counter)) {
1994                 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1995                 return; //LCOV_EXCL_LINE
1996         }
1997
1998         if (STC_DEBUG_LOG)
1999                 STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
2000                         context.counter->classid, context.counter->iftype,
2001                         context.counter->iotype, context.counter->intend,
2002                         context.counter->ifname, context.bytes);
2003
2004         if (g_system->rstns)
2005                 g_tree_foreach(g_system->rstns,
2006                                __rstn_counter_update_foreach_classid,
2007                                &context);
2008
2009         if (g_system->apps)
2010                 g_tree_foreach(g_system->apps,
2011                                __apps_counter_update_foreach_classid,
2012                                &context);
2013 }
2014
2015 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
2016                            void *user_data)
2017 {
2018         struct counter_arg *carg = user_data;
2019         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
2020         if (carg->initiate) {
2021                 /**
2022                  * TODO: this will be used when daemon starts to update existing
2023                  * counter data if present.
2024                  *
2025                  populate_counters(cnt_name, carg);
2026                  */
2027         } else {
2028                 int64_t *bytes_p =
2029                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
2030                 int bytes = be64toh(*bytes_p);
2031                 if (bytes) {
2032                         ++carg->serialized_counters;
2033                         __fill_nfacct_result(cnt_name, bytes, carg);
2034                 }
2035         }
2036
2037         return 0;
2038 }
2039
2040 static int __post_fill_counters(void *user_data)
2041 {
2042         struct counter_arg *carg = user_data;
2043
2044         if (carg->initiate)
2045                 carg->initiate = 0;
2046
2047         return 0;
2048 }
2049
2050 static void __process_network_counter(struct genl *ans,
2051                                       struct counter_arg *carg)
2052 {
2053         struct netlink_serialization_params ser_params = {
2054                 .carg = carg,
2055                 .ans = ans,
2056                 .eval_attr = __fill_counters,
2057                 .post_eval_attr = __post_fill_counters,
2058         };
2059
2060         netlink_serialization_command *netlink =
2061                 netlink_create_command(&ser_params);
2062         if (!netlink) {
2063                 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
2064                 return; //LCOV_EXCL_LINE
2065         }
2066
2067         netlink->deserialize_answer(&(netlink->params));
2068 }
2069
2070 static gboolean __process_contr_reply(GIOChannel *source,
2071                                       GIOCondition condition,
2072                                       gpointer user_data)
2073 {
2074         int sock = g_io_channel_unix_get_fd(source);
2075         struct genl *ans;
2076         int ret;
2077         stc_s *stc = stc_get_manager();
2078
2079 #ifdef TIZEN_GTESTS
2080         void __gcov_flush(void);
2081         __gcov_flush();
2082 #endif
2083
2084         if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
2085             (condition & G_IO_NVAL)) {
2086                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
2087
2088                 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
2089                          "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
2090                          (condition & G_IO_ERR), (condition & G_IO_HUP),
2091                          (condition & G_IO_NVAL));
2092                 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
2093                 return FALSE; //LCOV_EXCL_LINE
2094         }
2095
2096         ans = MALLOC0(struct genl, 1);
2097         if (ans == NULL) {
2098                 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
2099                 return TRUE; //LCOV_EXCL_LINE
2100         }
2101
2102         if (stc == NULL) {
2103                 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
2104                 goto out; //LCOV_EXCL_LINE
2105         }
2106
2107         ret = read_netlink(sock, ans, sizeof(struct genl));
2108         /* STC_LOGD("Counter data received ret [%d]", ret); */
2109         if (ret == 0)
2110                 goto out;
2111
2112         stc->carg->ans_len = ret;
2113         stc->carg->last_run_time = time(NULL);
2114
2115         __process_network_counter(ans, stc->carg);
2116
2117         g_idle_add(__flush_apps_stats_to_database, NULL);
2118         g_idle_add(__flush_rstns_counter_to_database, NULL);
2119 out:
2120
2121         FREE(ans);
2122         return TRUE;
2123 }
2124
2125 static gboolean __update_contr_cb(void *user_data)
2126 {
2127         /* Here we just sent command, answer we receive in another callback */
2128         stc_s *stc = stc_get_manager();
2129         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
2130         if (!stc->carg) {
2131                 stc->carg = MALLOC0(counter_arg_s, 1);
2132                 if (stc->carg == NULL)
2133                         return TRUE;  /* we need to continue the timer */
2134
2135                 stc->carg->sock = stc_monitor_get_counter_socket();
2136         }
2137
2138 #ifdef TIZEN_GTESTS
2139         void __gcov_flush(void);
2140         __gcov_flush();
2141 #endif
2142
2143         /* STC_LOGD("Get all counters"); */
2144         nfacct_send_get_all(stc->carg);
2145
2146         /* we need to continue the timer */
2147         return TRUE;
2148 }
2149
2150 /*
2151 //LCOV_EXCL_START
2152 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
2153                                           gpointer data)
2154 {
2155         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2156         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2157
2158         __print_rstn(rstn_key, rstn_value);
2159         return FALSE;
2160 }
2161
2162 static void __rstn_tree_printall(void)
2163 {
2164         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
2165 }
2166 //LCOV_EXCL_STOP
2167 */
2168
2169 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
2170                                         const stc_rstn_key_s *key)
2171 {
2172         stc_rstn_value_s *lookup;
2173
2174         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
2175
2176         lookup = g_tree_lookup(rstns_tree, key);
2177
2178         return lookup;
2179 }
2180
2181 static gboolean __remove_restriction(gpointer key, gpointer value,
2182                                      gpointer data)
2183 {
2184         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2185         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2186
2187         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
2188         __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, data);
2189         __print_rstn(rstn_key, rstn_value);
2190         return FALSE;
2191 }
2192
2193 static gboolean __add_restriction_debug(gpointer key, gpointer value,
2194                                         gpointer data)
2195 {
2196         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2197         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2198
2199         /* rstn rule is activated */
2200         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2201                 return FALSE;
2202
2203         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2204                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2205                 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2206         } else {
2207                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
2208                 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data);
2209         }
2210
2211         __print_rstn(rstn_key, rstn_value);
2212
2213         return FALSE;
2214 }
2215
2216 //LCOV_EXCL_START
2217 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
2218 {
2219         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2220         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2221
2222         /* rstn rule is activated */
2223         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2224                 return FALSE;
2225
2226         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2227                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2228                 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2229         } else {
2230                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
2231                 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data);
2232         }
2233
2234         return FALSE;
2235 }
2236 //LCOV_EXCL_STOP
2237
2238 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
2239 {
2240         stc_rstn_value_s *lookup_value;
2241
2242         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2243
2244         lookup_value = __rstn_lookup(g_system->rstns, key);
2245         if (!lookup_value) {
2246                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
2247                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
2248         }
2249
2250         /* remove counter also */
2251         table_counters_delete(lookup_value->restriction_id);
2252         __remove_restriction(key, lookup_value, NULL);
2253
2254         if (!g_tree_remove(g_system->rstns, key)) {
2255                 STC_LOGD("key not found"); //LCOV_EXCL_LINE
2256                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
2257         }
2258
2259         return STC_ERROR_NONE;
2260 }
2261
2262 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
2263                                    stc_rstn_value_s *value, gboolean debug)
2264 {
2265         stc_rstn_key_s *rstn_key;
2266         stc_rstn_value_s *rstn_value;
2267
2268         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2269
2270         rstn_value = __rstn_lookup(g_system->rstns, key);
2271         if (rstn_value)
2272                 __rstn_tree_remove(key);
2273
2274         rstn_key = MALLOC0(stc_rstn_key_s, 1);
2275         if (!rstn_key) {
2276                 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
2277                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2278         }
2279
2280         rstn_value = MALLOC0(stc_rstn_value_s, 1);
2281         if (!rstn_value) {
2282                 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
2283                 FREE(rstn_key); //LCOV_EXCL_LINE
2284                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2285         }
2286
2287         rstn_key->app_id = g_strdup(key->app_id);
2288         rstn_key->ifname = g_strdup(key->ifname);
2289         rstn_key->mac = g_strdup(key->mac);
2290         rstn_key->subscriber_id = g_strdup(key->subscriber_id);
2291         rstn_key->iftype = key->iftype;
2292         rstn_key->roaming = key->roaming;
2293
2294         g_tree_insert(g_system->rstns, rstn_key, rstn_value);
2295
2296         rstn_value->restriction_id = value->restriction_id;
2297         rstn_value->rstn_state = value->rstn_state;
2298         rstn_value->rstn_type = value->rstn_type;
2299         rstn_value->classid = value->classid;
2300
2301         int i;
2302         for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
2303                 rstn_value->limit[i] = value->limit[i];
2304                 rstn_value->counter[i] = 0;
2305         }
2306
2307         rstn_value->limit_exceeded = 0;
2308         rstn_value->limit_notified = 0;
2309         rstn_value->month_start_date = value->month_start_date;
2310         rstn_value->month_start_ts = value->month_start_ts;
2311
2312         if (debug == TRUE)
2313                 __add_restriction_debug(key, rstn_value, NULL);
2314         else
2315                 __add_restriction(key, rstn_value, NULL);
2316
2317         return STC_ERROR_NONE;
2318 }
2319
2320 //LCOV_EXCL_START
2321 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
2322                                             void *user_data)
2323 {
2324         stc_cb_ret_e ret = STC_CONTINUE;
2325
2326         stc_rstn_key_s key;
2327         stc_rstn_value_s value;
2328
2329         memset(&key, 0, sizeof(stc_rstn_key_s));
2330         memset(&value, 0, sizeof(stc_rstn_value_s));
2331
2332         key.app_id = g_strdup(info->app_id);
2333         key.ifname = g_strdup(info->ifname);
2334         key.subscriber_id = g_strdup(info->subscriber_id);
2335         key.iftype = info->iftype;
2336         key.roaming = info->roaming;
2337
2338         value.rstn_type = info->rstn_type;
2339         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2340         value.restriction_id = info->restriction_id;
2341
2342         if (info->app_id)
2343                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2344         else
2345                 value.classid = STC_UNKNOWN_CLASSID;
2346
2347         value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2348         value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2349         value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2350         value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2351         value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2352
2353         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
2354                 ret = STC_CANCEL;
2355
2356         FREE(key.app_id);
2357         FREE(key.ifname);
2358         FREE(key.subscriber_id);
2359         return ret;
2360 }
2361
2362 static void __fill_restritions_list(void)
2363 {
2364         table_restrictions_foreach(__insert_restriction_cb, NULL);
2365
2366         /* __rstn_tree_printall(); */
2367 }
2368
2369 static gboolean __add_rstn_foreach_application(gpointer key,
2370                                                gpointer value,
2371                                                gpointer data)
2372 {
2373         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2374         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2375         gchar *app_id = (gchar *)data;
2376
2377         /* rstn rule is not for applications */
2378         if (rstn_key->app_id == NULL)
2379                 goto out;
2380
2381         /* rstn rule is not for this application */
2382         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
2383                 goto out;
2384
2385         /* rstn rule is already applied */
2386         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2387                 goto out;
2388
2389         /* add restriction to system */
2390         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2391                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
2392                 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
2393         } else {
2394                 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
2395                 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, NULL);
2396         }
2397
2398         __print_rstn(rstn_key, rstn_value);
2399 out:
2400         return FALSE;
2401 }
2402 //LCOV_EXCL_STOP
2403
2404 static void __add_rstns_for_application(gchar *app_id)
2405 {
2406         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
2407                        app_id);
2408 }
2409
2410 static void __add_application_by_interface(const char *app_id)
2411 {
2412         stc_app_key_s app_key;
2413         stc_app_value_s app_value;
2414
2415         if (app_id == NULL)
2416                 return; //LCOV_EXCL_LINE
2417
2418         memset(&app_key, 0, sizeof(stc_app_key_s));
2419         memset(&app_value, 0, sizeof(stc_app_value_s));
2420
2421         app_key.pkg_id = g_strdup(app_id);
2422         app_key.app_id = g_strdup(app_id);
2423
2424         app_value.type = STC_APP_TYPE_NONE;
2425         app_value.processes = NULL;
2426         app_value.counter.in_bytes = 0;
2427         app_value.counter.out_bytes = 0;
2428
2429         stc_monitor_application_add(app_key, app_value);
2430
2431         FREE(app_key.pkg_id);
2432         FREE(app_key.app_id);
2433 }
2434
2435 static gboolean __processes_tree_foreach_background(gpointer key,
2436                                                     gpointer value,
2437                                                     gpointer data)
2438 {
2439         stc_process_key_s *proc_key = (stc_process_key_s *)key;
2440         stc_app_key_s *app_key = (stc_app_key_s *)data;
2441
2442         place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
2443
2444         return FALSE;
2445 }
2446
2447 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
2448                                         gpointer data)
2449 {
2450         stc_app_key_s *app_key = (stc_app_key_s *)key;
2451         stc_app_value_s *app_value = (stc_app_value_s *)value;
2452
2453         if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
2454                 g_tree_foreach(app_value->processes,
2455                                __processes_tree_foreach_background, app_key);
2456
2457         return FALSE;
2458 }
2459
2460 static stc_error_e __process_update_background(void)
2461 {
2462         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2463
2464         g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
2465
2466         return STC_ERROR_NONE;
2467 }
2468 //LCOV_EXCL_STOP
2469
2470 static void __fill_exceptions_list(void)
2471 {
2472         stc_plugin_fill_exception_list();
2473 }
2474
2475 stc_error_e stc_monitor_init(void)
2476 {
2477         stc_system_s *system = MALLOC0(stc_system_s, 1);
2478         GIOChannel *gio = NULL;
2479
2480         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
2481
2482         /* initializing current classid */
2483         init_current_classid();
2484
2485         /* initializing cgroups */
2486         cgroup_init();
2487
2488         /* creating monitored application tree */
2489         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
2490                                        __apps_tree_key_free,
2491                                        __apps_tree_value_free);
2492
2493         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
2494                                         __rstns_tree_key_free,
2495                                         __rstns_tree_value_free);
2496
2497         /* create netlink socket for updating kernel counters */
2498         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
2499         if (system->contr_sock < 0) {
2500                 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
2501                 FREE(system); //LCOV_EXCL_LINE
2502                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2503         }
2504
2505         gio = g_io_channel_unix_new(system->contr_sock);
2506         system->contr_gsource_id =
2507                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
2508                                (GIOFunc) __process_contr_reply,
2509                                NULL);
2510         g_io_channel_unref(gio);
2511
2512         g_system = system;
2513
2514         __add_application_by_interface(STC_TOTAL_DATACALL);
2515         __add_application_by_interface(STC_TOTAL_WIFI);
2516         __add_application_by_interface(STC_TOTAL_BLUETOOTH);
2517         __add_application_by_interface(STC_TOTAL_IPV4);
2518         __add_application_by_interface(STC_TOTAL_IPV6);
2519         __add_application_by_interface(STC_TOTAL_TETHERING);
2520
2521         /* creating restriction rules tree */
2522         __update_contr_cb(NULL);
2523
2524         /* registering periodic kernel counters update callback */
2525         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
2526                                                          __update_contr_cb,
2527                                                          NULL);
2528         if (g_system->contr_timer_id == 0) {
2529                 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
2530                 __close_contr_sock(g_system); //LCOV_EXCL_LINE
2531                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2532         }
2533
2534         __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
2535                         (int *)&g_system->background_state);
2536
2537         __fill_exceptions_list();
2538         __fill_restritions_list();
2539
2540         return STC_ERROR_NONE;
2541 }
2542
2543 stc_error_e stc_monitor_deinit(void)
2544 {
2545         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2546
2547         /* close netlink socket for updating kernel counters */
2548         __close_contr_sock(g_system);
2549
2550         /* remove kernel counters update timer */
2551         if (g_system->contr_timer_id > 0) {
2552                 g_source_remove(g_system->contr_timer_id);
2553                 g_system->contr_timer_id = 0;
2554         }
2555
2556         /* destroy monitored application tree */
2557         g_tree_destroy(g_system->apps);
2558         g_system->apps = NULL;
2559
2560         /* destroy restriction rules tree */
2561         g_tree_destroy(g_system->rstns);
2562         g_system->rstns = NULL;
2563
2564         FREE(g_system);
2565
2566         return STC_ERROR_NONE;
2567 }
2568
2569 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
2570                                         const stc_app_value_s app_value)
2571 {
2572         stc_error_e ret = STC_ERROR_NONE;
2573         stc_app_key_s *key;
2574         stc_app_value_s *value;
2575         stc_app_value_s *lookup;
2576
2577         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2578
2579         lookup = __application_lookup(g_system->apps, &app_key);
2580         if (lookup)
2581                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2582
2583         key = MALLOC0(stc_app_key_s, 1);
2584         if (!key) {
2585                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2586                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2587         }
2588
2589         value = MALLOC0(stc_app_value_s, 1);
2590         if (!value) {
2591                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2592                 FREE(key); //LCOV_EXCL_LINE
2593                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2594         }
2595
2596         key->app_id = g_strdup(app_key.app_id);
2597         key->pkg_id = g_strdup(app_key.pkg_id);
2598
2599         value->type = app_value.type;
2600         value->data_usage.in_bytes = app_value.data_usage.in_bytes;
2601         value->data_usage.out_bytes = app_value.data_usage.out_bytes;
2602         g_strlcpy(value->mac, app_value.mac, MAC_ADDRESS_LEN);
2603
2604         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
2605                                            __processes_tree_key_free,
2606                                            __processes_tree_value_free);
2607
2608         /* create cgroup and update classid */
2609         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
2610
2611         /* update classid for tethering station based on its mac address */
2612         if (g_str_has_suffix(app_key.app_id, STC_TETHERING_APP_SUFFIX) &&
2613                                 value->classid != STC_TETHERING_APP_CLASSID)
2614                 stc_plugin_tether_set_station_classid(value->mac, value->classid);
2615
2616         g_tree_insert(g_system->apps, key, value);
2617
2618         /* add nfacct rule for this classid */
2619         __add_application_monitor(key, value, stc_get_default_connection());
2620         __add_rstns_for_application(app_key.app_id);
2621
2622         return ret;
2623 }
2624
2625 API stc_error_e stc_monitor_application_remove(const stc_app_key_s app_key)
2626 {
2627         stc_error_e ret = STC_ERROR_NONE;
2628         stc_app_value_s *app_lookup;
2629
2630         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2631
2632         app_lookup = __application_lookup(g_system->apps, &app_key);
2633         if (!app_lookup) {
2634                 if (STC_DEBUG_LOG)
2635                         STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2636                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2637         }
2638
2639         /* remove nfacct rule for this classid */
2640         __remove_application_monitor((gpointer) &app_key, app_lookup,
2641                                      stc_get_default_connection());
2642
2643         /* remove ristrictions if any */
2644         __remove_rstns_for_application(app_key.app_id);
2645
2646         /* remove app_key from the stc-manager */
2647         if (!g_tree_remove(g_system->apps, &app_key)) {
2648                 ret = STC_ERROR_NO_DATA;
2649                 STC_LOGE("key not found");
2650         }
2651
2652         return ret;
2653 }
2654
2655 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
2656                                     const stc_process_key_s proc_key,
2657                                     const stc_process_value_s proc_value)
2658 {
2659         stc_error_e ret = STC_ERROR_NONE;
2660         stc_app_value_s *app_lookup;
2661         stc_process_key_s *key;
2662         stc_process_value_s *value;
2663         stc_process_value_s *proc_lookup;
2664
2665         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2666
2667         app_lookup = __application_lookup(g_system->apps, &app_key);
2668         if (!app_lookup) {
2669                 if (STC_DEBUG_LOG)
2670                         STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2671                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2672         }
2673
2674         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2675         if (proc_lookup)
2676                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2677
2678         key = MALLOC0(stc_process_key_s, 1);
2679         if (!key) {
2680                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2681                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2682         }
2683
2684         value = MALLOC0(stc_process_value_s, 1);
2685         if (!value) {
2686                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2687                 FREE(key); //LCOV_EXCL_LINE
2688                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2689         }
2690
2691         key->pid = proc_key.pid;
2692
2693         value->ground = proc_value.ground;
2694
2695         g_tree_insert(app_lookup->processes, key, value);
2696
2697         /* add pid to application cgroup */
2698         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2699
2700         /*
2701         __apps_tree_printall(); //LCOV_EXCL_LINE
2702         */
2703
2704         return ret;
2705 }
2706
2707 API stc_error_e stc_monitor_process_remove(pid_t pid)
2708 {
2709         stc_error_e ret = STC_ERROR_NONE;
2710         stc_process_key_s proc_key = {
2711                 .pid = pid
2712         };
2713
2714         remove_pid_context_s context = {
2715                 .app_key = NULL,
2716                 .proc_key = &proc_key,
2717                 .entry_removed = FALSE,
2718         };
2719
2720         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2721
2722         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
2723                        &context);
2724
2725         if (context.entry_removed)
2726                 __application_remove_if_empty(context.app_key);
2727
2728         /*
2729         __apps_tree_printall(); //LCOV_EXCL_LINE
2730         */
2731
2732         return ret;
2733 }
2734
2735 //LCOV_EXCL_START
2736 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
2737                                               const stc_process_key_s proc_key,
2738                                               stc_app_state_e ground)
2739 {
2740         stc_error_e ret = STC_ERROR_NONE;
2741         stc_app_value_s *app_lookup;
2742         stc_process_value_s *proc_lookup;
2743
2744         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2745
2746         app_lookup = __application_lookup(g_system->apps, &app_key);
2747         if (!app_lookup) {
2748                 STC_LOGD("app_key not found");
2749                 return STC_ERROR_FAIL;
2750         }
2751
2752         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2753         if (!proc_lookup) {
2754                 STC_LOGD("proc_key not found");
2755                 return STC_ERROR_FAIL;
2756         }
2757
2758         if (proc_lookup->ground != ground)
2759                 proc_lookup->ground = ground;
2760
2761         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2762
2763         return ret;
2764 }
2765 //LCOV_EXCL_STOP
2766
2767 void stc_monitor_update_rstn_by_default_connection(void *data)
2768 {
2769         static default_connection_s old_connection;
2770         default_connection_s *new_connection = (default_connection_s *)data;
2771
2772         if (old_connection.path != NULL) {
2773                 //LCOV_EXCL_START
2774                 if (g_system->apps)
2775                         g_tree_foreach(g_system->apps,
2776                                        __remove_application_monitor,
2777                                        (gpointer)&old_connection);
2778
2779                 if (g_system->rstns)
2780                         g_tree_foreach(g_system->rstns,
2781                                        __remove_restriction,
2782                                        (gpointer)&old_connection);
2783
2784                 iptables_flush_chains();
2785                 //LCOV_EXCL_STOP
2786         }
2787
2788         FREE(old_connection.path);
2789         FREE(old_connection.ifname);
2790         FREE(old_connection.tether_iface.ifname);
2791         old_connection.type = 0;
2792         old_connection.roaming = 0;
2793         old_connection.tether_state = FALSE;
2794         old_connection.tether_iface.type = 0;
2795
2796         if (new_connection != NULL && new_connection->path != NULL) {
2797                 if (g_system->apps)
2798                         g_tree_foreach(g_system->apps,
2799                                        __add_application_monitor,
2800                                        (gpointer)new_connection);
2801
2802                 if (g_system->rstns)
2803                         g_tree_foreach(g_system->rstns, __add_restriction,
2804                                        NULL);
2805
2806                 old_connection.path = g_strdup(new_connection->path);
2807                 old_connection.ifname = g_strdup(new_connection->ifname);
2808                 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
2809                 old_connection.type = new_connection->type;
2810                 old_connection.roaming = new_connection->roaming;
2811                 old_connection.tether_state = new_connection->tether_state;
2812                 old_connection.tether_iface.type = new_connection->tether_iface.type;
2813         }
2814 }
2815
2816 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
2817 {
2818         stc_error_e ret;
2819
2820         stc_rstn_key_s key;
2821         stc_rstn_value_s value;
2822
2823         memset(&key, 0, sizeof(stc_rstn_key_s));
2824         memset(&value, 0, sizeof(stc_rstn_value_s));
2825
2826         key.app_id = g_strdup(info->app_id);
2827         key.ifname = g_strdup(info->ifname);
2828         key.mac = g_strdup(info->mac);
2829         key.subscriber_id = g_strdup(info->subscriber_id);
2830         key.iftype = info->iftype;
2831         key.roaming = info->roaming;
2832
2833         value.rstn_type = info->rstn_type;
2834         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2835         value.restriction_id = info->restriction_id;
2836
2837         if (info->app_id)
2838                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2839         else
2840                 value.classid = STC_UNKNOWN_CLASSID;
2841
2842         if (value.classid == STC_BACKGROUND_APP_CLASSID) {
2843                 g_system->background_state = TRUE; //LCOV_EXCL_LINE
2844                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, TRUE); //LCOV_EXCL_LINE
2845                 __process_update_background(); //LCOV_EXCL_LINE
2846         }
2847
2848         value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2849         value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2850         value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2851         value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2852         value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2853         value.month_start_date = info->month_start_date;
2854         value.month_start_ts = stc_time_get_month_start(time(NULL),
2855                                                                                 info->month_start_date);
2856
2857         ret = __rstn_tree_add(&key, &value, TRUE);
2858
2859         FREE(key.app_id);
2860         FREE(key.ifname);
2861         FREE(key.mac);
2862         FREE(key.subscriber_id);
2863         return ret;
2864 }
2865
2866 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2867 {
2868         stc_error_e ret;
2869
2870         stc_rstn_key_s key = {
2871                 .app_id = g_strdup(info->app_id),
2872                 .ifname = g_strdup(info->ifname),
2873                 .subscriber_id = g_strdup(info->subscriber_id),
2874                 .iftype = info->iftype,
2875                 .roaming = info->roaming,
2876         };
2877
2878         if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2879                 g_system->background_state = FALSE; //LCOV_EXCL_LINE
2880                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, FALSE); //LCOV_EXCL_LINE
2881                 __process_update_background(); //LCOV_EXCL_LINE
2882         }
2883
2884         ret = __rstn_tree_remove(&key);
2885
2886         FREE(key.app_id);
2887         FREE(key.ifname);
2888         FREE(key.subscriber_id);
2889         return ret;
2890 }
2891
2892 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2893 {
2894         return stc_plugin_check_exception_by_cmdline(cmdline);
2895 }
2896
2897 int stc_monitor_get_counter_socket(void)
2898 {
2899         return g_system->contr_sock;
2900 }