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"
27 #include "stc-manager-plugin.h"
29 #define MAX_INT_LENGTH 128
32 stc_app_key_s *app_key;
33 stc_process_key_s *proc_key;
34 gboolean entry_removed;
35 } remove_pid_context_s;
38 struct nfacct_rule *counter;
40 gboolean in_limit_reached;
41 gboolean out_limit_reached;
42 } classid_bytes_context_s;
44 static stc_system_s *g_system = NULL;
46 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
48 if (counter->intend == NFACCT_WARN)
49 return NFACCT_JUMP_ACCEPT;
50 else if (counter->intend == NFACCT_BLOCK)
51 return NFACCT_JUMP_REJECT;
53 return NFACCT_JUMP_UNKNOWN;
56 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
58 return produce_net_rule(counter, 0, 0,
59 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
63 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
65 return produce_net_rule(counter, 0, 0,
66 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
70 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
72 return produce_net_rule(counter, 0, 0,
73 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
77 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
79 return produce_net_rule(counter, 0, 0,
80 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
84 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
85 gpointer UNUSED user_data)
87 stc_process_key_s *key_a = (stc_process_key_s *)a;
88 stc_process_key_s *key_b = (stc_process_key_s *)b;
90 return key_a->pid - key_b->pid;
93 static void __processes_tree_value_free(gpointer data)
95 stc_process_value_s *value = (stc_process_value_s *)data;
100 static void __processes_tree_key_free(gpointer data)
102 stc_process_key_s *key = (stc_process_key_s *)data;
107 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
108 gpointer UNUSED user_data)
110 stc_app_key_s *key_a = (stc_app_key_s *)a;
111 stc_app_key_s *key_b = (stc_app_key_s *)b;
114 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
118 return g_strcmp0(key_a->app_id, key_b->app_id);
121 static void __apps_tree_value_free(gpointer data)
123 stc_app_value_s *value = (stc_app_value_s *)data;
125 g_tree_destroy(value->processes);
126 value->processes = NULL;
131 static void __apps_tree_key_free(gpointer data)
133 stc_app_key_s *key = (stc_app_key_s *)data;
140 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
141 gpointer UNUSED user_data)
143 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
144 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
147 ret = g_strcmp0(key_a->app_id, key_b->app_id);
151 ret = g_strcmp0(key_a->ifname, key_b->ifname);
155 ret = g_strcmp0(key_a->imsi, key_b->imsi);
159 ret = key_a->iftype - key_b->iftype;
166 static void __rstns_tree_value_free(gpointer data)
168 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
173 static void __rstns_tree_key_free(gpointer data)
175 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
183 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
186 stc_process_key_s *proc_key = (stc_process_key_s *)key;
187 stc_process_value_s *proc_value = (stc_process_value_s *)value;
189 STC_LOGD("Process entry => PID [%d], Ground state [%d]",
190 proc_key->pid, proc_value->ground);
194 static void __processes_tree_printall(GTree *processes)
196 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
199 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
202 stc_app_key_s *app_key = (stc_app_key_s *)key;
203 stc_app_value_s *app_value = (stc_app_value_s *)value;
205 STC_LOGD("Application info => Pkg ID [%s], App ID [%s],"
206 " Type [%d], classid [%d],"
207 " counter [ in (%lld), out (%lld)]",
208 app_key->pkg_id, app_key->app_id,
209 app_value->type, app_value->classid,
210 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
212 __processes_tree_printall(app_value->processes);
216 static void __apps_tree_printall(void)
218 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
221 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
224 remove_pid_context_s *context = (remove_pid_context_s *)data;
225 stc_app_value_s *app_value = (stc_app_value_s *)value;
227 if (!g_tree_remove(app_value->processes, context->proc_key)) {
228 STC_LOGD("key not found");
232 context->entry_removed = TRUE;
233 context->app_key = (stc_app_key_s *)key;
238 static stc_app_value_s * __application_lookup(GTree *apps,
239 const stc_app_key_s *key)
241 stc_app_value_s *lookup;
243 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
245 lookup = g_tree_lookup(apps, key);
250 static stc_process_value_s * __process_lookup(GTree *processes,
251 const stc_process_key_s *key)
253 stc_process_value_s *lookup;
255 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
257 lookup = g_tree_lookup(processes, key);
262 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
265 guint *pid_count = (guint *)data;
270 static gboolean __add_application_monitor(gpointer key, gpointer value,
273 stc_app_value_s *app_value = (stc_app_value_s *)value;
274 default_connection_s *connection = (default_connection_s *)data;
275 stc_s *stc = stc_get_manager();
277 if (stc && connection && connection->ifname) {
278 struct nfacct_rule counter;
281 stc->carg = MALLOC0(counter_arg_s, 1);
282 stc->carg->sock = stc_monitor_get_counter_socket();
285 memset(&counter, 0, sizeof(struct nfacct_rule));
287 counter.carg = stc->carg;
288 counter.classid = app_value->classid;
289 counter.intend = NFACCT_COUNTER;
290 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
292 __add_iptables_in(&counter);
293 __add_iptables_out(&counter);
299 static gboolean __remove_application_monitor(gpointer key, gpointer value,
302 stc_app_value_s *app_value = (stc_app_value_s *)value;
303 default_connection_s *connection = (default_connection_s *)data;
304 stc_s *stc = stc_get_manager();
306 if (stc && connection && connection->ifname) {
307 struct nfacct_rule counter;
310 stc->carg = MALLOC0(counter_arg_s, 1);
311 stc->carg->sock = stc_monitor_get_counter_socket();
314 memset(&counter, 0, sizeof(struct nfacct_rule));
316 counter.carg = stc->carg;
317 counter.classid = app_value->classid;
318 counter.intend = NFACCT_COUNTER;
319 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
321 __del_iptables_in(&counter);
322 __del_iptables_out(&counter);
328 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
330 STC_LOGI("rstn info => rstn_id [%llu], "
331 "app_id [%s], classid [%lu], ifname [%s], "
332 "iftype [%d], rst_state [%d], "
333 "limit [ in (%lld), out (%lld)], "
334 "warn_limit [ in (%lld), out (%lld)], "
335 "counter [ in (%lld), out (%lld)], "
336 "roaming [%d], imsi [%s]",
337 rstn_value->restriction_id,
338 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
339 rstn_key->iftype, rstn_value->rst_state,
340 rstn_value->limit.in_bytes, rstn_value->limit.out_bytes,
341 rstn_value->warn_limit.in_bytes,
342 rstn_value->warn_limit.out_bytes,
343 rstn_value->counter.in_bytes, rstn_value->counter.out_bytes,
344 rstn_key->roaming, rstn_key->imsi);
347 static void __process_restriction(enum traffic_restriction_type rst_type,
348 stc_rstn_key_s *rstn_key,
349 stc_rstn_value_s *rstn_value, void *data)
351 stc_data_counter_s effective_limit, effective_warn_limit;
352 default_connection_s *old_connection = (default_connection_s *)data;
353 default_connection_s *connection = NULL;
355 if (old_connection != NULL)
356 connection = old_connection;
358 connection = stc_get_default_connection();
360 /* no default ifname */
361 if (connection->ifname == NULL)
364 /* rstn not applicable for this interface */
365 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
366 g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
369 /* classid is invalid */
370 if (rstn_value->classid == STC_UNKNOWN_CLASSID)
373 effective_limit.out_bytes = rstn_value->limit.out_bytes;
374 effective_limit.in_bytes = rstn_value->limit.in_bytes;
375 effective_warn_limit.out_bytes = rstn_value->warn_limit.out_bytes;
376 effective_warn_limit.in_bytes = rstn_value->warn_limit.in_bytes;
378 if (rst_type == RST_SET) {
379 /* TODO: Change this to runtime memory */
380 table_counters_info info;
382 memset(&info, 0, sizeof(table_counters_info));
383 table_counters_get(rstn_value->restriction_id, &info);
385 effective_limit.out_bytes -= info.sent_bytes;
386 effective_limit.in_bytes -= info.rcv_bytes;
387 effective_warn_limit.out_bytes -= info.sent_bytes;
388 effective_warn_limit.in_bytes -= info.rcv_bytes;
390 if (effective_limit.in_bytes < 0) {
391 effective_limit.in_bytes = 0;
392 rstn_value->in_limit_reached = TRUE;
395 if (effective_limit.out_bytes < 0) {
396 effective_limit.out_bytes = 0;
397 rstn_value->out_limit_reached = TRUE;
400 if (effective_warn_limit.in_bytes < 0)
401 effective_warn_limit.in_bytes = 0;
403 if (effective_warn_limit.out_bytes < 0)
404 effective_warn_limit.out_bytes = 0;
405 STC_LOGD("datausage [in: %lld, out: %lld]",
406 info.rcv_bytes, info.sent_bytes);
409 STC_LOGD("rstn_id [%llu], effective limit [in: %lld, out: %lld], "
410 "effective warn limit [in: %lld, out: %lld], "
411 "datausage [in: %lld, out: %lld]",
412 rstn_value->restriction_id,
413 effective_limit.in_bytes, effective_limit.out_bytes,
414 effective_warn_limit.in_bytes, effective_warn_limit.out_bytes);
416 send_net_restriction(rst_type,
420 effective_limit.out_bytes,
421 effective_limit.in_bytes,
422 effective_warn_limit.out_bytes,
423 effective_warn_limit.in_bytes,
427 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
428 rstn_value->in_limit_reached = FALSE;
429 rstn_value->out_limit_reached = FALSE;
435 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
436 rstn_value->in_limit_reached = FALSE;
437 rstn_value->out_limit_reached = FALSE;
444 static gboolean __remove_rstns_foreach_application(gpointer key,
448 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
449 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
450 gchar *app_id = (gchar *)data;
452 /* rstn rule is not for applications */
453 if (rstn_key->app_id == NULL)
456 /* rstn rule is not for this application */
457 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
460 /* rstn rule is already removed */
461 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
464 /* remove restriction from system */
465 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
467 __print_rstn(rstn_key, rstn_value);
472 static void __remove_rstns_for_application(gchar *app_id)
474 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
478 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
480 stc_error_e ret = STC_ERROR_NONE;
482 stc_app_value_s *lookup;
484 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
486 lookup = __application_lookup(g_system->apps, app_key);
488 STC_LOGE("app_key not found");
489 return STC_ERROR_NO_DATA;
492 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
496 /* remove nfacct rule for this classid */
497 __remove_application_monitor((gpointer) app_key, lookup,
498 stc_get_default_connection());
499 __remove_rstns_for_application(app_key->app_id);
502 if (!g_tree_remove(g_system->apps, app_key)) {
503 ret = STC_ERROR_NO_DATA;
504 STC_LOGE("key not found");
510 static stc_error_e __close_contr_sock(stc_system_s *system)
512 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
514 /* close netlink socket for updating kernel counters */
515 if (g_system->contr_sock != -1) {
516 close(g_system->contr_sock);
517 g_system->contr_sock = -1;
520 if (g_system->contr_gsource_id != 0) {
521 g_source_remove(g_system->contr_gsource_id);
522 g_system->contr_gsource_id = 0;
525 return STC_ERROR_NONE;
528 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
532 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
533 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
534 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
536 if (context->counter->intend != NFACCT_COUNTER)
537 goto try_next_callback;
539 if (rstn_value->classid != context->counter->classid)
540 goto try_next_callback;
542 if (rstn_value->in_limit_reached == TRUE) {
543 context->in_limit_reached = TRUE;
544 goto try_next_callback;
547 if (rstn_value->out_limit_reached == TRUE) {
548 context->out_limit_reached = TRUE;
549 goto try_next_callback;
552 switch (context->counter->iotype) {
553 case NFACCT_COUNTER_IN:
554 rstn_value->counter.in_bytes += context->bytes;
556 if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes
557 && rstn_value->warn_limit_crossed_notified == FALSE) {
559 char iftype[MAX_INT_LENGTH];
560 char byte[MAX_INT_LENGTH];
561 stc_s *stc = (stc_s *)stc_get_manager();
562 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
564 rv = stc_manager_dbus_emit_signal(stc->connection,
565 STC_DBUS_SERVICE_RESTRICTION_PATH,
566 STC_DBUS_INTERFACE_RESTRICTION,
567 "WarnThresholdCrossed",
568 g_variant_new("(s)", rstn_key->app_id));
570 rstn_value->warn_limit_crossed_notified = TRUE;
572 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
573 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes);
574 stc_send_warn_message_to_net_popup("warn threshold crossed",
575 "warning_noti", rstn_key->app_id, iftype, byte);
578 /* block immediately */
579 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
580 __del_iptables_in(context->counter);
581 __add_iptables_in(context->counter);
582 rstn_value->in_limit_reached = TRUE;
584 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
587 char iftype[MAX_INT_LENGTH];
588 char byte[MAX_INT_LENGTH];
589 stc_s *stc = (stc_s *)stc_get_manager();
590 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
592 rv = stc_manager_dbus_emit_signal(stc->connection,
593 STC_DBUS_SERVICE_RESTRICTION_PATH,
594 STC_DBUS_INTERFACE_RESTRICTION,
595 "RestrictionThresholdCrossed",
596 g_variant_new("(s)", rstn_key->app_id));
598 rstn_value->rstn_limit_crossed_notified = TRUE;
600 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
601 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes);
602 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
603 "restriction_noti", rstn_key->app_id, iftype, byte);
607 g_system->rstns_tree_updated = TRUE;
608 __print_rstn(rstn_key, rstn_value);
610 case NFACCT_COUNTER_OUT:
611 rstn_value->counter.out_bytes += context->bytes;
613 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes
614 && rstn_value->warn_limit_crossed_notified == FALSE) {
616 char iftype[MAX_INT_LENGTH];
617 char byte[MAX_INT_LENGTH];
618 stc_s *stc = (stc_s *)stc_get_manager();
619 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
621 rv = stc_manager_dbus_emit_signal(stc->connection,
622 STC_DBUS_SERVICE_RESTRICTION_PATH,
623 STC_DBUS_INTERFACE_RESTRICTION,
624 "WarnThresholdCrossed",
625 g_variant_new("(s)", rstn_key->app_id));
627 rstn_value->warn_limit_crossed_notified = TRUE;
629 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
630 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes);
631 stc_send_warn_message_to_net_popup("warn threshold crossed",
632 "warning_noti", rstn_key->app_id, iftype, byte);
635 /* block immediately */
636 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
637 __del_iptables_out(context->counter);
638 __add_iptables_out(context->counter);
639 rstn_value->out_limit_reached = TRUE;
641 if (rstn_value->rstn_limit_crossed_notified == FALSE) {
643 char iftype[MAX_INT_LENGTH];
644 char byte[MAX_INT_LENGTH];
645 stc_s *stc = (stc_s *)stc_get_manager();
646 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
648 rv = stc_manager_dbus_emit_signal(stc->connection,
649 STC_DBUS_SERVICE_RESTRICTION_PATH,
650 STC_DBUS_INTERFACE_RESTRICTION,
651 "RestrictionThresholdCrossed",
652 g_variant_new("(s)", rstn_key->app_id));
654 rstn_value->rstn_limit_crossed_notified = TRUE;
656 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
657 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes);
658 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
659 "restriction_noti", rstn_key->app_id, iftype, byte);
663 g_system->rstns_tree_updated = TRUE;
664 __print_rstn(rstn_key, rstn_value);
667 STC_LOGE("unknown iotype");
675 static gboolean __update_app_statistics(gpointer key, gpointer value,
678 stc_app_key_s *app_key = (stc_app_key_s *)key;
679 stc_app_value_s *app_value = (stc_app_value_s *)value;
680 time_t *touch_time = (time_t *)data;
681 stc_db_classid_iftype_key stat_key;
682 stc_db_app_stats stat;
683 char *default_ifname = stc_default_connection_get_ifname();
685 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
686 memset(&stat, 0 , sizeof(stc_db_app_stats));
688 stat_key.classid = app_value->classid;
689 stat_key.iftype = stc_default_connection_get_type();
690 if (STC_IFACE_DATACALL == stat_key.iftype)
691 stat_key.imsi = g_strdup("unknown");
693 stat_key.imsi = g_strdup("noneimsi");
694 g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
696 stat.app_id = g_strdup(app_key->app_id);
697 stat.snd_count = app_value->counter.out_bytes;
698 stat.rcv_count = app_value->counter.in_bytes;
701 stat.is_roaming = stc_default_connection_get_roaming();
702 stat.ground = STC_APP_STATE_UNKNOWN;
704 table_statistics_insert(&stat_key, &stat, *touch_time);
706 app_value->counter.out_bytes = 0;
707 app_value->counter.in_bytes = 0;
711 FREE(default_ifname);
716 static gboolean __flush_apps_stats_to_database(gpointer user_data)
718 time_t current_time = time(0);
720 if (g_system->apps_tree_updated == FALSE)
721 return G_SOURCE_REMOVE;
723 g_system->apps_tree_updated = FALSE;
726 g_tree_foreach(g_system->apps,
727 __update_app_statistics,
730 STC_LOGI("Flushed app stats to database");
731 return G_SOURCE_REMOVE;
734 static gboolean __update_counter_statistics(gpointer key, gpointer value,
737 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
738 table_counters_info info = {
739 .restriction_id = rstn_value->restriction_id,
740 .sent_bytes = rstn_value->counter.out_bytes,
741 .rcv_bytes = rstn_value->counter.in_bytes
744 table_counters_update_counters(&info);
749 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
751 time_t current_time = time(0);
753 if (g_system->rstns_tree_updated == FALSE)
754 return G_SOURCE_REMOVE;
756 g_system->rstns_tree_updated = FALSE;
759 g_tree_foreach(g_system->rstns,
760 __update_counter_statistics,
763 STC_LOGI("Flushed rstns counters to database");
764 return G_SOURCE_REMOVE;
767 static gboolean __apps_counter_update_foreach_classid(gpointer key,
771 stc_app_key_s *app_key = (stc_app_key_s *)key;
772 stc_app_value_s *app_value = (stc_app_value_s *)value;
773 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
775 if (context->counter->intend != NFACCT_COUNTER)
776 goto try_next_callback;
778 if (app_value->classid != context->counter->classid)
779 goto try_next_callback;
781 switch (context->counter->iotype) {
782 case NFACCT_COUNTER_IN:
783 app_value->data_usage.in_bytes += context->bytes;
784 app_value->counter.in_bytes = context->bytes;
785 g_system->apps_tree_updated = TRUE;
787 __apps_tree_foreach_print(app_key, app_value, NULL);
789 case NFACCT_COUNTER_OUT:
790 app_value->data_usage.out_bytes += context->bytes;
791 app_value->counter.out_bytes = context->bytes;
792 g_system->apps_tree_updated = TRUE;
794 __apps_tree_foreach_print(app_key, app_value, NULL);
797 STC_LOGE("unknown iotype");
804 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
805 struct counter_arg *carg)
807 struct nfacct_rule counter = {
814 classid_bytes_context_s context = {
817 .in_limit_reached = FALSE,
818 .out_limit_reached = FALSE
821 STC_LOGD("cnt_name %s", cnt_name);
823 if (!recreate_counter_by_name(cnt_name, &counter)) {
824 STC_LOGE("Can't parse counter name %s", cnt_name);
828 STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
829 context.counter->classid, context.counter->iftype,
830 context.counter->iotype, context.counter->intend,
831 context.counter->ifname, context.bytes);
834 g_tree_foreach(g_system->rstns,
835 __rstn_counter_update_foreach_classid,
839 g_tree_foreach(g_system->apps,
840 __apps_counter_update_foreach_classid,
844 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
847 struct counter_arg *carg = user_data;
848 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
849 if (carg->initiate) {
851 * TODO: this will be used when daemon starts to update existing
852 * counter data if present.
854 populate_counters(cnt_name, carg);
858 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
859 int bytes = be64toh(*bytes_p);
861 ++carg->serialized_counters;
862 __fill_nfacct_result(cnt_name, bytes, carg);
869 static int __post_fill_counters(void *user_data)
871 struct counter_arg *carg = user_data;
879 static void __process_network_counter(struct genl *ans,
880 struct counter_arg *carg)
882 struct netlink_serialization_params ser_params = {
885 .eval_attr = __fill_counters,
886 .post_eval_attr = __post_fill_counters,
889 netlink_serialization_command *netlink =
890 netlink_create_command(&ser_params);
892 STC_LOGE("Can not create command");
896 netlink->deserialize_answer(&(netlink->params));
899 static gboolean __process_contr_reply(GIOChannel *source,
900 GIOCondition condition,
903 int sock = g_io_channel_unix_get_fd(source);
906 stc_s *stc = stc_get_manager();
908 if ((condition & G_IO_ERR) ||
909 (condition & G_IO_HUP) ||
910 (condition & G_IO_NVAL)) {
911 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
913 __close_contr_sock(g_system);
918 STC_LOGE("Can't get stc data");
922 ret = read_netlink(sock,
923 &ans, sizeof(struct genl));
924 STC_LOGD("Counter data received ret [%d]", ret);
928 stc->carg->ans_len = ret;
929 __process_network_counter(&ans, stc->carg);
931 g_idle_add(__flush_apps_stats_to_database, NULL);
932 g_idle_add(__flush_rstns_counter_to_database, NULL);
937 static gboolean __update_contr_cb(void *user_data)
939 /* Here we just sent command, answer we receive in another callback */
940 stc_s *stc = stc_get_manager();
941 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
943 stc->carg = MALLOC0(counter_arg_s, 1);
944 stc->carg->sock = stc_monitor_get_counter_socket();
947 STC_LOGD("Get all counters");
948 nfacct_send_get_all(stc->carg);
950 /* we need to continue the timer */
954 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
957 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
958 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
960 __print_rstn(rstn_key, rstn_value);
964 static void __rstn_tree_printall(void)
966 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
969 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
970 const stc_rstn_key_s *key)
972 stc_rstn_value_s *lookup;
974 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
976 lookup = g_tree_lookup(rstns_tree, key);
981 static gboolean __remove_restriction(gpointer key, gpointer value,
984 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
985 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
987 /* rstn rule is already removed */
988 if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
991 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
992 __print_rstn(rstn_key, rstn_value);
996 static gboolean __add_restriction_debug(gpointer key, gpointer value,
999 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1000 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1002 /* rstn rule is activated */
1003 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1006 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1007 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1009 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1011 __print_rstn(rstn_key, rstn_value);
1016 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1018 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1019 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1021 /* rstn rule is activated */
1022 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1025 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1026 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1028 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1033 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1035 stc_rstn_value_s *lookup_value;
1037 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1039 lookup_value = __rstn_lookup(g_system->rstns, key);
1040 if (!lookup_value) {
1041 STC_LOGE("key not found");
1042 return STC_ERROR_NO_DATA;
1045 __remove_restriction(key, lookup_value, NULL);
1047 /* remove counter also */
1048 table_counters_delete(lookup_value->restriction_id);
1050 if (!g_tree_remove(g_system->rstns, key)) {
1051 STC_LOGD("key not found");
1052 return STC_ERROR_NO_DATA;
1055 return STC_ERROR_NONE;
1058 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1059 stc_rstn_value_s *value, gboolean debug)
1061 stc_rstn_value_s *rstn_value;
1063 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1065 rstn_value = __rstn_lookup(g_system->rstns, key);
1067 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1069 STC_LOGE("rstn_key allocation failed");
1070 return STC_ERROR_OUT_OF_MEMORY;
1073 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1075 STC_LOGE("rstn_value allocation failed");
1077 return STC_ERROR_OUT_OF_MEMORY;
1080 rstn_key->app_id = g_strdup(key->app_id);
1081 rstn_key->ifname = g_strdup(key->ifname);
1082 rstn_key->imsi = g_strdup(key->imsi);
1083 rstn_key->iftype = key->iftype;
1084 rstn_key->roaming = key->roaming;
1086 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1089 rstn_value->restriction_id = value->restriction_id;
1090 rstn_value->rst_state = value->rst_state;
1091 rstn_value->classid = value->classid;
1092 rstn_value->limit.in_bytes = value->limit.in_bytes;
1093 rstn_value->limit.out_bytes = value->limit.out_bytes;
1094 rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1095 rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1096 rstn_value->counter.in_bytes = 0;
1097 rstn_value->counter.out_bytes = 0;
1098 rstn_value->warn_limit_crossed_notified = FALSE;
1099 rstn_value->rstn_limit_crossed_notified = FALSE;
1102 __add_restriction_debug(key, rstn_value, NULL);
1104 __add_restriction(key, rstn_value, NULL);
1106 return STC_ERROR_NONE;
1109 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1112 stc_cb_ret_e ret = STC_CONTINUE;
1115 stc_rstn_value_s value;
1117 memset(&key, 0, sizeof(stc_rstn_key_s));
1118 memset(&value, 0, sizeof(stc_rstn_value_s));
1120 key.app_id = g_strdup(info->app_id);
1121 key.ifname = g_strdup(info->ifname);
1122 key.imsi = g_strdup(info->imsi);
1123 key.iftype = info->iftype;
1124 key.roaming = info->roaming;
1126 value.rst_state = info->rst_state;
1127 value.restriction_id = info->restriction_id;
1129 if (value.rst_state != STC_RESTRICTION_EXCLUDED)
1130 value.classid = get_classid_by_app_id(info->app_id ?
1134 value.classid = STC_UNKNOWN_CLASSID;
1136 value.limit.in_bytes = info->rcv_limit;
1137 value.limit.out_bytes = info->send_limit;
1138 value.warn_limit.in_bytes = info->rcv_warn_limit;
1139 value.warn_limit.out_bytes = info->send_warn_limit;
1141 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1150 static void __fill_restritions_list(void)
1152 table_restrictions_foreach(__insert_restriction_cb, NULL);
1153 //__rstn_tree_printall();
1156 static gboolean __add_rstn_foreach_application(gpointer key,
1160 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1161 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1162 gchar *app_id = (gchar *)data;
1164 /* rstn rule is not for applications */
1165 if (rstn_key->app_id == NULL)
1168 /* rstn rule is not for this application */
1169 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1172 /* rstn rule is already applied */
1173 if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1176 /* add restriction to system */
1177 if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1178 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1180 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1182 __print_rstn(rstn_key, rstn_value);
1187 static void __add_rstns_for_application(gchar *app_id)
1189 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1193 /* used only for (STC_ALL_APP/STC_BACKGROUND_APP_NAME) */
1194 static void __stc_monitor_add_application_by_app_id(const char *app_id)
1196 stc_app_key_s app_key;
1197 stc_app_value_s app_value;
1202 memset(&app_key, 0, sizeof(stc_app_key_s));
1203 memset(&app_value, 0, sizeof(stc_app_value_s));
1205 app_key.pkg_id = g_strdup(app_id);
1206 app_key.app_id = g_strdup(app_id);
1208 app_value.type = STC_APP_TYPE_NONE;
1209 app_value.processes = NULL;
1210 app_value.counter.in_bytes = 0;
1211 app_value.counter.out_bytes = 0;
1213 stc_monitor_application_add(app_key, app_value);
1215 FREE(app_key.pkg_id);
1216 FREE(app_key.app_id);
1218 __apps_tree_printall();
1221 stc_error_e stc_monitor_init(void)
1223 stc_system_s *system = MALLOC0(stc_system_s, 1);
1224 GIOChannel *gio = NULL;
1226 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1228 /* creating monitored application tree */
1229 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1230 __apps_tree_key_free,
1231 __apps_tree_value_free);
1233 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1234 __rstns_tree_key_free,
1235 __rstns_tree_value_free);
1237 /* create netlink socket for updating kernel counters */
1238 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1239 if (!(system->contr_sock)) {
1240 STC_LOGE("failed to open socket");
1242 return STC_ERROR_FAIL;
1245 gio = g_io_channel_unix_new(system->contr_sock);
1246 system->contr_gsource_id =
1247 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1248 (GIOFunc) __process_contr_reply,
1250 g_io_channel_unref(gio);
1254 /* create entry for STC_ALL_APP */
1255 __stc_monitor_add_application_by_app_id(STC_ALL_APP);
1257 /* create entry for STC_BACKGROUND_APP_NAME */
1258 __stc_monitor_add_application_by_app_id(STC_BACKGROUND_APP_NAME);
1260 /* create background cgroup */
1261 g_system->background_pids = NULL;
1262 create_net_background_cgroup(g_system->background_pids);
1264 /* creating restriction rules tree */
1265 __update_contr_cb(NULL);
1267 /* registering periodic kernel counters update callback */
1268 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1271 if (g_system->contr_timer_id == 0) {
1272 STC_LOGE("Failed to register kernel counters update timer");
1273 __close_contr_sock(g_system);
1274 return STC_ERROR_FAIL;
1277 __fill_restritions_list();
1279 return STC_ERROR_NONE;
1282 stc_error_e stc_monitor_deinit(void)
1284 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1286 /* close netlink socket for updating kernel counters */
1287 __close_contr_sock(g_system);
1289 /* remove kernel counters update timer */
1290 if (g_system->contr_timer_id > 0) {
1291 g_source_remove(g_system->contr_timer_id);
1292 g_system->contr_timer_id = 0;
1295 /* free background pid list */
1296 g_slist_free(g_system->background_pids);
1297 g_system->background_pids = NULL;
1299 /* destroy monitored application tree */
1300 g_tree_destroy(g_system->apps);
1301 g_system->apps = NULL;
1303 /* destroy restriction rules tree */
1304 g_tree_destroy(g_system->rstns);
1305 g_system->rstns = NULL;
1309 return STC_ERROR_NONE;
1312 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1313 const stc_app_value_s app_value)
1315 stc_error_e ret = STC_ERROR_NONE;
1317 stc_app_value_s *value;
1318 stc_app_value_s *lookup;
1320 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1322 lookup = __application_lookup(g_system->apps, &app_key);
1324 STC_LOGD("app_key already present");
1325 return STC_ERROR_NONE;
1328 key = MALLOC0(stc_app_key_s, 1);
1330 STC_LOGE("key allocation failed");
1331 return STC_ERROR_OUT_OF_MEMORY;
1334 value = MALLOC0(stc_app_value_s, 1);
1336 STC_LOGE("value allocation failed");
1338 return STC_ERROR_OUT_OF_MEMORY;
1341 key->app_id = g_strdup(app_key.app_id);
1342 key->pkg_id = g_strdup(app_key.pkg_id);
1344 value->type = app_value.type;
1345 value->counter.in_bytes = app_value.counter.in_bytes;
1346 value->counter.out_bytes = app_value.counter.out_bytes;
1348 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1349 __processes_tree_key_free,
1350 __processes_tree_value_free);
1352 /* create cgroup and update classid */
1353 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1355 g_tree_insert(g_system->apps, key, value);
1357 /* add nfacct rule for this classid */
1358 __add_application_monitor(key, value, stc_get_default_connection());
1359 __add_rstns_for_application(app_key.app_id);
1364 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1365 const stc_process_key_s proc_key,
1366 const stc_process_value_s proc_value)
1368 stc_error_e ret = STC_ERROR_NONE;
1369 stc_app_value_s *app_lookup;
1370 stc_process_key_s *key;
1371 stc_process_value_s *value;
1372 stc_process_value_s *proc_lookup;
1374 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1376 app_lookup = __application_lookup(g_system->apps, &app_key);
1378 STC_LOGD("app_key not found");
1379 return STC_ERROR_FAIL;
1382 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1384 STC_LOGD("proc_key already present");
1385 return STC_ERROR_NONE;
1388 key = MALLOC0(stc_process_key_s, 1);
1390 STC_LOGE("key allocation failed");
1391 return STC_ERROR_OUT_OF_MEMORY;
1394 value = MALLOC0(stc_process_value_s, 1);
1396 STC_LOGE("value allocation failed");
1398 return STC_ERROR_OUT_OF_MEMORY;
1401 key->pid = proc_key.pid;
1403 value->ground = proc_value.ground;
1405 g_tree_insert(app_lookup->processes, key, value);
1407 /* add pid to application cgroup */
1408 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1410 /* add pid to background cgroup if gound state is background */
1411 if (proc_value.ground == STC_APP_STATE_BACKGROUND)
1412 add_pid_to_background_cgroup(proc_key.pid);
1417 stc_error_e stc_monitor_process_remove(pid_t pid)
1419 stc_error_e ret = STC_ERROR_NONE;
1420 stc_process_key_s proc_key = {
1424 remove_pid_context_s context = {
1426 .proc_key = &proc_key,
1427 .entry_removed = FALSE,
1430 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1432 /* remove pid from background cgroup */
1433 remove_pid_from_background_cgroup(pid);
1435 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1438 if (context.entry_removed)
1439 __application_remove_if_empty(context.app_key);
1444 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1445 const stc_process_key_s proc_key,
1446 stc_app_state_e ground)
1448 stc_error_e ret = STC_ERROR_NONE;
1449 stc_app_value_s *app_lookup;
1450 stc_process_value_s *proc_lookup;
1452 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1454 app_lookup = __application_lookup(g_system->apps, &app_key);
1456 STC_LOGD("app_key not found");
1457 return STC_ERROR_FAIL;
1460 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1462 STC_LOGD("proc_key not found");
1463 return STC_ERROR_FAIL;
1466 if (proc_lookup->ground != ground)
1467 proc_lookup->ground = ground;
1469 if (ground == STC_APP_STATE_BACKGROUND)
1470 add_pid_to_background_cgroup(proc_key.pid);
1472 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1477 void stc_monitor_update_rstn_by_default_connection(void *data)
1479 static default_connection_s old_connection;
1480 default_connection_s *new_connection = (default_connection_s *)data;
1482 if (old_connection.path != NULL) {
1484 g_tree_foreach(g_system->apps,
1485 __remove_application_monitor,
1486 (gpointer)&old_connection);
1488 if (g_system->rstns)
1489 g_tree_foreach(g_system->rstns,
1490 __remove_restriction,
1491 (gpointer)&old_connection);
1494 FREE(old_connection.path);
1495 FREE(old_connection.ifname);
1496 old_connection.type = 0;
1497 old_connection.roaming = 0;
1499 if (new_connection != NULL && new_connection->path != NULL) {
1501 g_tree_foreach(g_system->apps,
1502 __add_application_monitor,
1503 (gpointer)new_connection);
1505 if (g_system->rstns)
1506 g_tree_foreach(g_system->rstns, __add_restriction,
1509 old_connection.path = g_strdup(new_connection->path);
1510 old_connection.ifname = g_strdup(new_connection->ifname);
1511 old_connection.type = new_connection->type;
1512 old_connection.roaming = new_connection->roaming;
1516 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1521 stc_rstn_value_s value;
1523 memset(&key, 0, sizeof(stc_rstn_key_s));
1524 memset(&value, 0, sizeof(stc_rstn_value_s));
1526 key.app_id = g_strdup(info->app_id);
1527 key.ifname = g_strdup(info->ifname);
1528 key.imsi = g_strdup(info->imsi);
1529 key.iftype = info->iftype;
1530 key.roaming = info->roaming;
1532 value.rst_state = info->rst_state;
1533 value.restriction_id = info->restriction_id;
1535 if (value.rst_state != STC_RESTRICTION_EXCLUDED)
1536 value.classid = get_classid_by_app_id(info->app_id ?
1540 value.classid = STC_UNKNOWN_CLASSID;
1542 value.limit.in_bytes = info->rcv_limit;
1543 value.limit.out_bytes = info->send_limit;
1544 value.warn_limit.in_bytes = info->rcv_warn_limit;
1545 value.warn_limit.out_bytes = info->send_warn_limit;
1547 ret = __rstn_tree_add(&key, &value, TRUE);
1555 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1559 stc_rstn_key_s key = {
1560 .app_id = g_strdup(info->app_id),
1561 .ifname = g_strdup(info->ifname),
1562 .imsi = g_strdup(info->imsi),
1563 .iftype = info->iftype,
1564 .roaming = info->roaming,
1567 ret = __rstn_tree_remove(&key);
1575 int stc_monitor_get_counter_socket(void)
1577 return g_system->contr_sock;