5d6074954ab545ace1035e3b99a0f9659385bfde
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-monitor.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <linux/netlink.h>
18 #include <vconf.h>
19 #include <vconf-keys.h>
20
21 #include "stc-default-connection.h"
22 #include "helper-nl.h"
23 #include "helper-nfacct-rule.h"
24 #include "helper-net-cls.h"
25 #include "helper-cgroup.h"
26 #include "helper-iptables.h"
27 #include "counter.h"
28 #include "table-statistics.h"
29 #include "table-counters.h"
30 #include "stc-monitor.h"
31 #include "stc-manager-plugin-exception.h"
32 #include "stc-manager-plugin-tether.h"
33
34 static stc_system_s *g_system = NULL;
35
36 static int __vconf_get_int(const char *key, int *value)
37 {
38         int ret = 0;
39
40         ret = vconf_get_int(key, value);
41         if (ret != VCONF_OK) {
42                 STC_LOGE("Failed to get vconfkey [%s] value", key);
43                 return -1;
44         }
45
46         return 0;
47 }
48
49 static stc_error_e __close_contr_sock(stc_system_s *system)
50 {
51         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
52
53         /* close netlink socket for updating kernel counters */
54         if (system->contr_sock != -1) {
55                 close(system->contr_sock);
56                 system->contr_sock = -1;
57         }
58
59         if (system->contr_gsource_id != 0) {
60                 g_source_remove(system->contr_gsource_id);
61                 system->contr_gsource_id = 0;
62         }
63
64         return STC_ERROR_NONE;
65 }
66
67 static gboolean __process_contr_reply(GIOChannel *source,
68                                       GIOCondition condition,
69                                       gpointer user_data);
70
71 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
72 {
73         GIOChannel *gio = NULL;
74         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
75
76         /* close netlink socket for updating kernel counters */
77         if (system->contr_sock != -1) {
78                 close(system->contr_sock);
79                 system->contr_sock = -1;
80         }
81
82         if (system->contr_gsource_id != 0) {
83                 g_source_remove(system->contr_gsource_id);
84                 system->contr_gsource_id = 0;
85         }
86
87         /* create netlink socket for updating kernel counters */
88         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
89         if (system->contr_sock < 0) {
90                 STC_LOGE("failed to open socket");
91                 FREE(system);
92                 return STC_ERROR_FAIL;
93         }
94
95         gio = g_io_channel_unix_new(system->contr_sock);
96         system->contr_gsource_id =
97                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
98                                (GIOFunc) __process_contr_reply,
99                                NULL);
100         g_io_channel_unref(gio);
101
102         return STC_ERROR_NONE;
103 }
104
105 static void __check_rstn_limit_exceeded(gpointer data,
106                                 gpointer user_data)
107 {
108         stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
109         int32_t *limit_exceeded = (int32_t *)user_data;
110
111         if (rstn_data->limit_exceeded != 0)
112                 *limit_exceeded = rstn_data->limit_exceeded;
113 }
114
115 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
116                                 struct counter_arg *carg)
117 {
118         stc_monitor_rstn_reset_time_counters_if_required();
119
120         struct nfacct_rule counter = {
121                 .carg = carg,
122                 .name = {0},
123                 .ifname = {0},
124                 0
125         };
126
127         classid_bytes_context_s context = {
128                 .counter = &counter,
129                 .bytes = bytes,
130                 .data_limit_exceeded = FALSE,
131         };
132
133         if (!recreate_counter_by_name(cnt_name, &counter)) {
134                 STC_LOGE("Can't parse counter name %s", cnt_name);
135                 return;
136         }
137
138         if (STC_DEBUG_LOG)
139                 STC_LOGI("classid[\033[1;36m%u\033[0;m] iftype[%u] "
140                         "iotype[%d] intend[%d] ifname[%s] bytes[%lld]",
141                         context.counter->classid, context.counter->iftype,
142                         context.counter->iotype, context.counter->intend,
143                         context.counter->ifname, context.bytes);
144
145         if (context.counter->intend == NFACCT_COUNTER ||
146                 context.counter->intend == NFACCT_TETH_COUNTER) {
147                 if (g_system->apps) {
148                         stc_app_value_s *lookup_app;
149                         stc_rstn_value_s *lookup_rstn;
150
151                         stc_monitor_app_update_iface_counter(&context);
152
153                         lookup_app = g_hash_table_lookup(g_system->apps,
154                                                 GUINT_TO_POINTER(context.counter->classid));
155                         if (lookup_app)
156                                 stc_monitor_app_update_counter(lookup_app, &context);
157
158                         lookup_rstn = g_hash_table_lookup(g_system->rstns,
159                                                 GUINT_TO_POINTER(context.counter->classid));
160                         if (lookup_rstn) {
161                                 int32_t limit_exceeded = 0;
162                                 g_slist_foreach(lookup_rstn->rules,
163                                         __check_rstn_limit_exceeded, &limit_exceeded);
164
165                                 if (limit_exceeded != 0)
166                                         return;
167                         }
168                 }
169
170                 if (g_system->rstns) {
171                         stc_rstn_value_s *lookup_value;
172                         uint32_t classid = context.counter->classid;
173
174                         stc_monitor_rstn_update_iface_counter(&context);
175                         context.counter->classid = classid;
176
177                         lookup_value = g_hash_table_lookup(g_system->rstns,
178                                                         GUINT_TO_POINTER(classid));
179                         if (lookup_value) {
180                                 g_slist_foreach(lookup_value->rules,
181                                         stc_monitor_rstn_update_counter,
182                                         &context);
183                         }
184                 }
185         }
186 }
187
188 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
189                            void *user_data)
190 {
191         struct counter_arg *carg = user_data;
192         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
193         if (carg->initiate) {
194                 /**
195                  * TODO: this will be used when daemon starts to update existing
196                  * counter data if present.
197                  *
198                  populate_counters(cnt_name, carg);
199                  */
200         } else {
201                 int64_t *bytes_p =
202                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
203                 int bytes = be64toh(*bytes_p);
204                 if (bytes) {
205                         ++carg->serialized_counters;
206                         __fill_nfacct_result(cnt_name, bytes, carg);
207                 }
208         }
209
210         return 0;
211 }
212
213 static int __post_fill_counters(void *user_data)
214 {
215         struct counter_arg *carg = user_data;
216
217         if (carg->initiate)
218                 carg->initiate = 0;
219
220         return 0;
221 }
222
223 static void __process_network_counter(struct genl *ans,
224                                       struct counter_arg *carg)
225 {
226         struct netlink_serialization_params ser_params = {
227                 .carg = carg,
228                 .ans = ans,
229                 .eval_attr = __fill_counters,
230                 .post_eval_attr = __post_fill_counters,
231         };
232
233         netlink_serialization_command *netlink =
234                 netlink_create_command(&ser_params);
235         if (!netlink) {
236                 STC_LOGE("Can not create command");
237                 return;
238         }
239
240         netlink->deserialize_answer(&(netlink->params));
241 }
242
243 static gboolean __process_contr_reply(GIOChannel *source,
244                                       GIOCondition condition,
245                                       gpointer user_data)
246 {
247         int sock = g_io_channel_unix_get_fd(source);
248         struct genl *ans;
249         int ret;
250         stc_s *stc = stc_get_manager();
251
252 #ifdef TIZEN_GTESTS
253         void __gcov_flush(void);
254         __gcov_flush();
255 #endif
256
257         if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
258             (condition & G_IO_NVAL)) {
259                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
260
261                 STC_LOGE("Counter socket received G_IO event, closing socket."
262                          "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
263                          (condition & G_IO_ERR), (condition & G_IO_HUP),
264                          (condition & G_IO_NVAL));
265                 __close_and_reopen_contr_sock(g_system);
266                 return FALSE;
267         }
268
269         ans = MALLOC0(struct genl, 1);
270         if (ans == NULL) {
271                 STC_LOGE("Failed allocate memory to genl reply message");
272                 return TRUE;
273         }
274
275         if (stc == NULL) {
276                 STC_LOGE("Can't get stc data");
277                 goto out;
278         }
279
280         ret = read_netlink(sock, ans, sizeof(struct genl));
281
282         if (ret == 0)
283                 goto out;
284
285         stc->carg->ans_len = ret;
286         stc->carg->last_run_time = time(NULL);
287
288         __process_network_counter(ans, stc->carg);
289
290         g_idle_add(stc_monitor_app_flush_stats_to_db, NULL);
291         g_idle_add(stc_monitor_rstn_flush_contr_to_db, NULL);
292 out:
293
294         FREE(ans);
295         return TRUE;
296 }
297
298 static gboolean __update_contr_cb(void *user_data)
299 {
300         /* Here we just sent command, answer we receive in another callback */
301         stc_s *stc = stc_get_manager();
302         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
303         if (!stc->carg) {
304                 stc->carg = MALLOC0(counter_arg_s, 1);
305                 if (stc->carg == NULL)
306                         return TRUE;  /* we need to continue the timer */
307
308                 stc->carg->sock = g_system->contr_sock;
309         }
310
311 #ifdef TIZEN_GTESTS
312         void __gcov_flush(void);
313         __gcov_flush();
314 #endif
315
316         /* STC_LOGD("Get all counters"); */
317         nfacct_send_get_all(stc->carg);
318
319         /* we need to continue the timer */
320         return TRUE;
321 }
322
323 static void __fill_exceptions_list(void)
324 {
325         stc_plugin_fill_exception_list();
326 }
327
328 stc_error_e stc_monitor_init(void)
329 {
330         stc_system_s *system = MALLOC0(stc_system_s, 1);
331         GIOChannel *gio = NULL;
332
333         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY,
334                                 "stc_system_s malloc fail!");
335
336         /* initializing current classid */
337         init_current_classid();
338
339         /* initializing cgroups */
340         cgroup_init();
341
342         /* creating monitored application tree */
343         system->apps = stc_monitor_apps_init();
344         system->rstns = stc_monitor_rstns_init();
345
346         /* create netlink socket for updating kernel counters */
347         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
348         if (system->contr_sock < 0) {
349                 STC_LOGE("failed to open socket");
350                 FREE(system);
351                 return STC_ERROR_FAIL;
352         }
353
354         gio = g_io_channel_unix_new(system->contr_sock);
355         system->contr_gsource_id =
356                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
357                                (GIOFunc) __process_contr_reply,
358                                NULL);
359         g_io_channel_unref(gio);
360
361         g_system = system;
362
363         stc_monitor_app_add_by_iface(STC_TOTAL_DATACALL);
364         stc_monitor_app_add_by_iface(STC_TOTAL_WIFI);
365         stc_monitor_app_add_by_iface(STC_TOTAL_BLUETOOTH);
366         stc_monitor_app_add_by_iface(STC_TOTAL_IPV4);
367         stc_monitor_app_add_by_iface(STC_TOTAL_IPV6);
368         /* stc_monitor_app_add_by_iface(STC_TOTAL_TETHERING); */
369
370         __update_contr_cb(NULL);
371
372         /* registering periodic kernel counters update callback */
373         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
374                                                          __update_contr_cb,
375                                                          NULL);
376         if (g_system->contr_timer_id == 0) {
377                 STC_LOGE("Failed to register kernel counters update timer");
378                 __close_contr_sock(g_system);
379                 return STC_ERROR_FAIL;
380         }
381
382         __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
383                         (int *)&g_system->background_state);
384
385         __fill_exceptions_list();
386         stc_monitor_rstns_load();
387
388         return STC_ERROR_NONE;
389 }
390
391 stc_error_e stc_monitor_deinit(void)
392 {
393         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
394
395         /* close netlink socket for updating kernel counters */
396         __close_contr_sock(g_system);
397
398         /* remove kernel counters update timer */
399         if (g_system->contr_timer_id > 0) {
400                 g_source_remove(g_system->contr_timer_id);
401                 g_system->contr_timer_id = 0;
402         }
403
404         /* destroy monitored application tree */
405         g_hash_table_destroy(g_system->apps);
406         g_system->apps = NULL;
407
408         /* destroy restriction rules tree */
409         g_hash_table_destroy(g_system->rstns);
410         g_system->rstns = NULL;
411
412         FREE(g_system);
413
414         return STC_ERROR_NONE;
415 }
416
417 GHashTable *stc_monitor_get_system_apps(void)
418 {
419         ret_value_msg_if(g_system == NULL, NULL,
420                                 "stc monitor not initialized!");
421
422         ret_value_msg_if(g_system->apps == NULL, NULL,
423                                 "apps is null!");
424
425         return g_system->apps;
426 }
427
428 GHashTable *stc_monitor_get_system_rstns(void)
429 {
430         ret_value_msg_if(g_system == NULL, NULL,
431                                 "stc monitor not initialized!");
432
433         ret_value_msg_if(g_system->rstns == NULL, NULL,
434                                 "rstns is null!");
435
436         return g_system->rstns;
437 }
438
439 int stc_monitor_get_contr_sock(void)
440 {
441         ret_value_msg_if(g_system == NULL, 0,
442                                 "stc monitor not initialized!");
443
444         return g_system->contr_sock;
445 }
446
447 time_t stc_monitor_get_last_month_ts(void)
448 {
449         ret_value_msg_if(g_system == NULL, 0,
450                                 "stc monitor not initialized!");
451
452         return g_system->last_month_ts;
453 }
454
455 void stc_monitor_set_last_month_ts(time_t time)
456 {
457         ret_msg_if(g_system == NULL, "stc monitor not initialized!");
458
459         g_system->last_month_ts = time;
460 }
461
462 time_t stc_monitor_get_last_week_ts(void)
463 {
464         ret_value_msg_if(g_system == NULL, 0,
465                                 "stc monitor not initialized!");
466
467         return g_system->last_week_ts;
468 }
469
470 void stc_monitor_set_last_week_ts(time_t time)
471 {
472         ret_msg_if(g_system == NULL, "stc monitor not initialized!");
473
474         g_system->last_week_ts = time;
475 }
476
477 time_t stc_monitor_get_last_day_ts(void)
478 {
479         ret_value_msg_if(g_system == NULL, 0,
480                                 "stc monitor not initialized!");
481
482         return g_system->last_day_ts;
483 }
484
485 void stc_monitor_set_last_day_ts(time_t time)
486 {
487         ret_msg_if(g_system == NULL, "stc monitor not initialized!");
488
489         g_system->last_day_ts = time;
490 }
491
492 void stc_monitor_set_rstns_updated(gboolean value)
493 {
494         ret_msg_if(g_system == NULL, "stc monitor not initialized!");
495
496         g_system->rstns_updated = value;
497 }
498
499 gboolean stc_monitor_get_rstns_updated(void)
500 {
501         ret_value_msg_if(g_system == NULL, FALSE,
502                                 "stc monitor not initialized!");
503
504         return g_system->rstns_updated;
505 }
506
507 void stc_monitor_set_apps_updated(gboolean value)
508 {
509         ret_msg_if(g_system == NULL, "stc monitor not initialized!");
510
511         g_system->apps_updated = value;
512 }
513
514 gboolean stc_monitor_get_apps_updated(void)
515 {
516         ret_value_msg_if(g_system == NULL, FALSE,
517                                 "stc monitor not initialized!");
518
519         return g_system->apps_updated;
520 }
521
522 void stc_monitor_set_background_state(gboolean value)
523 {
524         ret_msg_if(g_system == NULL, "stc monitor not initialized!");
525
526         g_system->background_state = value;
527 }
528
529 gboolean stc_monitor_get_background_state(void)
530 {
531         ret_value_msg_if(g_system == NULL, FALSE,
532                                 "stc monitor not initialized!");
533
534         return g_system->background_state;
535 }
536
537 void stc_monitor_update_by_default_connection(void *data)
538 {
539         static default_connection_s old_connection;
540         default_connection_s *new_connection = (default_connection_s *)data;
541
542         if (old_connection.path != NULL) {
543                 stc_monitor_app_remove_by_connection(&old_connection);
544                 stc_monitor_rstn_remove_by_connection(&old_connection);
545
546                 iptables_flush_chains();
547         }
548
549         FREE(old_connection.path);
550         FREE(old_connection.ifname);
551         FREE(old_connection.tether_iface.ifname);
552         old_connection.type = 0;
553         old_connection.roaming = 0;
554         old_connection.tether_state = FALSE;
555         old_connection.tether_iface.type = 0;
556
557         if (new_connection != NULL && new_connection->path != NULL) {
558                 stc_monitor_app_add_by_connection(new_connection);
559                 stc_monitor_rstn_add_by_connection(new_connection);
560
561                 old_connection.path = g_strdup(new_connection->path);
562                 old_connection.ifname = g_strdup(new_connection->ifname);
563                 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
564                 old_connection.type = new_connection->type;
565                 old_connection.roaming = new_connection->roaming;
566                 old_connection.tether_state = new_connection->tether_state;
567                 old_connection.tether_iface.type = new_connection->tether_iface.type;
568         }
569 }
570
571 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
572 {
573         return stc_plugin_check_exception_by_cmdline(cmdline);
574 }