2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <linux/netlink.h>
19 #include "stc-default-connection.h"
20 #include "helper-nl.h"
21 #include "helper-nfacct-rule.h"
22 #include "helper-net-cls.h"
24 #include "table-statistics.h"
25 #include "table-counters.h"
26 #include "stc-monitor.h"
29 stc_app_key_s *app_key;
30 stc_process_key_s *proc_key;
31 gboolean entry_removed;
32 } remove_pid_context_s;
35 struct nfacct_rule *counter;
37 gboolean in_limit_reached;
38 gboolean out_limit_reached;
39 } classid_bytes_context_s;
41 static stc_system_s *g_system = NULL;
43 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
45 if (counter->intend == NFACCT_WARN)
46 return NFACCT_JUMP_ACCEPT;
47 else if (counter->intend == NFACCT_BLOCK)
48 return NFACCT_JUMP_REJECT;
50 return NFACCT_JUMP_UNKNOWN;
53 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
55 return produce_net_rule(counter, 0, 0,
56 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
60 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
62 return produce_net_rule(counter, 0, 0,
63 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
67 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
69 return produce_net_rule(counter, 0, 0,
70 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
74 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
76 return produce_net_rule(counter, 0, 0,
77 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
81 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
82 gpointer UNUSED user_data)
84 stc_process_key_s *key_a = (stc_process_key_s *)a;
85 stc_process_key_s *key_b = (stc_process_key_s *)b;
87 return key_a->pid - key_b->pid;
90 static void __processes_tree_value_free(gpointer data)
92 stc_process_value_s *value = (stc_process_value_s *)data;
97 static void __processes_tree_key_free(gpointer data)
99 stc_process_key_s *key = (stc_process_key_s *)data;
104 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
105 gpointer UNUSED user_data)
107 stc_app_key_s *key_a = (stc_app_key_s *)a;
108 stc_app_key_s *key_b = (stc_app_key_s *)b;
111 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
115 return g_strcmp0(key_a->app_id, key_b->app_id);
118 static void __apps_tree_value_free(gpointer data)
120 stc_app_value_s *value = (stc_app_value_s *)data;
122 g_tree_destroy(value->processes);
123 value->processes = NULL;
128 static void __apps_tree_key_free(gpointer data)
130 stc_app_key_s *key = (stc_app_key_s *)data;
137 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
138 gpointer UNUSED user_data)
140 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
141 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
144 ret = g_strcmp0(key_a->app_id, key_b->app_id);
148 ret = g_strcmp0(key_a->ifname, key_b->ifname);
152 ret = g_strcmp0(key_a->imsi, key_b->imsi);
156 ret = key_a->iftype - key_b->iftype;
163 static void __rstns_tree_value_free(gpointer data)
165 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
170 static void __rstns_tree_key_free(gpointer data)
172 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
180 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
183 stc_process_key_s *proc_key = (stc_process_key_s *)key;
184 stc_process_value_s *proc_value = (stc_process_value_s *)value;
186 STC_LOGD("Process entry => PID [%d], Ground state [%d]",
187 proc_key->pid, proc_value->ground);
191 static void __processes_tree_printall(GTree *processes)
193 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
196 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
199 stc_app_key_s *app_key = (stc_app_key_s *)key;
200 stc_app_value_s *app_value = (stc_app_value_s *)value;
202 STC_LOGD("Application info => Pkg ID [%s], App ID [%s],"
203 " Type [%d], classid [%d],"
204 " counter [ in (%lld), out (%lld)]",
205 app_key->pkg_id, app_key->app_id,
206 app_value->type, app_value->classid,
207 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
209 __processes_tree_printall(app_value->processes);
213 static void __apps_tree_printall(void)
215 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
218 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
221 remove_pid_context_s *context = (remove_pid_context_s *)data;
222 stc_app_value_s *app_value = (stc_app_value_s *)value;
224 if (!g_tree_remove(app_value->processes, context->proc_key)) {
225 STC_LOGD("key not found");
229 context->entry_removed = TRUE;
230 context->app_key = (stc_app_key_s *)key;
235 static stc_app_value_s * __application_lookup(GTree *apps,
236 const stc_app_key_s *key)
238 stc_app_value_s *lookup;
240 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
242 lookup = g_tree_lookup(apps, key);
247 static stc_process_value_s * __process_lookup(GTree *processes,
248 const stc_process_key_s *key)
250 stc_process_value_s *lookup;
252 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
254 lookup = g_tree_lookup(processes, key);
259 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
262 guint *pid_count = (guint *)data;
267 static gboolean __add_application_monitor(gpointer key, gpointer value,
270 stc_app_value_s *app_value = (stc_app_value_s *)value;
271 default_connection_s *connection = (default_connection_s *)data;
272 stc_s *stc = stc_get_manager();
274 if (stc && connection && connection->ifname) {
275 struct nfacct_rule counter;
278 stc->carg = MALLOC0(counter_arg_s, 1);
279 stc->carg->sock = stc_monitor_get_counter_socket();
282 memset(&counter, 0, sizeof(struct nfacct_rule));
284 counter.carg = stc->carg;
285 counter.classid = app_value->classid;
286 counter.intend = NFACCT_COUNTER;
287 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
289 __add_iptables_in(&counter);
290 __add_iptables_out(&counter);
296 static gboolean __remove_application_monitor(gpointer key, gpointer value,
299 stc_app_value_s *app_value = (stc_app_value_s *)value;
300 default_connection_s *connection = (default_connection_s *)data;
301 stc_s *stc = stc_get_manager();
303 if (stc && connection && connection->ifname) {
304 struct nfacct_rule counter;
307 stc->carg = MALLOC0(counter_arg_s, 1);
308 stc->carg->sock = stc_monitor_get_counter_socket();
311 memset(&counter, 0, sizeof(struct nfacct_rule));
313 counter.carg = stc->carg;
314 counter.classid = app_value->classid;
315 counter.intend = NFACCT_COUNTER;
316 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
318 __del_iptables_in(&counter);
319 __del_iptables_out(&counter);
325 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
327 STC_LOGI("rstn info => rstn_id [%llu], "
328 "app_id [%s], classid [%lu], ifname [%s], "
329 "iftype [%d], rst_state [%d], "
330 "limit [ in (%lld), out (%lld)], "
331 "warn_limit [ in (%lld), out (%lld)], "
332 "counter [ in (%lld), out (%lld)], "
333 "roaming [%d], imsi [%s]",
334 rstn_value->restriction_id,
335 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
336 rstn_key->iftype, rstn_value->rst_state,
337 rstn_value->limit.in_bytes, rstn_value->limit.out_bytes,
338 rstn_value->warn_limit.in_bytes,
339 rstn_value->warn_limit.out_bytes,
340 rstn_value->counter.in_bytes, rstn_value->counter.out_bytes,
341 rstn_key->roaming, rstn_key->imsi);
344 static void __process_restriction(enum traffic_restriction_type rst_type,
345 stc_rstn_key_s *rstn_key,
346 stc_rstn_value_s *rstn_value, void *data)
348 stc_data_counter_s effective_limit, effective_warn_limit;
349 default_connection_s *old_connection = (default_connection_s *)data;
350 default_connection_s *connection = NULL;
352 if (old_connection != NULL)
353 connection = old_connection;
355 connection = stc_get_default_connection();
357 /* no default ifname */
358 if (connection->ifname == NULL)
361 /* rstn not applicable for this interface */
362 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
363 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
366 /* classid is invalid */
367 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
370 effective_limit.out_bytes = rstn_value->limit.out_bytes;
371 effective_limit.in_bytes = rstn_value->limit.in_bytes;
372 effective_warn_limit.out_bytes = rstn_value->warn_limit.out_bytes;
373 effective_warn_limit.in_bytes = rstn_value->warn_limit.in_bytes;
375 if (rst_type == RST_SET) {
376 /* TODO: Change this to runtime memory */
377 table_counters_info info;
379 memset(&info, 0, sizeof(table_counters_info));
380 table_counters_get(rstn_value->restriction_id, &info);
382 effective_limit.out_bytes -= info.sent_bytes;
383 effective_limit.in_bytes -= info.rcv_bytes;
384 effective_warn_limit.out_bytes -= info.sent_bytes;
385 effective_warn_limit.in_bytes -= info.rcv_bytes;
387 if (effective_limit.in_bytes < 0) {
388 effective_limit.in_bytes = 0;
389 rstn_value->in_limit_reached = TRUE;
392 if (effective_limit.out_bytes < 0) {
393 effective_limit.out_bytes = 0;
394 rstn_value->out_limit_reached = TRUE;
397 if (effective_warn_limit.in_bytes < 0)
398 effective_warn_limit.in_bytes = 0;
400 if (effective_warn_limit.out_bytes < 0)
401 effective_warn_limit.out_bytes = 0;
402 STC_LOGD("datausage [in: %lld, out: %lld]",
403 info.rcv_bytes, info.sent_bytes);
406 STC_LOGD("rstn_id [%llu], effective limit [in: %lld, out: %lld], "
407 "effective warn limit [in: %lld, out: %lld], "
408 "datausage [in: %lld, out: %lld]",
409 rstn_value->restriction_id,
410 effective_limit.in_bytes, effective_limit.out_bytes,
411 effective_warn_limit.in_bytes, effective_warn_limit.out_bytes);
413 send_net_restriction(rst_type,
417 effective_limit.out_bytes,
418 effective_limit.in_bytes,
419 effective_warn_limit.out_bytes,
420 effective_warn_limit.in_bytes,
424 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
425 rstn_value->in_limit_reached = FALSE;
426 rstn_value->out_limit_reached = FALSE;
432 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
433 rstn_value->in_limit_reached = FALSE;
434 rstn_value->out_limit_reached = FALSE;
441 static gboolean __remove_rstns_foreach_application(gpointer key,
445 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
446 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
447 gchar *app_id = (gchar *)data;
449 /* rstn rule is not for applications */
450 if (rstn_key->app_id == NULL)
453 /* rstn rule is not for this application */
454 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
457 /* rstn rule is already removed */
458 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
461 /* remove restriction from system */
462 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
464 __print_rstn(rstn_key, rstn_value);
469 static void __remove_rstns_for_application(gchar *app_id)
471 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
475 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
477 stc_error_e ret = STC_ERROR_NONE;
479 stc_app_value_s *lookup;
481 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
483 lookup = __application_lookup(g_system->apps, app_key);
485 STC_LOGE("app_key not found");
486 return STC_ERROR_NO_DATA;
489 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
493 /* remove nfacct rule for this classid */
494 __remove_application_monitor((gpointer) app_key, lookup,
495 stc_get_default_connection());
496 __remove_rstns_for_application(app_key->app_id);
499 if (!g_tree_remove(g_system->apps, app_key)) {
500 ret = STC_ERROR_NO_DATA;
501 STC_LOGE("key not found");
507 static stc_error_e __close_contr_sock(stc_system_s *system)
509 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
511 /* close netlink socket for updating kernel counters */
512 if (g_system->contr_sock != -1) {
513 close(g_system->contr_sock);
514 g_system->contr_sock = -1;
517 if (g_system->contr_gsource_id != 0) {
518 g_source_remove(g_system->contr_gsource_id);
519 g_system->contr_gsource_id = 0;
522 return STC_ERROR_NONE;
525 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
529 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
530 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
531 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
533 if (context->counter->intend != NFACCT_COUNTER)
534 goto try_next_callback;
536 if (rstn_value->classid != context->counter->classid)
537 goto try_next_callback;
539 if (rstn_value->in_limit_reached == TRUE) {
540 context->in_limit_reached = TRUE;
541 goto try_next_callback;
544 if (rstn_value->out_limit_reached == TRUE) {
545 context->out_limit_reached = TRUE;
546 goto try_next_callback;
549 switch (context->counter->iotype) {
550 case NFACCT_COUNTER_IN:
551 rstn_value->counter.in_bytes += context->bytes;
553 /* block immediately */
554 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
555 __del_iptables_in(context->counter);
556 __add_iptables_in(context->counter);
557 rstn_value->in_limit_reached = TRUE;
558 //context->in_limit_reached = TRUE;
561 g_system->rstns_tree_updated = TRUE;
562 __print_rstn(rstn_key, rstn_value);
564 case NFACCT_COUNTER_OUT:
565 rstn_value->counter.out_bytes += context->bytes;
567 /* block immediately */
568 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
569 __del_iptables_out(context->counter);
570 __add_iptables_out(context->counter);
571 rstn_value->out_limit_reached = TRUE;
572 //context->out_limit_reached = TRUE;
575 g_system->rstns_tree_updated = TRUE;
576 __print_rstn(rstn_key, rstn_value);
579 STC_LOGE("unknown iotype");
587 static gboolean __update_app_statistics(gpointer key, gpointer value,
590 stc_app_key_s *app_key = (stc_app_key_s *)key;
591 stc_app_value_s *app_value = (stc_app_value_s *)value;
592 time_t *touch_time = (time_t *)data;
593 stc_db_classid_iftype_key stat_key;
594 stc_db_app_stats stat;
595 char *default_ifname = stc_default_connection_get_ifname();
597 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
598 memset(&stat, 0 , sizeof(stc_db_app_stats));
600 stat_key.classid = app_value->classid;
601 stat_key.iftype = stc_default_connection_get_type();
602 if (STC_IFACE_DATACALL == stat_key.iftype)
603 stat_key.imsi = g_strdup("unknown");
605 stat_key.imsi = g_strdup("noneimsi");
606 g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
608 stat.app_id = g_strdup(app_key->app_id);
609 stat.snd_count = app_value->counter.out_bytes;
610 stat.rcv_count = app_value->counter.in_bytes;
613 stat.is_roaming = stc_default_connection_get_roaming();
614 stat.ground = STC_APP_STATE_UNKNOWN;
616 table_statistics_insert(&stat_key, &stat, *touch_time);
618 app_value->counter.out_bytes = 0;
619 app_value->counter.in_bytes = 0;
623 FREE(default_ifname);
628 static gboolean __flush_apps_stats_to_database(gpointer user_data)
630 time_t current_time = time(0);
632 if (g_system->apps_tree_updated == FALSE)
633 return G_SOURCE_REMOVE;
635 g_system->apps_tree_updated = FALSE;
638 g_tree_foreach(g_system->apps,
639 __update_app_statistics,
642 STC_LOGI("Flushed app stats to database");
643 return G_SOURCE_REMOVE;
646 static gboolean __update_counter_statistics(gpointer key, gpointer value,
649 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
650 table_counters_info info = {
651 .restriction_id = rstn_value->restriction_id,
652 .sent_bytes = rstn_value->counter.out_bytes,
653 .rcv_bytes = rstn_value->counter.in_bytes
656 table_counters_update_counters(&info);
661 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
663 time_t current_time = time(0);
665 if (g_system->rstns_tree_updated == FALSE)
666 return G_SOURCE_REMOVE;
668 g_system->rstns_tree_updated = FALSE;
671 g_tree_foreach(g_system->rstns,
672 __update_counter_statistics,
675 STC_LOGI("Flushed rstns counters to database");
676 return G_SOURCE_REMOVE;
679 static gboolean __apps_counter_update_foreach_classid(gpointer key,
683 stc_app_key_s *app_key = (stc_app_key_s *)key;
684 stc_app_value_s *app_value = (stc_app_value_s *)value;
685 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
687 if (context->counter->intend != NFACCT_COUNTER)
688 goto try_next_callback;
690 if (app_value->classid != context->counter->classid)
691 goto try_next_callback;
693 switch (context->counter->iotype) {
694 case NFACCT_COUNTER_IN:
695 app_value->data_usage.in_bytes += context->bytes;
696 app_value->counter.in_bytes = context->bytes;
697 g_system->apps_tree_updated = TRUE;
699 __apps_tree_foreach_print(app_key, app_value, NULL);
701 case NFACCT_COUNTER_OUT:
702 app_value->data_usage.out_bytes += context->bytes;
703 app_value->counter.out_bytes = context->bytes;
704 g_system->apps_tree_updated = TRUE;
706 __apps_tree_foreach_print(app_key, app_value, NULL);
709 STC_LOGE("unknown iotype");
716 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
717 struct counter_arg *carg)
719 struct nfacct_rule counter = {
726 classid_bytes_context_s context = {
729 .in_limit_reached = FALSE,
730 .out_limit_reached = FALSE
733 STC_LOGD("cnt_name %s", cnt_name);
735 if (!recreate_counter_by_name(cnt_name, &counter)) {
736 STC_LOGE("Can't parse counter name %s", cnt_name);
740 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
741 context.counter->classid, context.counter->iftype,
742 context.counter->iotype, context.counter->intend,
743 context.counter->ifname, context.bytes);
746 g_tree_foreach(g_system->rstns,
747 __rstn_counter_update_foreach_classid,
751 g_tree_foreach(g_system->apps,
752 __apps_counter_update_foreach_classid,
756 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
759 struct counter_arg *carg = user_data;
760 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
761 if (carg->initiate) {
763 * TODO: this will be used when daemon starts to update existing
764 * counter data if present.
766 populate_counters(cnt_name, carg);
770 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
771 int bytes = be64toh(*bytes_p);
773 ++carg->serialized_counters;
774 __fill_nfacct_result(cnt_name, bytes, carg);
781 static int __post_fill_counters(void *user_data)
783 struct counter_arg *carg = user_data;
791 static void __process_network_counter(struct genl *ans,
792 struct counter_arg *carg)
794 struct netlink_serialization_params ser_params = {
797 .eval_attr = __fill_counters,
798 .post_eval_attr = __post_fill_counters,
801 netlink_serialization_command *netlink =
802 netlink_create_command(&ser_params);
804 STC_LOGE("Can not create command");
808 netlink->deserialize_answer(&(netlink->params));
811 static gboolean __process_contr_reply(GIOChannel *source,
812 GIOCondition condition,
815 int sock = g_io_channel_unix_get_fd(source);
818 stc_s *stc = stc_get_manager();
820 if ((condition & G_IO_ERR) ||
821 (condition & G_IO_HUP) ||
822 (condition & G_IO_NVAL)) {
823 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
825 __close_contr_sock(g_system);
830 STC_LOGE("Can't get stc data");
834 ret = read_netlink(sock,
835 &ans, sizeof(struct genl));
836 STC_LOGD("Counter data received ret [%d]", ret);
840 stc->carg->ans_len = ret;
841 __process_network_counter(&ans, stc->carg);
843 g_idle_add(__flush_apps_stats_to_database, NULL);
844 g_idle_add(__flush_rstns_counter_to_database, NULL);
849 static gboolean __update_contr_cb(void *user_data)
851 /* Here we just sent command, answer we receive in another callback */
852 stc_s *stc = stc_get_manager();
853 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
855 stc->carg = MALLOC0(counter_arg_s, 1);
856 stc->carg->sock = stc_monitor_get_counter_socket();
859 STC_LOGD("Get all counters");
860 nfacct_send_get_all(stc->carg);
862 /* we need to continue the timer */
866 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
869 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
870 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
872 __print_rstn(rstn_key, rstn_value);
876 static void __rstn_tree_printall(void)
878 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
881 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
882 const stc_rstn_key_s *key)
884 stc_rstn_value_s *lookup;
886 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
888 lookup = g_tree_lookup(rstns_tree, key);
893 static gboolean __remove_restriction(gpointer key, gpointer value,
896 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
897 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
899 /* rstn rule is already removed */
900 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
903 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
904 __print_rstn(rstn_key, rstn_value);
908 static gboolean __add_restriction_debug(gpointer key, gpointer value,
911 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
912 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
914 /* rstn rule is activated */
915 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
918 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
919 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
921 __process_restriction(RST_SET, rstn_key, rstn_value, data);
923 __print_rstn(rstn_key, rstn_value);
928 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
930 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
931 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
933 /* rstn rule is activated */
934 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
937 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
938 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
940 __process_restriction(RST_SET, rstn_key, rstn_value, data);
945 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
947 stc_rstn_value_s *lookup_value;
949 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
951 lookup_value = __rstn_lookup(g_system->rstns, key);
953 STC_LOGE("key not found");
954 return STC_ERROR_NO_DATA;
957 __remove_restriction(key, lookup_value, NULL);
959 /* remove counter also */
960 table_counters_delete(lookup_value->restriction_id);
962 if (!g_tree_remove(g_system->rstns, key)) {
963 STC_LOGD("key not found");
964 return STC_ERROR_NO_DATA;
967 return STC_ERROR_NONE;
970 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
971 stc_rstn_value_s *value, gboolean debug)
973 stc_rstn_value_s *rstn_value;
975 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
977 rstn_value = __rstn_lookup(g_system->rstns, key);
979 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
981 STC_LOGE("rstn_key allocation failed");
982 return STC_ERROR_OUT_OF_MEMORY;
985 rstn_value = MALLOC0(stc_rstn_value_s, 1);
987 STC_LOGE("rstn_value allocation failed");
989 return STC_ERROR_OUT_OF_MEMORY;
992 rstn_key->app_id = g_strdup(key->app_id);
993 rstn_key->ifname = g_strdup(key->ifname);
994 rstn_key->imsi = g_strdup(key->imsi);
995 rstn_key->iftype = key->iftype;
996 rstn_key->roaming = key->roaming;
998 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1001 rstn_value->restriction_id = value->restriction_id;
1002 rstn_value->rst_state = value->rst_state;
1003 rstn_value->classid = value->classid;
1004 rstn_value->limit.in_bytes = value->limit.in_bytes;
1005 rstn_value->limit.out_bytes = value->limit.out_bytes;
1006 rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1007 rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1008 rstn_value->counter.in_bytes = 0;
1009 rstn_value->counter.out_bytes = 0;
1012 __add_restriction_debug(key, rstn_value, NULL);
1014 __add_restriction(key, rstn_value, NULL);
1016 return STC_ERROR_NONE;
1019 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1022 stc_cb_ret_e ret = STC_CONTINUE;
1025 stc_rstn_value_s value;
1027 memset(&key, 0, sizeof(stc_rstn_key_s));
1028 memset(&value, 0, sizeof(stc_rstn_value_s));
1030 key.app_id = g_strdup(info->app_id);
1031 key.ifname = g_strdup(info->ifname);
1032 key.imsi = g_strdup(info->imsi);
1033 key.iftype = info->iftype;
1034 key.roaming = info->roaming;
1036 value.rst_state = info->rst_state;
1037 value.restriction_id = info->restriction_id;
1039 if (value.rst_state != STC_RESTRICTION_EXCLUDED)
1040 value.classid = get_classid_by_app_id(info->app_id ?
1044 value.classid = STC_UNKNOWN_CLASSID;
1046 value.limit.in_bytes = info->rcv_limit;
1047 value.limit.out_bytes = info->send_limit;
1048 value.warn_limit.in_bytes = info->rcv_warn_limit;
1049 value.warn_limit.out_bytes = info->send_warn_limit;
1051 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1060 static void __fill_restritions_list(void)
1062 table_restrictions_foreach(__insert_restriction_cb, NULL);
1063 //__rstn_tree_printall();
1066 static gboolean __add_rstn_foreach_application(gpointer key,
1070 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1071 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1072 gchar *app_id = (gchar *)data;
1074 /* rstn rule is not for applications */
1075 if (rstn_key->app_id == NULL)
1078 /* rstn rule is not for this application */
1079 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1082 /* rstn rule is already applied */
1083 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1086 /* add restriction to system */
1087 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1088 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1090 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1092 __print_rstn(rstn_key, rstn_value);
1097 static void __add_rstns_for_application(gchar *app_id)
1099 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1103 /* used only for (STC_ALL_APP/STC_BACKGROUND_APP_NAME) */
1104 static void __stc_monitor_add_application_by_app_id(const char *app_id)
1106 stc_app_key_s app_key;
1107 stc_app_value_s app_value;
1112 memset(&app_key, 0, sizeof(stc_app_key_s));
1113 memset(&app_value, 0, sizeof(stc_app_value_s));
1115 app_key.pkg_id = g_strdup(app_id);
1116 app_key.app_id = g_strdup(app_id);
1118 app_value.type = STC_APP_TYPE_NONE;
1119 app_value.processes = NULL;
1120 app_value.counter.in_bytes = 0;
1121 app_value.counter.out_bytes = 0;
1123 stc_monitor_application_add(app_key, app_value);
1125 FREE(app_key.pkg_id);
1126 FREE(app_key.app_id);
1128 __apps_tree_printall();
1131 stc_error_e stc_monitor_init(void)
1133 stc_system_s *system = MALLOC0(stc_system_s, 1);
1134 GIOChannel *gio = NULL;
1136 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1138 /* creating monitored application tree */
1139 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1140 __apps_tree_key_free,
1141 __apps_tree_value_free);
1143 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1144 __rstns_tree_key_free,
1145 __rstns_tree_value_free);
1147 /* create netlink socket for updating kernel counters */
1148 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1149 if (!(system->contr_sock)) {
1150 STC_LOGE("failed to open socket");
1152 return STC_ERROR_FAIL;
1155 gio = g_io_channel_unix_new(system->contr_sock);
1156 system->contr_gsource_id =
1157 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1158 (GIOFunc) __process_contr_reply,
1160 g_io_channel_unref(gio);
1164 /* create entry for STC_ALL_APP */
1165 __stc_monitor_add_application_by_app_id(STC_ALL_APP);
1167 /* create entry for STC_BACKGROUND_APP_NAME */
1168 __stc_monitor_add_application_by_app_id(STC_BACKGROUND_APP_NAME);
1170 /* create background cgroup */
1171 g_system->background_pids = NULL;
1172 create_net_background_cgroup(g_system->background_pids);
1174 /* creating restriction rules tree */
1175 __update_contr_cb(NULL);
1177 /* registering periodic kernel counters update callback */
1178 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1181 if (g_system->contr_timer_id == 0) {
1182 STC_LOGE("Failed to register kernel counters update timer");
1183 __close_contr_sock(g_system);
1184 return STC_ERROR_FAIL;
1187 __fill_restritions_list();
1189 return STC_ERROR_NONE;
1192 stc_error_e stc_monitor_deinit(void)
1194 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1196 /* close netlink socket for updating kernel counters */
1197 __close_contr_sock(g_system);
1199 /* remove kernel counters update timer */
1200 if (g_system->contr_timer_id > 0) {
1201 g_source_remove(g_system->contr_timer_id);
1202 g_system->contr_timer_id = 0;
1205 /* free background pid list */
1206 g_slist_free(g_system->background_pids);
1207 g_system->background_pids = NULL;
1209 /* destroy monitored application tree */
1210 g_tree_destroy(g_system->apps);
1211 g_system->apps = NULL;
1213 /* destroy restriction rules tree */
1214 g_tree_destroy(g_system->rstns);
1215 g_system->rstns = NULL;
1219 return STC_ERROR_NONE;
1222 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1223 const stc_app_value_s app_value)
1225 stc_error_e ret = STC_ERROR_NONE;
1227 stc_app_value_s *value;
1228 stc_app_value_s *lookup;
1230 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1232 lookup = __application_lookup(g_system->apps, &app_key);
1234 STC_LOGD("app_key already present");
1235 return STC_ERROR_NONE;
1238 key = MALLOC0(stc_app_key_s, 1);
1240 STC_LOGE("key allocation failed");
1241 return STC_ERROR_OUT_OF_MEMORY;
1244 value = MALLOC0(stc_app_value_s, 1);
1246 STC_LOGE("value allocation failed");
1248 return STC_ERROR_OUT_OF_MEMORY;
1251 key->app_id = g_strdup(app_key.app_id);
1252 key->pkg_id = g_strdup(app_key.pkg_id);
1254 value->type = app_value.type;
1255 value->counter.in_bytes = app_value.counter.in_bytes;
1256 value->counter.out_bytes = app_value.counter.out_bytes;
1258 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1259 __processes_tree_key_free,
1260 __processes_tree_value_free);
1262 /* create cgroup and update classid */
1263 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1265 g_tree_insert(g_system->apps, key, value);
1267 /* add nfacct rule for this classid */
1268 __add_application_monitor(key, value, stc_get_default_connection());
1269 __add_rstns_for_application(app_key.app_id);
1274 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1275 const stc_process_key_s proc_key,
1276 const stc_process_value_s proc_value)
1278 stc_error_e ret = STC_ERROR_NONE;
1279 stc_app_value_s *app_lookup;
1280 stc_process_key_s *key;
1281 stc_process_value_s *value;
1282 stc_process_value_s *proc_lookup;
1284 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1286 app_lookup = __application_lookup(g_system->apps, &app_key);
1288 STC_LOGD("app_key not found");
1289 return STC_ERROR_FAIL;
1292 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1294 STC_LOGD("proc_key already present");
1295 return STC_ERROR_NONE;
1298 key = MALLOC0(stc_process_key_s, 1);
1300 STC_LOGE("key allocation failed");
1301 return STC_ERROR_OUT_OF_MEMORY;
1304 value = MALLOC0(stc_process_value_s, 1);
1306 STC_LOGE("value allocation failed");
1308 return STC_ERROR_OUT_OF_MEMORY;
1311 key->pid = proc_key.pid;
1313 value->ground = proc_value.ground;
1315 g_tree_insert(app_lookup->processes, key, value);
1317 /* add pid to application cgroup */
1318 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1320 /* add pid to background cgroup if gound state is background */
1321 if (proc_value.ground == STC_APP_STATE_BACKGROUND)
1322 add_pid_to_background_cgroup(proc_key.pid);
1327 stc_error_e stc_monitor_process_remove(pid_t pid)
1329 stc_error_e ret = STC_ERROR_NONE;
1330 stc_process_key_s proc_key = {
1334 remove_pid_context_s context = {
1336 .proc_key = &proc_key,
1337 .entry_removed = FALSE,
1340 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1342 /* remove pid from background cgroup */
1343 remove_pid_from_background_cgroup(pid);
1345 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1348 if (context.entry_removed)
1349 __application_remove_if_empty(context.app_key);
1354 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1355 const stc_process_key_s proc_key,
1356 stc_app_state_e ground)
1358 stc_error_e ret = STC_ERROR_NONE;
1359 stc_app_value_s *app_lookup;
1360 stc_process_value_s *proc_lookup;
1362 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1364 app_lookup = __application_lookup(g_system->apps, &app_key);
1366 STC_LOGD("app_key not found");
1367 return STC_ERROR_FAIL;
1370 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1372 STC_LOGD("proc_key not found");
1373 return STC_ERROR_FAIL;
1376 if (proc_lookup->ground != ground)
1377 proc_lookup->ground = ground;
1379 if (ground == STC_APP_STATE_BACKGROUND)
1380 add_pid_to_background_cgroup(proc_key.pid);
1382 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1387 void stc_monitor_update_rstn_by_default_connection(void *data)
1389 static default_connection_s old_connection;
1390 default_connection_s *new_connection = (default_connection_s *)data;
1392 if (old_connection.path != NULL) {
1394 g_tree_foreach(g_system->apps,
1395 __remove_application_monitor,
1396 (gpointer)&old_connection);
1398 if (g_system->rstns)
1399 g_tree_foreach(g_system->rstns,
1400 __remove_restriction,
1401 (gpointer)&old_connection);
1404 FREE(old_connection.path);
1405 FREE(old_connection.ifname);
1406 old_connection.type = 0;
1407 old_connection.roaming = 0;
1409 if (new_connection != NULL && new_connection->path != NULL) {
1411 g_tree_foreach(g_system->apps,
1412 __add_application_monitor,
1413 (gpointer)new_connection);
1415 if (g_system->rstns)
1416 g_tree_foreach(g_system->rstns, __add_restriction,
1419 old_connection.path = g_strdup(new_connection->path);
1420 old_connection.ifname = g_strdup(new_connection->ifname);
1421 old_connection.type = new_connection->type;
1422 old_connection.roaming = new_connection->roaming;
1426 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1431 stc_rstn_value_s value;
1433 memset(&key, 0, sizeof(stc_rstn_key_s));
1434 memset(&value, 0, sizeof(stc_rstn_value_s));
1436 key.app_id = g_strdup(info->app_id);
1437 key.ifname = g_strdup(info->ifname);
1438 key.imsi = g_strdup(info->imsi);
1439 key.iftype = info->iftype;
1440 key.roaming = info->roaming;
1442 value.rst_state = info->rst_state;
1443 value.restriction_id = info->restriction_id;
1445 if (value.rst_state != STC_RESTRICTION_EXCLUDED)
1446 value.classid = get_classid_by_app_id(info->app_id ?
1450 value.classid = STC_UNKNOWN_CLASSID;
1452 value.limit.in_bytes = info->rcv_limit;
1453 value.limit.out_bytes = info->send_limit;
1454 value.warn_limit.in_bytes = info->rcv_warn_limit;
1455 value.warn_limit.out_bytes = info->send_warn_limit;
1457 ret = __rstn_tree_add(&key, &value, TRUE);
1465 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1469 stc_rstn_key_s key = {
1470 .app_id = g_strdup(info->app_id),
1471 .ifname = g_strdup(info->ifname),
1472 .imsi = g_strdup(info->imsi),
1473 .iftype = info->iftype,
1474 .roaming = info->roaming,
1477 ret = __rstn_tree_remove(&key);
1485 int stc_monitor_get_counter_socket(void)
1487 return g_system->contr_sock;