Add tethering client data limitation
[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         counter.carg = stc->carg;
788         counter.classid = classid;
789         counter.intend = intend;
790
791         if (connection && connection->tether_iface.ifname != NULL &&
792                 classid == STC_TETHERING_APP_CLASSID) {
793                 counter.iftype = connection->tether_iface.type;
794                 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
795         } else {
796                 counter.iftype = iftype;
797                 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
798         }
799
800         g_free(default_ifname);
801
802         /* iptables rule */
803         __add_iptables_in(&counter);
804         __add_iptables_out(&counter);
805
806         /* ip6tables rule */
807         __add_ip6tables_in(&counter);
808         __add_ip6tables_out(&counter);
809 }
810
811 static void __add_tethering_iptables_rule(int64_t classid, gchar *mac,
812                 nfacct_rule_intend intend, stc_iface_type_e iftype)
813 {
814         default_connection_s *connection = stc_get_default_connection();
815         struct nfacct_rule counter;
816         stc_s *stc = stc_get_manager();
817         char *ipaddr = NULL;
818         int ret;
819
820         if (!stc || !mac)
821                 return;
822
823         if (!stc->carg) {
824                 stc->carg = MALLOC0(counter_arg_s, 1);
825                 if (stc->carg == NULL)
826                         return;
827
828                 stc->carg->sock = stc_monitor_get_counter_socket();
829         }
830
831         memset(&counter, 0, sizeof(struct nfacct_rule));
832
833         counter.carg = stc->carg;
834         counter.classid = classid;
835         counter.intend = intend;
836
837         if (connection->tether_state != TRUE ||
838                 connection->tether_iface.ifname == NULL)
839         return;
840
841         counter.iftype = connection->tether_iface.type;
842         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
843
844         /* get connected station ip based on its mac */
845         ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
846         if (ret != STC_ERROR_NONE)
847                 return;
848
849         /* tethering iptables rule */
850         __add_iptables_tether_in(&counter, ipaddr);
851         __add_iptables_tether_out(&counter, ipaddr);
852         g_free(ipaddr);
853 }
854
855 static void __del_tethering_iptables_rule(int64_t classid, gchar *mac,
856                 nfacct_rule_intend intend, stc_iface_type_e iftype)
857 {
858         default_connection_s *connection = stc_get_default_connection();
859         struct nfacct_rule counter;
860         stc_s *stc = stc_get_manager();
861         char *ipaddr = NULL;
862         int ret;
863
864         if (!stc || !mac)
865                 return;
866
867         if (!stc->carg) {
868                 stc->carg = MALLOC0(counter_arg_s, 1);
869                 if (stc->carg == NULL)
870                         return;
871
872                 stc->carg->sock = stc_monitor_get_counter_socket();
873         }
874
875         memset(&counter, 0, sizeof(struct nfacct_rule));
876
877         counter.carg = stc->carg;
878         counter.classid = classid;
879         counter.intend = intend;
880
881         if (connection->tether_state != TRUE ||
882                 connection->tether_iface.ifname == NULL)
883         return;
884
885         counter.iftype = connection->tether_iface.type;
886         g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
887
888         /* get connected station ip based on its mac */
889         ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
890         if (ret != STC_ERROR_NONE) {
891                 STC_LOGE("Error: no IP found for station mac(%s)", mac);
892                 return;
893         }
894
895         /* tethering iptables rule */
896         __del_iptables_tether_in(&counter, ipaddr);
897         __del_iptables_tether_out(&counter, ipaddr);
898         g_free(ipaddr);
899 }
900
901 static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
902                                 stc_iface_type_e iftype)
903 {
904         char *default_ifname = stc_default_connection_get_ifname();
905         default_connection_s *connection = stc_get_default_connection();
906         struct nfacct_rule counter;
907         stc_s *stc = stc_get_manager();
908         if (!stc) {
909                 g_free(default_ifname); //LCOV_EXCL_LINE
910                 return; //LCOV_EXCL_LINE
911         }
912
913         if (!stc->carg) {
914                 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
915                 if (stc->carg == NULL) { //LCOV_EXCL_LINE
916                         g_free(default_ifname); //LCOV_EXCL_LINE
917                         return; //LCOV_EXCL_LINE
918                 }
919
920                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
921         }
922
923         counter.carg = stc->carg;
924         counter.classid = classid;
925         counter.intend = intend;
926
927         if (connection && connection->tether_iface.ifname != NULL &&
928                 classid == STC_TETHERING_APP_CLASSID) {
929                 counter.iftype = connection->tether_iface.type;
930                 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
931         } else {
932                 counter.iftype = iftype;
933                 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
934         }
935
936         g_free(default_ifname);
937
938         /* iptables rule */
939         __del_iptables_in(&counter);
940         __del_iptables_out(&counter);
941
942         /* ip6tables rule */
943         __del_ip6tables_in(&counter);
944         __del_ip6tables_out(&counter);
945 }
946
947 static void __set_rstn_noti_state(int value)
948 {
949         int state = STC_RSTN_STATE_INIT;
950
951         if (__vconf_get_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, &state))
952                 return;
953
954         if (state == value) {
955                 STC_LOGI("No need to change a restriction status: %d", state);
956                 return;
957         }
958
959         vconf_set_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, value);
960         return;
961 }
962
963 typedef struct {
964         time_t month_start_ts;
965         time_t week_start_ts;
966         time_t day_start_ts;
967         int64_t monthly_stat;
968         int64_t weekly_stat;
969         int64_t daily_stat;
970 } cumulative_data_s;
971
972 static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info,
973                                         void *user_data)
974 {
975         cumulative_data_s *stat = (cumulative_data_s *)user_data;
976         int64_t counters = 0;
977
978         counters = info->cnt.in_bytes + info->cnt.out_bytes;
979
980         stat->monthly_stat += counters;
981         if (stat->week_start_ts <= info->interval->from)
982                 stat->weekly_stat += counters;
983         if (stat->day_start_ts <= info->interval->from)
984                 stat->daily_stat += counters;
985
986         return STC_CONTINUE;
987 }
988
989 static void __process_tethering_restriction(enum traffic_restriction_type rstn_type,
990                 stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value, void *data)
991 {
992         default_connection_s *old_connection = (default_connection_s *)data;
993         default_connection_s *connection = NULL;
994         char *mac_str = NULL;
995
996         if (old_connection != NULL)
997                 connection = old_connection;
998         else
999                 connection = stc_get_default_connection();
1000
1001         /* in case tethering is not active */
1002         if (connection->tether_state == FALSE)
1003                 return;
1004
1005         /* rstn not applicable for this interface */
1006         if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
1007                         (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
1008                 return;
1009
1010         /* in case appid not a tethering app */
1011         if (!g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX))
1012                 return;
1013
1014         /* Ignore TOTAL_TETHERING,
1015          * Process only station appids */
1016         if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
1017                 return;
1018
1019         /* get the station mac based on classid */
1020         stc_plugin_tether_get_station_by_classid(rstn_value->classid, &mac_str);
1021         if (!mac_str) {
1022                 STC_LOGE("station not found for classid(%d)", rstn_value->classid);
1023                 return;
1024         }
1025
1026         switch (rstn_type) {
1027         case RST_SET:
1028         {
1029                 int i;
1030                 table_counters_info info;
1031                 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
1032
1033                         memset(&info, 0, sizeof(table_counters_info));
1034                         rstn_value->limit_exceeded = 0;
1035
1036                         if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1037                                                 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1038                                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1039                                          rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1040                                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1041                                          rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1042                                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1043                                          rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1044                                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1045                                          rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1046                                 table_counters_get(rstn_value->restriction_id, &info);
1047
1048                                 time_t current_time = 0;
1049                                 cumulative_data_s stat;
1050                                 table_statistics_select_rule rule;
1051
1052                                 memset(&stat, 0, sizeof(cumulative_data_s));
1053                                 stat.month_start_ts = rstn_value->month_start_ts;
1054                                 stat.week_start_ts = g_system->last_week_ts;
1055                                 stat.day_start_ts = g_system->last_day_ts;
1056
1057                                 memset(&rule, 0, sizeof(table_statistics_select_rule));
1058                                 rule.from = rstn_value->month_start_ts;
1059                                 time(&current_time);
1060                                 rule.to = current_time;
1061                                 rule.iftype = rstn_key->iftype;
1062                                 rule.granularity = GRANULARITY;
1063
1064                                 table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
1065
1066                                 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1067                                 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1068                                 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
1069                                 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
1070                                 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
1071                         }
1072
1073                         for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1074                                 if (rstn_value->limit[i] >= 0) {
1075                                         effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
1076
1077                                         if (effective_limit[i] < 0)
1078                                                 rstn_value->limit_exceeded |= (1 << i);
1079                                 }
1080                         }
1081
1082                         STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
1083                                         rstn_value->restriction_id, info.data_counter);
1084
1085                         if (rstn_value->limit_exceeded != 0 &&
1086                                         rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
1087                                 __add_tethering_iptables_rule(rstn_value->classid, mac_str,
1088                                                 NFACCT_TETH_BLOCK, rstn_key->iftype);
1089                         }
1090
1091                         rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1092         }
1093         break;
1094         case RST_EXCLUDE:
1095         {
1096                 __add_tethering_iptables_rule(rstn_value->classid, mac_str,
1097                                         NFACCT_TETH_ALLOW, rstn_key->iftype);
1098
1099                         rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1100                         rstn_value->limit_exceeded = 0;
1101                         rstn_value->limit_notified = 0;
1102         }
1103         break;
1104         case RST_UNSET:
1105         {
1106                         int i;
1107                         __del_tethering_iptables_rule(rstn_value->classid, mac_str,
1108                                         NFACCT_TETH_BLOCK, rstn_key->iftype);
1109
1110                         rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1111                         rstn_value->limit_exceeded = 0;
1112                         rstn_value->limit_notified = 0;
1113
1114                         for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
1115                                 if (rstn_value->limit[i] >= 0)
1116                                         rstn_value->counter[i] = 0;
1117         }
1118         break;
1119         default:
1120                         ;//Do Nothing
1121         }
1122         FREE(mac_str);
1123 }
1124
1125 static void __process_restriction(enum traffic_restriction_type rstn_type,
1126                                   stc_rstn_key_s *rstn_key,
1127                                   stc_rstn_value_s *rstn_value, void *data)
1128 {
1129         default_connection_s *old_connection = (default_connection_s *)data;
1130         default_connection_s *connection = NULL;
1131
1132         if (old_connection != NULL)
1133                 connection = old_connection;
1134         else
1135                 connection = stc_get_default_connection();
1136
1137         /* no default ifname */
1138         if (connection->ifname == NULL)
1139                 return;
1140
1141         /* rstn not applicable for this interface */
1142         if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
1143             (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
1144                 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
1145                 return;
1146
1147         /* classid is invalid */
1148         if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
1149                 return;
1150
1151         /* Do not proceed for tethering station appid if found here,
1152          * for tethering station apps __process_tethering_restriction() call
1153          * will handle it */
1154         if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) &&
1155                         rstn_value->classid != STC_TETHERING_APP_CLASSID)
1156                 return;
1157
1158         switch (rstn_type) {
1159         case RST_SET:
1160         {
1161                 int i;
1162                 table_counters_info info;
1163                 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
1164
1165                 memset(&info, 0, sizeof(table_counters_info));
1166                 rstn_value->limit_exceeded = 0;
1167
1168                 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1169                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1170                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1171                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1172                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1173                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1174                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1175                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1176                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1177                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1178                         table_counters_get(rstn_value->restriction_id, &info);
1179
1180                         time_t current_time = 0;
1181                         cumulative_data_s stat;
1182                         table_statistics_select_rule rule;
1183
1184                         memset(&stat, 0, sizeof(cumulative_data_s));
1185                         stat.month_start_ts = rstn_value->month_start_ts;
1186                         stat.week_start_ts = g_system->last_week_ts;
1187                         stat.day_start_ts = g_system->last_day_ts;
1188
1189                         memset(&rule, 0, sizeof(table_statistics_select_rule));
1190                         rule.from = rstn_value->month_start_ts;
1191                         time(&current_time);
1192                         rule.to = current_time;
1193                         rule.iftype = rstn_key->iftype;
1194                         rule.granularity = GRANULARITY;
1195
1196                         table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
1197
1198                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1199                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1200                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
1201                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
1202                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
1203                 }
1204
1205                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1206                         if (rstn_value->limit[i] >= 0) {
1207                                 effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
1208
1209                                 if (effective_limit[i] < 0)
1210                                         rstn_value->limit_exceeded |= (1 << i);
1211                         }
1212                 }
1213
1214                 STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
1215                         rstn_value->restriction_id, info.data_counter);
1216
1217                 if (rstn_value->limit_exceeded != 0 &&
1218                         rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
1219                         __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK, rstn_key->iftype);
1220                 }
1221
1222                 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1223         }
1224         break;
1225         case RST_EXCLUDE:
1226                 __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
1227                                     rstn_key->iftype);
1228
1229                 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
1230                 rstn_value->limit_exceeded = 0;
1231                 rstn_value->limit_notified = 0;
1232                 break;
1233         case RST_UNSET:
1234         {
1235                 int i;
1236
1237                 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
1238                         __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
1239                                             rstn_key->iftype);
1240                 else
1241                         __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
1242                                             rstn_key->iftype);
1243
1244                 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1245                 rstn_value->limit_exceeded = 0;
1246                 rstn_value->limit_notified = 0;
1247
1248                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
1249                         if (rstn_value->limit[i] >= 0)
1250                                 rstn_value->counter[i] = 0;
1251
1252                 __set_rstn_noti_state(STC_RSTN_STATE_UNSET);
1253         }
1254         break;
1255         default:
1256                 ;//Do Nothing
1257         }
1258 }
1259
1260 //LCOV_EXCL_START
1261 static gboolean __remove_rstns_foreach_application(gpointer key,
1262                                                    gpointer value,
1263                                                    gpointer data)
1264 {
1265         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1266         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1267         gchar *app_id = (gchar *)data;
1268
1269         /* rstn rule is not for applications */
1270         if (rstn_key->app_id == NULL)
1271                 goto out;
1272
1273         /* rstn rule is not for this application */
1274         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1275                 goto out;
1276
1277         /* rstn rule is already removed */
1278         if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
1279                 goto out;
1280
1281         /* remove restriction from system */
1282         __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
1283
1284         /* remove tethering restriction from system*/
1285         __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
1286
1287         __print_rstn(rstn_key, rstn_value);
1288 out:
1289         return FALSE;
1290 }
1291 //LCOV_EXCL_STOP
1292
1293 static void __remove_rstns_for_application(gchar *app_id)
1294 {
1295         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
1296                        app_id);
1297 }
1298
1299 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
1300 {
1301         stc_error_e ret = STC_ERROR_NONE;
1302         guint pid_count = 0;
1303         stc_app_value_s *lookup;
1304
1305         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1306
1307         lookup = __application_lookup(g_system->apps, app_key);
1308         if (!lookup) {
1309                 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
1310                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1311         }
1312
1313         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
1314                        &pid_count);
1315
1316         if (!pid_count) {
1317                 /* remove nfacct rule for this classid */
1318                 __remove_application_monitor((gpointer) app_key, lookup,
1319                                              stc_get_default_connection());
1320                 __remove_rstns_for_application(app_key->app_id);
1321         }
1322
1323         if (!g_tree_remove(g_system->apps, app_key)) {
1324                 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1325                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1326         }
1327
1328         return ret;
1329 }
1330
1331 static stc_error_e __close_contr_sock(stc_system_s *system)
1332 {
1333         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
1334
1335         /* close netlink socket for updating kernel counters */
1336         if (system->contr_sock != -1) {
1337                 close(system->contr_sock);
1338                 system->contr_sock = -1;
1339         }
1340
1341         if (system->contr_gsource_id != 0) {
1342                 g_source_remove(system->contr_gsource_id);
1343                 system->contr_gsource_id = 0;
1344         }
1345
1346         return STC_ERROR_NONE;
1347 }
1348
1349 static gboolean __process_contr_reply(GIOChannel *source,
1350                                       GIOCondition condition,
1351                                       gpointer user_data);
1352
1353 //LCOV_EXCL_START
1354 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
1355 {
1356         GIOChannel *gio = NULL;
1357         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
1358
1359         /* close netlink socket for updating kernel counters */
1360         if (system->contr_sock != -1) {
1361                 close(system->contr_sock);
1362                 system->contr_sock = -1;
1363         }
1364
1365         if (system->contr_gsource_id != 0) {
1366                 g_source_remove(system->contr_gsource_id);
1367                 system->contr_gsource_id = 0;
1368         }
1369
1370         /* create netlink socket for updating kernel counters */
1371         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1372         if (system->contr_sock < 0) {
1373                 STC_LOGE("failed to open socket");
1374                 FREE(system);
1375                 return STC_ERROR_FAIL;
1376         }
1377
1378         gio = g_io_channel_unix_new(system->contr_sock);
1379         system->contr_gsource_id =
1380                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1381                                (GIOFunc) __process_contr_reply,
1382                                NULL);
1383         g_io_channel_unref(gio);
1384
1385         return STC_ERROR_NONE;
1386 }
1387
1388 static void __action_when_rstn_limit_exceeded_tethering(stc_rstn_key_s *rstn_key,
1389                 stc_rstn_value_s *rstn_value, classid_bytes_context_s *context)
1390 {
1391         char *mac_str = NULL;
1392         struct nfacct_rule *counter = context->counter;
1393
1394         /* get the station mac based on classid */
1395         stc_plugin_tether_get_station_by_classid(counter->classid, &mac_str);
1396         if (!mac_str) {
1397                 STC_LOGE("station not found for classid(%d)", counter->classid);
1398                 return;
1399         }
1400
1401         STC_LOGI("station mac %s, classid %u, iftype %u, iotype %d, \
1402                         intend %d, ifname %s, bytes %lld", mac_str,
1403                         counter->classid, counter->iftype, counter->iotype,
1404                         counter->intend, counter->ifname, context->bytes);
1405
1406         /* Block tethering station immediately */
1407         counter->intend = NFACCT_TETH_BLOCK;
1408         __del_tethering_iptables_rule(counter->classid, mac_str,
1409                         NFACCT_TETH_BLOCK, rstn_key->iftype);
1410
1411         __add_tethering_iptables_rule(counter->classid, mac_str,
1412                         NFACCT_TETH_BLOCK, rstn_key->iftype);
1413         counter->intend = NFACCT_TETH_COUNTER;
1414
1415         g_free(mac_str);
1416 }
1417
1418 static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type,
1419                                                 stc_rstn_key_s *rstn_key,
1420                                                 stc_rstn_value_s *rstn_value,
1421                                                 classid_bytes_context_s *context)
1422 {
1423         gboolean rv;
1424         char iftype[MAX_INT_LENGTH] = { 0, };
1425         char byte[MAX_INT_LENGTH] = { 0, };
1426         const char *signal_name = NULL;
1427         const char *net_popup_content = NULL;
1428         const char *net_popup_type = NULL;
1429         stc_s *stc = (stc_s *)stc_get_manager();
1430
1431         if (stc == NULL) {
1432                 STC_LOGE("Failed to get stc data");
1433                 return;
1434         }
1435
1436         switch (limit_type) {
1437         case STC_RSTN_LIMIT_TYPE_DATA_WARN:
1438         {
1439                 signal_name = "WarnThresholdCrossed";
1440                 net_popup_content = "warn threshold crossed";
1441                 net_popup_type = "warning_noti";
1442         }
1443         break;
1444         case STC_RSTN_LIMIT_TYPE_DATA:
1445         case STC_RSTN_LIMIT_TYPE_MONTHLY:
1446         case STC_RSTN_LIMIT_TYPE_WEEKLY:
1447         case STC_RSTN_LIMIT_TYPE_DAILY:
1448         {
1449                 signal_name = "RestrictionThresholdCrossed";
1450                 net_popup_content = "restriction threshold crossed";
1451                 net_popup_type = "restriction_noti";
1452
1453                 /* Apply restriction for tethering apps if app_id is of tethering client
1454                  * otherwise do the normal iptables rule */
1455                 if (context->counter->intend == NFACCT_TETH_COUNTER) {
1456
1457                         if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) &&
1458                                         rstn_value->classid != STC_TETHERING_APP_CLASSID) {
1459                                 __action_when_rstn_limit_exceeded_tethering(rstn_key, rstn_value,
1460                                                 context);
1461                         }
1462
1463                 } else {
1464                         /* block immediately */
1465                         context->counter->intend = NFACCT_BLOCK;
1466                         __del_iptables_in(context->counter);
1467                         __del_iptables_out(context->counter);
1468                         __add_iptables_in(context->counter);
1469                         __add_iptables_out(context->counter);
1470
1471                         __del_ip6tables_in(context->counter);
1472                         __del_ip6tables_out(context->counter);
1473                         __add_ip6tables_in(context->counter);
1474                         __add_ip6tables_out(context->counter);
1475                         context->counter->intend = NFACCT_COUNTER;
1476                 }
1477
1478                 rstn_value->limit_exceeded |= (1 << limit_type);
1479
1480                 __set_rstn_noti_state(STC_RSTN_STATE_SET);
1481         }
1482         break;
1483         default:
1484                 break;
1485         }
1486
1487         if (signal_name == NULL) {
1488                 STC_LOGE("Invalid parameter: limit_type");
1489                 return;
1490         }
1491
1492         /* emit signal */
1493         rv = stc_manager_dbus_emit_signal(stc->connection,
1494                                                   STC_DBUS_SERVICE_RESTRICTION_PATH,
1495                                                   STC_DBUS_INTERFACE_RESTRICTION,
1496                                                   signal_name,
1497                                                   g_variant_new("(si)",
1498                                                   rstn_key->app_id,
1499                                                   rstn_key->iftype));
1500
1501         if (rv == TRUE)
1502                 rstn_value->limit_notified |= (1 << limit_type);
1503
1504         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
1505         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
1506         stc_plugin_appstatus_send_message(net_popup_content,
1507                         net_popup_type, rstn_key->app_id, iftype, byte);
1508 }
1509
1510 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
1511                                       stc_rstn_value_s *rstn_value,
1512                                       classid_bytes_context_s *context)
1513 {
1514         int i;
1515         switch (context->counter->iotype) {
1516         case NFACCT_COUNTER_IN:
1517         case NFACCT_COUNTER_OUT:
1518                 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
1519                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
1520                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
1521                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
1522                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
1523                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
1524                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
1525                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
1526                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
1527                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
1528                         table_counters_info info;
1529                         memset(&info, 0, sizeof(table_counters_info));
1530                         table_counters_get(rstn_value->restriction_id, &info);
1531
1532                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
1533                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
1534                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
1535                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
1536                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
1537                 }
1538
1539                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1540                         if (rstn_value->limit[i] >= 0 &&
1541                                 !(rstn_value->limit_notified & (1 << i))) {
1542                                 rstn_value->counter[i] += context->bytes;
1543                                 if (rstn_value->limit[i] <= rstn_value->counter[i])
1544                                         __action_when_rstn_limit_exceeded(i,
1545                                                                                 rstn_key,
1546                                                                                 rstn_value,
1547                                                                                 context);
1548                         }
1549                 }
1550
1551                 g_system->rstns_tree_updated = TRUE;
1552                 __print_rstn(rstn_key, rstn_value);
1553                 break;
1554         default:
1555                 STC_LOGE("unknown iotype");
1556         }
1557
1558         return FALSE;
1559 }
1560
1561 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
1562                                                 stc_rstn_value_s *rstn_value,
1563                                                 classid_bytes_context_s *context)
1564 {
1565         if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1566                 context->counter->iftype == STC_IFACE_DATACALL) ||
1567                 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
1568                 context->counter->iftype == STC_IFACE_WIFI) ||
1569                 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1570                 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1571                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1572                  context->counter->iftype == STC_IFACE_WIFI) ||
1573                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1574                  context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1575                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1576                  context->counter->iftype == STC_IFACE_USB) ||
1577                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1578                  context->counter->iftype == STC_IFACE_P2P)) {
1579                 context->counter->classid = rstn_value->classid;
1580                 return __rstn_counter_update(rstn_key, rstn_value, context);
1581         }
1582
1583         return FALSE;
1584 }
1585
1586 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
1587                                                       gpointer value,
1588                                                       gpointer data)
1589 {
1590         gboolean rv = FALSE;
1591         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1592         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1593         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1594         uint32_t classid;
1595
1596         if (context->counter->intend != NFACCT_COUNTER &&
1597                         context->counter->intend != NFACCT_TETH_COUNTER)
1598                 goto try_next_callback;
1599
1600         if (rstn_value->limit_exceeded == TRUE) {
1601                 context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE
1602                 goto try_next_callback; //LCOV_EXCL_LINE
1603         }
1604
1605         classid = context->counter->classid;
1606         rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
1607
1608         context->counter->classid = classid;
1609         if (rstn_value->classid != context->counter->classid)
1610                 goto try_next_callback;
1611
1612         rv = __rstn_counter_update(rstn_key, rstn_value, context);
1613
1614 try_next_callback:
1615         return rv;
1616 }
1617 //LCOV_EXCL_STOP
1618
1619 static gboolean __update_app_statistics(gpointer key, gpointer value,
1620                                         gpointer data)
1621 {
1622         stc_app_key_s *app_key = (stc_app_key_s *)key;
1623         stc_app_value_s *app_value = (stc_app_value_s *)value;
1624         time_t *touch_time = (time_t *)data;
1625         stc_db_classid_iftype_key stat_key;
1626         stc_db_app_stats stat;
1627         default_connection_s *default_connection = stc_get_default_connection();
1628
1629         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
1630         memset(&stat, 0 , sizeof(stc_db_app_stats));
1631
1632         /* Do not update statistics for Tethering
1633          * if tethering is in-active found */
1634         if (default_connection &&
1635                 default_connection->tether_state == FALSE &&
1636                 !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
1637                 return FALSE;
1638
1639         /* Do not update statistics for Wi-Fi
1640          * if tethering is active on wlan0 iface */
1641         if (default_connection && default_connection->tether_state &&
1642                 default_connection->tether_iface.type == STC_IFACE_WIFI &&
1643                 !strcmp(app_key->app_id, STC_TOTAL_WIFI))
1644                 return FALSE;
1645
1646         stat_key.classid = app_value->classid;
1647
1648         if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1649                 default_connection->tether_state == TRUE)
1650                 stat_key.iftype = default_connection->tether_iface.type;
1651         else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
1652                 stat_key.iftype = default_connection->tether_iface.type;
1653         else
1654                 stat_key.iftype = default_connection->type;
1655
1656         if (STC_IFACE_DATACALL == stat_key.iftype)
1657                 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
1658         else
1659                 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
1660
1661         if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1662                 default_connection->tether_state == TRUE)
1663                 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1664                           MAX_IFACE_LENGTH);
1665         else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
1666                 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1667                           MAX_IFACE_LENGTH);
1668         else
1669                 g_strlcpy(stat_key.ifname, default_connection->ifname,
1670                           MAX_IFACE_LENGTH);
1671
1672         stat.app_id = g_strdup(app_key->app_id);
1673         stat.snd_count = app_value->counter.out_bytes;
1674         stat.rcv_count = app_value->counter.in_bytes;
1675         stat.is_roaming = default_connection->roaming;
1676
1677         if (strstr(stat.app_id, "_BACKGROUND")) {
1678                 stat.ground = STC_APP_STATE_BACKGROUND;
1679         } else {
1680                 if (strstr(stat.app_id, "TOTAL_"))
1681                         stat.ground = STC_APP_STATE_UNKNOWN;
1682                 else
1683                         stat.ground = STC_APP_STATE_FOREGROUND;
1684         }
1685
1686         table_statistics_insert(&stat_key, &stat, *touch_time);
1687
1688         app_value->counter.out_bytes = 0;
1689         app_value->counter.in_bytes = 0;
1690
1691         FREE(stat.app_id);
1692         FREE(stat_key.subscriber_id);
1693
1694         return FALSE;
1695 }
1696
1697 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1698 {
1699         time_t current_time = 0;
1700         stc_s *stc = stc_get_manager();
1701
1702         if (stc && stc->carg)
1703                 current_time = stc->carg->last_run_time;
1704
1705         if (g_system->apps_tree_updated == FALSE)
1706                 return G_SOURCE_REMOVE;
1707
1708         g_system->apps_tree_updated = FALSE;
1709
1710         if (g_system->apps)
1711                 g_tree_foreach(g_system->apps,
1712                                __update_app_statistics,
1713                                &current_time);
1714
1715         STC_LOGI("Flushed app stats to database");
1716         return G_SOURCE_REMOVE;
1717 }
1718
1719 //LCOV_EXCL_START
1720 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1721                                             gpointer data)
1722 {
1723         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1724         table_counters_info info = {
1725                 .restriction_id = rstn_value->restriction_id,
1726                 .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
1727                 .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
1728                 .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
1729                 .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
1730                 .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY]
1731         };
1732
1733         table_counters_update_counters(&info);
1734
1735         return FALSE;
1736 }
1737
1738 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1739 {
1740         time_t current_time = 0;
1741         stc_s *stc = stc_get_manager();
1742
1743         if (stc && stc->carg)
1744                 current_time = stc->carg->last_run_time;
1745
1746         if (g_system->rstns_tree_updated == FALSE)
1747                 return G_SOURCE_REMOVE;
1748
1749         g_system->rstns_tree_updated = FALSE;
1750
1751         if (g_system->rstns)
1752                 g_tree_foreach(g_system->rstns,
1753                                __update_counter_statistics,
1754                                &current_time);
1755
1756         STC_LOGI("Flushed rstns counters to database");
1757         return G_SOURCE_REMOVE;
1758 }
1759 //LCOV_EXCL_STOP
1760
1761 static void __app_counter_update(stc_app_key_s *app_key,
1762                                  stc_app_value_s *app_value,
1763                                  classid_bytes_context_s *context)
1764 {
1765         switch (context->counter->iotype) {
1766         case NFACCT_COUNTER_IN:
1767                 app_value->data_usage.in_bytes += context->bytes;
1768                 app_value->counter.in_bytes = context->bytes;
1769                 g_system->apps_tree_updated = TRUE;
1770
1771                 /*
1772                 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1773                 */
1774                 break;
1775         case NFACCT_COUNTER_OUT:
1776                 app_value->data_usage.out_bytes += context->bytes;
1777                 app_value->counter.out_bytes = context->bytes;
1778                 g_system->apps_tree_updated = TRUE;
1779
1780                 /*
1781                 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1782                 */
1783                 break;
1784         default:
1785                 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1786         }
1787 }
1788
1789 static void __interface_counter_update(stc_app_key_s *app_key,
1790                                        stc_app_value_s *app_value,
1791                                        classid_bytes_context_s *context)
1792 {
1793         if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1794              context->counter->iftype == STC_IFACE_DATACALL) ||
1795             (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1796              context->counter->iftype == STC_IFACE_WIFI) ||
1797             (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1798              context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1799                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1800                  context->counter->iftype == STC_IFACE_WIFI) ||
1801                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1802                  context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1803                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1804                  context->counter->iftype == STC_IFACE_USB) ||
1805                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1806                  context->counter->iftype == STC_IFACE_P2P))
1807                  __app_counter_update(app_key, app_value, context);
1808 }
1809
1810
1811 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1812                                                       gpointer value,
1813                                                       gpointer data)
1814 {
1815         stc_app_key_s *app_key = (stc_app_key_s *)key;
1816         stc_app_value_s *app_value = (stc_app_value_s *)value;
1817         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1818
1819         if (context->counter->intend != NFACCT_COUNTER &&
1820                         context->counter->intend != NFACCT_TETH_COUNTER)
1821                 goto try_next_callback;
1822
1823         __interface_counter_update(app_key, app_value, context);
1824
1825         if (app_value->classid != context->counter->classid)
1826                 goto try_next_callback;
1827
1828         __app_counter_update(app_key, app_value, context);
1829
1830 try_next_callback:
1831         return FALSE;
1832 }
1833
1834 static gboolean __reset_time_counter_foreach_rstn(gpointer key,
1835                                                   gpointer value,
1836                                                   gpointer data)
1837 {
1838         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1839         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1840         reset_time_limits_context_s *context = (reset_time_limits_context_s *)data;
1841         int i;
1842         time_t now_month_start_ts;
1843
1844         if (rstn_value->month_start_date == 0) {
1845                 table_counters_info info;
1846                 memset(&info, 0, sizeof(table_counters_info));
1847                 table_counters_get_timestamps(rstn_value->restriction_id, &info);
1848
1849                 if (info.month_start_date == 0)
1850                         rstn_value->month_start_date = 1;
1851                 else
1852                         rstn_value->month_start_date = info.month_start_date;
1853                 rstn_value->month_start_ts = info.month_start_ts;
1854         }
1855
1856         now_month_start_ts =
1857                 stc_time_get_month_start(context->now,
1858                                          rstn_value->month_start_date);
1859
1860         if (rstn_value->month_start_ts != now_month_start_ts) {
1861                 rstn_value->month_start_ts = now_month_start_ts;
1862                 context->month_start_ts = now_month_start_ts;
1863                 context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
1864         }
1865
1866         if (context->is_updated) {
1867                 table_counters_info info;
1868                 memset(&info, 0, sizeof(table_counters_info));
1869
1870                 info.restriction_id = rstn_value->restriction_id;
1871                 info.month_start_date = rstn_value->month_start_date;
1872                 info.month_start_ts = rstn_value->month_start_ts;
1873                 info.week_start_ts = context->week_start_ts;
1874                 info.day_start_ts = context->day_start_ts;
1875
1876                 table_counters_update_timestamps(&info);
1877         }
1878
1879         for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1880
1881                 if ((context->is_updated) & (1 << i)) {
1882                         /* reset limit */
1883                         rstn_value->counter[i] = 0;
1884
1885                         if (rstn_value->limit_exceeded & (1 << i)) {
1886                                 /* remove iptables rule */
1887                                 char *default_ifname = stc_default_connection_get_ifname();
1888                                 struct nfacct_rule counter;
1889                                 stc_s *stc = stc_get_manager();
1890                                 if (stc == NULL) {
1891                                         STC_LOGE("Can't get stc data");
1892                                         g_free(default_ifname);
1893                                         goto try_next_callback;
1894                                 }
1895
1896                                 if (!stc->carg) {
1897                                         stc->carg = MALLOC0(counter_arg_s, 1);
1898                                         if (stc->carg == NULL) {
1899                                                 g_free(default_ifname);
1900                                                 goto try_next_callback;
1901                                         }
1902
1903                                         stc->carg->sock =
1904                                                 stc_monitor_get_counter_socket();
1905                                 }
1906
1907                                 counter.carg = stc->carg;
1908                                 counter.classid = rstn_value->classid;
1909                                 counter.intend = NFACCT_BLOCK;
1910                                 counter.iftype = rstn_key->iftype;
1911                                 g_strlcpy(counter.ifname, default_ifname,
1912                                           MAX_IFACE_LENGTH);
1913
1914                                 g_free(default_ifname);
1915
1916                                 /* iptables rule */
1917                                 __del_iptables_in(&counter);
1918                                 __del_iptables_out(&counter);
1919
1920                                 /* ip6tables rule */
1921                                 __del_ip6tables_in(&counter);
1922                                 __del_ip6tables_out(&counter);
1923
1924                                 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1925                                 rstn_value->limit_exceeded &= ~(1 << i);
1926                                 rstn_value->limit_notified &= ~(1 << i);
1927                         }
1928                 }
1929         }
1930
1931 try_next_callback:
1932         return FALSE;
1933 }
1934
1935 static void __reset_time_counters_if_required(void)
1936 {
1937         reset_time_limits_context_s context;
1938
1939         if (g_system == NULL) {
1940                 STC_LOGE("stc monitor not initialized!");
1941                 return;
1942         }
1943
1944         context.now = time(NULL);
1945         context.week_start_ts = stc_time_get_week_start(context.now);
1946         context.day_start_ts = stc_time_get_day_start(context.now);
1947         context.is_updated = 0;
1948
1949         if (g_system->last_week_ts != context.week_start_ts) {
1950                 g_system->last_week_ts = context.week_start_ts;
1951                 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
1952         }
1953
1954         if (g_system->last_day_ts != context.day_start_ts) {
1955                 g_system->last_day_ts = context.day_start_ts;
1956                 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
1957         }
1958
1959         if (g_system->rstns) {
1960                 g_tree_foreach(g_system->rstns,
1961                                __reset_time_counter_foreach_rstn,
1962                                &context);
1963                 if (context.is_updated)
1964                         STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]",
1965                                  context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts);
1966         }
1967 }
1968
1969 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1970                                  struct counter_arg *carg)
1971 {
1972         __reset_time_counters_if_required();
1973
1974         struct nfacct_rule counter = {
1975                 .carg = carg,
1976                 .name = {0},
1977                 .ifname = {0},
1978                 0
1979         };
1980
1981         classid_bytes_context_s context = {
1982                 .counter = &counter,
1983                 .bytes = bytes,
1984                 .data_limit_exceeded = FALSE,
1985         };
1986
1987         if (!recreate_counter_by_name(cnt_name, &counter)) {
1988                 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1989                 return; //LCOV_EXCL_LINE
1990         }
1991
1992         if (STC_DEBUG_LOG)
1993                 STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1994                         context.counter->classid, context.counter->iftype,
1995                         context.counter->iotype, context.counter->intend,
1996                         context.counter->ifname, context.bytes);
1997
1998         if (g_system->rstns)
1999                 g_tree_foreach(g_system->rstns,
2000                                __rstn_counter_update_foreach_classid,
2001                                &context);
2002
2003         if (g_system->apps)
2004                 g_tree_foreach(g_system->apps,
2005                                __apps_counter_update_foreach_classid,
2006                                &context);
2007 }
2008
2009 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
2010                            void *user_data)
2011 {
2012         struct counter_arg *carg = user_data;
2013         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
2014         if (carg->initiate) {
2015                 /**
2016                  * TODO: this will be used when daemon starts to update existing
2017                  * counter data if present.
2018                  *
2019                  populate_counters(cnt_name, carg);
2020                  */
2021         } else {
2022                 int64_t *bytes_p =
2023                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
2024                 int bytes = be64toh(*bytes_p);
2025                 if (bytes) {
2026                         ++carg->serialized_counters;
2027                         __fill_nfacct_result(cnt_name, bytes, carg);
2028                 }
2029         }
2030
2031         return 0;
2032 }
2033
2034 static int __post_fill_counters(void *user_data)
2035 {
2036         struct counter_arg *carg = user_data;
2037
2038         if (carg->initiate)
2039                 carg->initiate = 0;
2040
2041         return 0;
2042 }
2043
2044 static void __process_network_counter(struct genl *ans,
2045                                       struct counter_arg *carg)
2046 {
2047         struct netlink_serialization_params ser_params = {
2048                 .carg = carg,
2049                 .ans = ans,
2050                 .eval_attr = __fill_counters,
2051                 .post_eval_attr = __post_fill_counters,
2052         };
2053
2054         netlink_serialization_command *netlink =
2055                 netlink_create_command(&ser_params);
2056         if (!netlink) {
2057                 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
2058                 return; //LCOV_EXCL_LINE
2059         }
2060
2061         netlink->deserialize_answer(&(netlink->params));
2062 }
2063
2064 static gboolean __process_contr_reply(GIOChannel *source,
2065                                       GIOCondition condition,
2066                                       gpointer user_data)
2067 {
2068         int sock = g_io_channel_unix_get_fd(source);
2069         struct genl *ans;
2070         int ret;
2071         stc_s *stc = stc_get_manager();
2072
2073 #ifdef TIZEN_GTESTS
2074         void __gcov_flush(void);
2075         __gcov_flush();
2076 #endif
2077
2078         if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
2079             (condition & G_IO_NVAL)) {
2080                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
2081
2082                 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
2083                          "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
2084                          (condition & G_IO_ERR), (condition & G_IO_HUP),
2085                          (condition & G_IO_NVAL));
2086                 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
2087                 return FALSE; //LCOV_EXCL_LINE
2088         }
2089
2090         ans = MALLOC0(struct genl, 1);
2091         if (ans == NULL) {
2092                 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
2093                 return TRUE; //LCOV_EXCL_LINE
2094         }
2095
2096         if (stc == NULL) {
2097                 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
2098                 goto out; //LCOV_EXCL_LINE
2099         }
2100
2101         ret = read_netlink(sock, ans, sizeof(struct genl));
2102         /* STC_LOGD("Counter data received ret [%d]", ret); */
2103         if (ret == 0)
2104                 goto out;
2105
2106         stc->carg->ans_len = ret;
2107         stc->carg->last_run_time = time(NULL);
2108
2109         __process_network_counter(ans, stc->carg);
2110
2111         g_idle_add(__flush_apps_stats_to_database, NULL);
2112         g_idle_add(__flush_rstns_counter_to_database, NULL);
2113 out:
2114
2115         FREE(ans);
2116         return TRUE;
2117 }
2118
2119 static gboolean __update_contr_cb(void *user_data)
2120 {
2121         /* Here we just sent command, answer we receive in another callback */
2122         stc_s *stc = stc_get_manager();
2123         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
2124         if (!stc->carg) {
2125                 stc->carg = MALLOC0(counter_arg_s, 1);
2126                 if (stc->carg == NULL)
2127                         return TRUE;  /* we need to continue the timer */
2128
2129                 stc->carg->sock = stc_monitor_get_counter_socket();
2130         }
2131
2132 #ifdef TIZEN_GTESTS
2133         void __gcov_flush(void);
2134         __gcov_flush();
2135 #endif
2136
2137         /* STC_LOGD("Get all counters"); */
2138         nfacct_send_get_all(stc->carg);
2139
2140         /* we need to continue the timer */
2141         return TRUE;
2142 }
2143
2144 /*
2145 //LCOV_EXCL_START
2146 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
2147                                           gpointer data)
2148 {
2149         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2150         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2151
2152         __print_rstn(rstn_key, rstn_value);
2153         return FALSE;
2154 }
2155
2156 static void __rstn_tree_printall(void)
2157 {
2158         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
2159 }
2160 //LCOV_EXCL_STOP
2161 */
2162
2163 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
2164                                         const stc_rstn_key_s *key)
2165 {
2166         stc_rstn_value_s *lookup;
2167
2168         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
2169
2170         lookup = g_tree_lookup(rstns_tree, key);
2171
2172         return lookup;
2173 }
2174
2175 static gboolean __remove_restriction(gpointer key, gpointer value,
2176                                      gpointer data)
2177 {
2178         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2179         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2180
2181         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
2182         __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, data);
2183         __print_rstn(rstn_key, rstn_value);
2184         return FALSE;
2185 }
2186
2187 static gboolean __add_restriction_debug(gpointer key, gpointer value,
2188                                         gpointer data)
2189 {
2190         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2191         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2192
2193         /* rstn rule is activated */
2194         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2195                 return FALSE;
2196
2197         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2198                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2199                 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2200         } else {
2201                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
2202                 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data);
2203         }
2204
2205         __print_rstn(rstn_key, rstn_value);
2206
2207         return FALSE;
2208 }
2209
2210 //LCOV_EXCL_START
2211 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
2212 {
2213         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2214         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2215
2216         /* rstn rule is activated */
2217         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2218                 return FALSE;
2219
2220         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2221                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2222                 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
2223         } else {
2224                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
2225                 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data);
2226         }
2227
2228         return FALSE;
2229 }
2230 //LCOV_EXCL_STOP
2231
2232 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
2233 {
2234         stc_rstn_value_s *lookup_value;
2235
2236         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2237
2238         lookup_value = __rstn_lookup(g_system->rstns, key);
2239         if (!lookup_value) {
2240                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
2241                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
2242         }
2243
2244         /* remove counter also */
2245         table_counters_delete(lookup_value->restriction_id);
2246         __remove_restriction(key, lookup_value, NULL);
2247
2248         if (!g_tree_remove(g_system->rstns, key)) {
2249                 STC_LOGD("key not found"); //LCOV_EXCL_LINE
2250                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
2251         }
2252
2253         return STC_ERROR_NONE;
2254 }
2255
2256 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
2257                                    stc_rstn_value_s *value, gboolean debug)
2258 {
2259         stc_rstn_key_s *rstn_key;
2260         stc_rstn_value_s *rstn_value;
2261
2262         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2263
2264         rstn_value = __rstn_lookup(g_system->rstns, key);
2265         if (rstn_value)
2266                 __rstn_tree_remove(key);
2267
2268         rstn_key = MALLOC0(stc_rstn_key_s, 1);
2269         if (!rstn_key) {
2270                 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
2271                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2272         }
2273
2274         rstn_value = MALLOC0(stc_rstn_value_s, 1);
2275         if (!rstn_value) {
2276                 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
2277                 FREE(rstn_key); //LCOV_EXCL_LINE
2278                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2279         }
2280
2281         rstn_key->app_id = g_strdup(key->app_id);
2282         rstn_key->ifname = g_strdup(key->ifname);
2283         rstn_key->mac = g_strdup(key->mac);
2284         rstn_key->subscriber_id = g_strdup(key->subscriber_id);
2285         rstn_key->iftype = key->iftype;
2286         rstn_key->roaming = key->roaming;
2287
2288         g_tree_insert(g_system->rstns, rstn_key, rstn_value);
2289
2290         rstn_value->restriction_id = value->restriction_id;
2291         rstn_value->rstn_state = value->rstn_state;
2292         rstn_value->rstn_type = value->rstn_type;
2293         rstn_value->classid = value->classid;
2294
2295         int i;
2296         for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
2297                 rstn_value->limit[i] = value->limit[i];
2298                 rstn_value->counter[i] = 0;
2299         }
2300
2301         rstn_value->limit_exceeded = 0;
2302         rstn_value->limit_notified = 0;
2303         rstn_value->month_start_date = value->month_start_date;
2304         rstn_value->month_start_ts = value->month_start_ts;
2305
2306         if (debug == TRUE)
2307                 __add_restriction_debug(key, rstn_value, NULL);
2308         else
2309                 __add_restriction(key, rstn_value, NULL);
2310
2311         return STC_ERROR_NONE;
2312 }
2313
2314 //LCOV_EXCL_START
2315 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
2316                                             void *user_data)
2317 {
2318         stc_cb_ret_e ret = STC_CONTINUE;
2319
2320         stc_rstn_key_s key;
2321         stc_rstn_value_s value;
2322
2323         memset(&key, 0, sizeof(stc_rstn_key_s));
2324         memset(&value, 0, sizeof(stc_rstn_value_s));
2325
2326         key.app_id = g_strdup(info->app_id);
2327         key.ifname = g_strdup(info->ifname);
2328         key.subscriber_id = g_strdup(info->subscriber_id);
2329         key.iftype = info->iftype;
2330         key.roaming = info->roaming;
2331
2332         value.rstn_type = info->rstn_type;
2333         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2334         value.restriction_id = info->restriction_id;
2335
2336         if (info->app_id)
2337                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2338         else
2339                 value.classid = STC_UNKNOWN_CLASSID;
2340
2341         value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2342         value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2343         value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2344         value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2345         value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2346
2347         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
2348                 ret = STC_CANCEL;
2349
2350         FREE(key.app_id);
2351         FREE(key.ifname);
2352         FREE(key.subscriber_id);
2353         return ret;
2354 }
2355
2356 static void __fill_restritions_list(void)
2357 {
2358         table_restrictions_foreach(__insert_restriction_cb, NULL);
2359
2360         /* __rstn_tree_printall(); */
2361 }
2362
2363 static gboolean __add_rstn_foreach_application(gpointer key,
2364                                                gpointer value,
2365                                                gpointer data)
2366 {
2367         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
2368         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
2369         gchar *app_id = (gchar *)data;
2370
2371         /* rstn rule is not for applications */
2372         if (rstn_key->app_id == NULL)
2373                 goto out;
2374
2375         /* rstn rule is not for this application */
2376         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
2377                 goto out;
2378
2379         /* rstn rule is already applied */
2380         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
2381                 goto out;
2382
2383         /* add restriction to system */
2384         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
2385                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
2386                 __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
2387         } else {
2388                 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
2389                 __process_tethering_restriction(RST_SET, rstn_key, rstn_value, NULL);
2390         }
2391
2392         __print_rstn(rstn_key, rstn_value);
2393 out:
2394         return FALSE;
2395 }
2396 //LCOV_EXCL_STOP
2397
2398 static void __add_rstns_for_application(gchar *app_id)
2399 {
2400         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
2401                        app_id);
2402 }
2403
2404 static void __add_application_by_interface(const char *app_id)
2405 {
2406         stc_app_key_s app_key;
2407         stc_app_value_s app_value;
2408
2409         if (app_id == NULL)
2410                 return; //LCOV_EXCL_LINE
2411
2412         memset(&app_key, 0, sizeof(stc_app_key_s));
2413         memset(&app_value, 0, sizeof(stc_app_value_s));
2414
2415         app_key.pkg_id = g_strdup(app_id);
2416         app_key.app_id = g_strdup(app_id);
2417
2418         app_value.type = STC_APP_TYPE_NONE;
2419         app_value.processes = NULL;
2420         app_value.counter.in_bytes = 0;
2421         app_value.counter.out_bytes = 0;
2422
2423         stc_monitor_application_add(app_key, app_value);
2424
2425         FREE(app_key.pkg_id);
2426         FREE(app_key.app_id);
2427 }
2428
2429 static guint __get_background_state(void)
2430 {
2431         return g_system->background_state;;
2432 }
2433
2434 static void __set_background_state(guint state)
2435 {
2436         g_system->background_state = state;
2437 }
2438
2439 static gboolean __processes_tree_foreach_background(gpointer key,
2440                                                     gpointer value,
2441                                                     gpointer data)
2442 {
2443         stc_process_key_s *proc_key = (stc_process_key_s *)key;
2444         stc_app_key_s *app_key = (stc_app_key_s *)data;
2445
2446         if (g_system->background_state)
2447                 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
2448         else
2449                 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
2450
2451         return FALSE;
2452 }
2453
2454 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
2455                                         gpointer data)
2456 {
2457         stc_app_key_s *app_key = (stc_app_key_s *)key;
2458         stc_app_value_s *app_value = (stc_app_value_s *)value;
2459
2460         if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
2461                 g_tree_foreach(app_value->processes,
2462                                __processes_tree_foreach_background, app_key);
2463
2464         return FALSE;
2465 }
2466
2467 static stc_error_e __process_update_background(void)
2468 {
2469         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2470
2471         g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
2472
2473         return STC_ERROR_NONE;
2474 }
2475 //LCOV_EXCL_STOP
2476
2477 static void __fill_exceptions_list(void)
2478 {
2479         stc_plugin_fill_exception_list();
2480 }
2481
2482 stc_error_e stc_monitor_init(void)
2483 {
2484         stc_system_s *system = MALLOC0(stc_system_s, 1);
2485         GIOChannel *gio = NULL;
2486
2487         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
2488
2489         /* initializing current classid */
2490         init_current_classid();
2491
2492         /* initializing cgroups */
2493         cgroup_init();
2494
2495         /* creating monitored application tree */
2496         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
2497                                        __apps_tree_key_free,
2498                                        __apps_tree_value_free);
2499
2500         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
2501                                         __rstns_tree_key_free,
2502                                         __rstns_tree_value_free);
2503
2504         /* create netlink socket for updating kernel counters */
2505         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
2506         if (system->contr_sock < 0) {
2507                 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
2508                 FREE(system); //LCOV_EXCL_LINE
2509                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2510         }
2511
2512         gio = g_io_channel_unix_new(system->contr_sock);
2513         system->contr_gsource_id =
2514                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
2515                                (GIOFunc) __process_contr_reply,
2516                                NULL);
2517         g_io_channel_unref(gio);
2518
2519         g_system = system;
2520
2521         __add_application_by_interface(STC_TOTAL_DATACALL);
2522         __add_application_by_interface(STC_TOTAL_WIFI);
2523         __add_application_by_interface(STC_TOTAL_BLUETOOTH);
2524         __add_application_by_interface(STC_TOTAL_IPV4);
2525         __add_application_by_interface(STC_TOTAL_IPV6);
2526         __add_application_by_interface(STC_TOTAL_TETHERING);
2527
2528         /* creating restriction rules tree */
2529         __update_contr_cb(NULL);
2530
2531         /* registering periodic kernel counters update callback */
2532         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
2533                                                          __update_contr_cb,
2534                                                          NULL);
2535         if (g_system->contr_timer_id == 0) {
2536                 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
2537                 __close_contr_sock(g_system); //LCOV_EXCL_LINE
2538                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2539         }
2540
2541         __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
2542                         (int *)&g_system->background_state);
2543
2544         __fill_exceptions_list();
2545         __fill_restritions_list();
2546
2547         return STC_ERROR_NONE;
2548 }
2549
2550 stc_error_e stc_monitor_deinit(void)
2551 {
2552         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2553
2554         /* close netlink socket for updating kernel counters */
2555         __close_contr_sock(g_system);
2556
2557         /* remove kernel counters update timer */
2558         if (g_system->contr_timer_id > 0) {
2559                 g_source_remove(g_system->contr_timer_id);
2560                 g_system->contr_timer_id = 0;
2561         }
2562
2563         /* destroy monitored application tree */
2564         g_tree_destroy(g_system->apps);
2565         g_system->apps = NULL;
2566
2567         /* destroy restriction rules tree */
2568         g_tree_destroy(g_system->rstns);
2569         g_system->rstns = NULL;
2570
2571         FREE(g_system);
2572
2573         return STC_ERROR_NONE;
2574 }
2575
2576 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
2577                                         const stc_app_value_s app_value)
2578 {
2579         stc_error_e ret = STC_ERROR_NONE;
2580         stc_app_key_s *key;
2581         stc_app_value_s *value;
2582         stc_app_value_s *lookup;
2583
2584         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2585
2586         lookup = __application_lookup(g_system->apps, &app_key);
2587         if (lookup)
2588                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2589
2590         key = MALLOC0(stc_app_key_s, 1);
2591         if (!key) {
2592                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2593                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2594         }
2595
2596         value = MALLOC0(stc_app_value_s, 1);
2597         if (!value) {
2598                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2599                 FREE(key); //LCOV_EXCL_LINE
2600                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2601         }
2602
2603         key->app_id = g_strdup(app_key.app_id);
2604         key->pkg_id = g_strdup(app_key.pkg_id);
2605
2606         value->type = app_value.type;
2607         value->data_usage.in_bytes = app_value.data_usage.in_bytes;
2608         value->data_usage.out_bytes = app_value.data_usage.out_bytes;
2609         g_strlcpy(value->mac, app_value.mac, MAC_ADDRESS_LEN);
2610
2611         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
2612                                            __processes_tree_key_free,
2613                                            __processes_tree_value_free);
2614
2615         /* create cgroup and update classid */
2616         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
2617
2618         /* update classid for tethering station based on its mac address */
2619         if (g_str_has_suffix(app_key.app_id, STC_TETHERING_APP_SUFFIX) &&
2620                                 value->classid != STC_TETHERING_APP_CLASSID)
2621                 stc_plugin_tether_set_station_classid(value->mac, value->classid);
2622
2623         g_tree_insert(g_system->apps, key, value);
2624
2625         /* add nfacct rule for this classid */
2626         __add_application_monitor(key, value, stc_get_default_connection());
2627         __add_rstns_for_application(app_key.app_id);
2628
2629         return ret;
2630 }
2631
2632 API stc_error_e stc_monitor_application_remove(const stc_app_key_s app_key)
2633 {
2634         stc_error_e ret = STC_ERROR_NONE;
2635         stc_app_value_s *app_lookup;
2636
2637         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2638
2639         app_lookup = __application_lookup(g_system->apps, &app_key);
2640         if (!app_lookup) {
2641                 if (STC_DEBUG_LOG)
2642                         STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2643                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2644         }
2645
2646         /* remove nfacct rule for this classid */
2647         __remove_application_monitor((gpointer) &app_key, app_lookup,
2648                                      stc_get_default_connection());
2649
2650         /* remove ristrictions if any */
2651         __remove_rstns_for_application(app_key.app_id);
2652
2653         /* remove app_key from the stc-manager */
2654         if (!g_tree_remove(g_system->apps, &app_key)) {
2655                 ret = STC_ERROR_NO_DATA;
2656                 STC_LOGE("key not found");
2657         }
2658
2659         return ret;
2660 }
2661
2662 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
2663                                     const stc_process_key_s proc_key,
2664                                     const stc_process_value_s proc_value)
2665 {
2666         stc_error_e ret = STC_ERROR_NONE;
2667         stc_app_value_s *app_lookup;
2668         stc_process_key_s *key;
2669         stc_process_value_s *value;
2670         stc_process_value_s *proc_lookup;
2671
2672         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2673
2674         app_lookup = __application_lookup(g_system->apps, &app_key);
2675         if (!app_lookup) {
2676                 if (STC_DEBUG_LOG)
2677                         STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2678                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2679         }
2680
2681         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2682         if (proc_lookup)
2683                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2684
2685         key = MALLOC0(stc_process_key_s, 1);
2686         if (!key) {
2687                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2688                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2689         }
2690
2691         value = MALLOC0(stc_process_value_s, 1);
2692         if (!value) {
2693                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2694                 FREE(key); //LCOV_EXCL_LINE
2695                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2696         }
2697
2698         key->pid = proc_key.pid;
2699
2700         value->ground = proc_value.ground;
2701
2702         g_tree_insert(app_lookup->processes, key, value);
2703
2704         /* add pid to application cgroup */
2705         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2706
2707         /*
2708         __apps_tree_printall(); //LCOV_EXCL_LINE
2709         */
2710
2711         return ret;
2712 }
2713
2714 API stc_error_e stc_monitor_process_remove(pid_t pid)
2715 {
2716         stc_error_e ret = STC_ERROR_NONE;
2717         stc_process_key_s proc_key = {
2718                 .pid = pid
2719         };
2720
2721         remove_pid_context_s context = {
2722                 .app_key = NULL,
2723                 .proc_key = &proc_key,
2724                 .entry_removed = FALSE,
2725         };
2726
2727         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2728
2729         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
2730                        &context);
2731
2732         if (context.entry_removed)
2733                 __application_remove_if_empty(context.app_key);
2734
2735         /*
2736         __apps_tree_printall(); //LCOV_EXCL_LINE
2737         */
2738
2739         return ret;
2740 }
2741
2742 //LCOV_EXCL_START
2743 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
2744                                               const stc_process_key_s proc_key,
2745                                               stc_app_state_e ground)
2746 {
2747         stc_error_e ret = STC_ERROR_NONE;
2748         stc_app_value_s *app_lookup;
2749         stc_process_value_s *proc_lookup;
2750
2751         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2752
2753         app_lookup = __application_lookup(g_system->apps, &app_key);
2754         if (!app_lookup) {
2755                 STC_LOGD("app_key not found");
2756                 return STC_ERROR_FAIL;
2757         }
2758
2759         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2760         if (!proc_lookup) {
2761                 STC_LOGD("proc_key not found");
2762                 return STC_ERROR_FAIL;
2763         }
2764
2765         if (proc_lookup->ground != ground)
2766                 proc_lookup->ground = ground;
2767
2768         if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
2769                 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
2770         else
2771                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2772
2773         return ret;
2774 }
2775 //LCOV_EXCL_STOP
2776
2777 void stc_monitor_update_rstn_by_default_connection(void *data)
2778 {
2779         static default_connection_s old_connection;
2780         default_connection_s *new_connection = (default_connection_s *)data;
2781
2782         if (old_connection.path != NULL) {
2783                 //LCOV_EXCL_START
2784                 if (g_system->apps)
2785                         g_tree_foreach(g_system->apps,
2786                                        __remove_application_monitor,
2787                                        (gpointer)&old_connection);
2788
2789                 if (g_system->rstns)
2790                         g_tree_foreach(g_system->rstns,
2791                                        __remove_restriction,
2792                                        (gpointer)&old_connection);
2793
2794                 iptables_flush_chains();
2795                 //LCOV_EXCL_STOP
2796         }
2797
2798         FREE(old_connection.path);
2799         FREE(old_connection.ifname);
2800         FREE(old_connection.tether_iface.ifname);
2801         old_connection.type = 0;
2802         old_connection.roaming = 0;
2803         old_connection.tether_state = FALSE;
2804         old_connection.tether_iface.type = 0;
2805
2806         if (new_connection != NULL && new_connection->path != NULL) {
2807                 if (g_system->apps)
2808                         g_tree_foreach(g_system->apps,
2809                                        __add_application_monitor,
2810                                        (gpointer)new_connection);
2811
2812                 if (g_system->rstns)
2813                         g_tree_foreach(g_system->rstns, __add_restriction,
2814                                        NULL);
2815
2816                 old_connection.path = g_strdup(new_connection->path);
2817                 old_connection.ifname = g_strdup(new_connection->ifname);
2818                 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
2819                 old_connection.type = new_connection->type;
2820                 old_connection.roaming = new_connection->roaming;
2821                 old_connection.tether_state = new_connection->tether_state;
2822                 old_connection.tether_iface.type = new_connection->tether_iface.type;
2823         }
2824 }
2825
2826 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
2827 {
2828         stc_error_e ret;
2829
2830         stc_rstn_key_s key;
2831         stc_rstn_value_s value;
2832
2833         memset(&key, 0, sizeof(stc_rstn_key_s));
2834         memset(&value, 0, sizeof(stc_rstn_value_s));
2835
2836         key.app_id = g_strdup(info->app_id);
2837         key.ifname = g_strdup(info->ifname);
2838         key.mac = g_strdup(info->mac);
2839         key.subscriber_id = g_strdup(info->subscriber_id);
2840         key.iftype = info->iftype;
2841         key.roaming = info->roaming;
2842
2843         value.rstn_type = info->rstn_type;
2844         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2845         value.restriction_id = info->restriction_id;
2846
2847         if (info->app_id)
2848                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2849         else
2850                 value.classid = STC_UNKNOWN_CLASSID;
2851
2852         if (value.classid == STC_BACKGROUND_APP_CLASSID) {
2853                 __set_background_state(TRUE); //LCOV_EXCL_LINE
2854                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2855                 __process_update_background(); //LCOV_EXCL_LINE
2856         }
2857
2858         value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2859         value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2860         value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2861         value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2862         value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2863         value.month_start_date = info->month_start_date;
2864         value.month_start_ts = stc_time_get_month_start(time(NULL),
2865                                                                                 info->month_start_date);
2866
2867         ret = __rstn_tree_add(&key, &value, TRUE);
2868
2869         FREE(key.app_id);
2870         FREE(key.ifname);
2871         FREE(key.mac);
2872         FREE(key.subscriber_id);
2873         return ret;
2874 }
2875
2876 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2877 {
2878         stc_error_e ret;
2879
2880         stc_rstn_key_s key = {
2881                 .app_id = g_strdup(info->app_id),
2882                 .ifname = g_strdup(info->ifname),
2883                 .subscriber_id = g_strdup(info->subscriber_id),
2884                 .iftype = info->iftype,
2885                 .roaming = info->roaming,
2886         };
2887
2888         if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2889                 __set_background_state(FALSE); //LCOV_EXCL_LINE
2890                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2891                 __process_update_background(); //LCOV_EXCL_LINE
2892         }
2893
2894         ret = __rstn_tree_remove(&key);
2895
2896         FREE(key.app_id);
2897         FREE(key.ifname);
2898         FREE(key.subscriber_id);
2899         return ret;
2900 }
2901
2902 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2903 {
2904         return stc_plugin_check_exception_by_cmdline(cmdline);
2905 }
2906
2907 int stc_monitor_get_counter_socket(void)
2908 {
2909         return g_system->contr_sock;
2910 }