Refactoring structures for monitoring and restrictions
[platform/core/connectivity/stc-manager.git] / src / helper / helper-nfacct-rule.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 <errno.h>
18 #include <inttypes.h>
19 #include <stdbool.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25 #include <arpa/inet.h>
26
27 #include "counter.h"
28 #include "helper-nfacct-rule.h"
29 #include "helper-iptables.h"
30
31 #include "configure_stub.h"
32
33 #define IPTABLES "/usr/sbin/iptables"
34 #define IP6TABLES "/usr/sbin/ip6tables"
35 #define IPTABLES_CHECK "-C"
36 #define APPEND "-A"
37 #define DELETE "-D"
38 #define INSERT "-I"
39
40 #define NFACCT_NAME_MOD " -m nfacct --nfacct-name %s"
41 #define REJECT_RULE "REJECT"
42 #define ACCEPT_RULE "ACCEPT"
43 #define OUT_RULE "OUTPUT"
44 #define IN_RULE "INPUT"
45 #define FORWARD_RULE "FORWARD"
46
47 /* TODO idea to use the same rule both for BLOCK (REJECT) and WARNING (ACCEPT) */
48 #define RULE_APP_OUT "%s -w %s OUTPUT -o %s -m cgroup --cgroup %u %s %s"
49 #define RULE_APP_IN "%s -w %s INPUT -i %s -m cgroup --cgroup %u %s %s"
50
51 /* iptables -w [I/A/D] [OUTPUT/FORWARD/INPUT] -o/-i iface -m nfacct --nfacct-name name -j ACCEPT/REJECT */
52
53 #define RULE_IFACE_OUT "%s -w %s %s -o %s %s %s"
54 #define RULE_IFACE_IN "%s -w %s %s -i %s %s  %s"
55
56 #define NFNL_SUBSYS_ACCT                7
57 #define BUF_SIZE_FOR_ERR 100
58
59 static void prepare_netlink_msg(struct genl *req, int type, int flag)
60 {
61         int seq = time(NULL);
62         memset(req, 0, sizeof(struct genl));
63         req->n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
64         req->n.nlmsg_type = (NFNL_SUBSYS_ACCT << 8) | type;
65         req->n.nlmsg_flags = NLM_F_REQUEST | flag;
66         req->n.nlmsg_seq = seq;
67 }
68
69 static void add_value_attr(struct genl *req, const void *data, int len,
70                            int type)
71 {
72         int payload;
73         /* get tail */
74         struct nlattr *na = (struct nlattr *)((char *)req +
75                                               NLMSG_ALIGN(req->n.nlmsg_len));
76
77         na->nla_type = type;
78         payload = len + NLA_HDRLEN;
79         na->nla_len = payload;
80         memcpy(NLA_DATA(na), data, len);
81         req->n.nlmsg_len += NLMSG_ALIGN(payload);
82 }
83
84 /*
85  * following 2 function should be used in combination.
86  * start_nest_attr returns nlattr structure, which should be completed by
87  * end_nest_attr,
88  * before these invocations any number of netlink arguments could be inserted
89  * */
90 static struct nlattr *start_nest_attr(struct genl *req, uint16_t type)
91 {
92         struct nlattr *start = (struct nlattr *)((char *)req +
93                                                  NLMSG_ALIGN(req->n.nlmsg_len));
94
95         start->nla_type = NLA_F_NESTED | type;
96         req->n.nlmsg_len += NLMSG_ALIGN(sizeof(struct nlattr));
97         return start;
98 }
99
100 static void end_nest_attr(struct genl *req, struct nlattr *start)
101 {
102         start->nla_len = (__u16)((char *)req +
103                                  NLMSG_ALIGN(req->n.nlmsg_len) - (char *)start);
104 }
105
106 static void add_string_attr(struct genl *req, const char *str, int type)
107 {
108         add_value_attr(req, str, strlen(str) + 1, type);
109 }
110
111 static void add_uint64_attr(struct genl *req, const uint64_t v, int type)
112 {
113         add_value_attr(req, &v, sizeof(v), type);
114 }
115
116 /* macros or templare, due uint64 and uint32 is the same functions */
117 static void add_uint32_attr(struct genl *req, const uint32_t v, int type)
118 {
119         add_value_attr(req, &v, sizeof(v), type);
120 }
121
122 static stc_error_e send_nfacct_request(int sock, struct genl *req)
123 {
124         struct sockaddr_nl nladdr = {.nl_family = AF_NETLINK};
125         int ret = sendto(sock, (char *)(&req->n), req->n.nlmsg_len, 0,
126                          (struct sockaddr *)&nladdr, sizeof(nladdr));
127         ret_value_msg_if(ret < 0, STC_ERROR_FAIL,
128                          "Failed to send nfacct request, error [%d]", ret);
129
130         return STC_ERROR_NONE;
131 }
132
133 static stc_error_e nfacct_send_new(nfacct_rule_s *counter)
134 {
135         int ret = STC_ERROR_NONE;
136         struct genl *req = MALLOC0(struct genl, 1);
137         if (req == NULL) {
138                 STC_LOGE("Failed allocate memory to genl request message"); //LCOV_EXCL_LINE
139                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
140         }
141
142         prepare_netlink_msg(req, NFNL_MSG_ACCT_NEW, NLM_F_CREATE | NLM_F_ACK);
143         add_string_attr(req, counter->name, NFACCT_NAME);
144
145         /* padding */
146         add_uint64_attr(req, 0, NFACCT_PKTS);
147         add_uint64_attr(req, 0, NFACCT_BYTES);
148         //LCOV_EXCL_START
149         if (counter->quota) {
150                 STC_LOGD("quota bytes %"PRId64, counter->quota);
151
152                 add_uint32_attr(req, htobe32(NFACCT_F_QUOTA_BYTES),
153                                 NFACCT_FLAGS);
154                 add_uint64_attr(req, htobe64(counter->quota), NFACCT_QUOTA);
155         }
156         //LCOV_EXCL_STOP
157
158         ret = send_nfacct_request(counter->carg->sock, req);
159         FREE(req);
160         return ret;
161 }
162
163 stc_error_e nfacct_send_del(nfacct_rule_s *counter)
164 {
165         int ret = STC_ERROR_NONE;
166         struct genl *req = MALLOC0(struct genl, 1);
167         if (req == NULL) {
168                 STC_LOGE("Failed allocate memory to genl request message"); //LCOV_EXCL_LINE
169                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
170         }
171
172         prepare_netlink_msg(req, NFNL_MSG_ACCT_DEL, NLM_F_ACK);
173         add_string_attr(req, counter->name, NFACCT_NAME);
174
175         ret = send_nfacct_request(counter->carg->sock, req);
176         FREE(req);
177         return ret;
178 }
179 #define NFACCT_F_QUOTAS (NFACCT_F_QUOTA_BYTES | NFACCT_F_QUOTA_PKTS)
180
181 static stc_error_e internal_nfacct_send_get(struct counter_arg *carg,
182                                             enum nfnl_acct_msg_types get_type,
183                                             const char *name,
184                                             int mask, int filter)
185 {
186         int ret = STC_ERROR_NONE;
187         struct nlattr *na;
188         int flag = !name ? NLM_F_DUMP : 0;
189         struct genl *req = MALLOC0(struct genl, 1);
190         if (req == NULL) {
191                 STC_LOGE("Failed allocate memory to genl request message"); //LCOV_EXCL_LINE
192                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
193         }
194
195         prepare_netlink_msg(req, get_type, flag);
196         /* due we don't get counter with quota any where else,
197          * here we will request just counters by default */
198         if (name)
199                 add_string_attr(req, name, NFACCT_NAME);
200
201         na = start_nest_attr(req, NFACCT_FILTER);
202         add_uint32_attr(req, htonl(mask), NFACCT_FILTER_ATTR_MASK);
203         add_uint32_attr(req, htonl(filter), NFACCT_FILTER_ATTR_VALUE);
204         end_nest_attr(req, na);
205
206         ret = send_nfacct_request(carg->sock, req);
207         FREE(req);
208         return ret;
209 }
210
211 stc_error_e nfacct_send_get_counters(struct counter_arg *carg, const char *name)
212 {
213         /* get and reset countes value */
214         return internal_nfacct_send_get(carg, NFNL_MSG_ACCT_GET_CTRZERO, name,
215                                         NFACCT_F_QUOTAS, 0);
216 }
217
218 stc_error_e nfacct_send_get_quotas(struct counter_arg *carg, const char *name)
219 {
220         /* just get counters */
221         return internal_nfacct_send_get(carg, NFNL_MSG_ACCT_GET, name,
222                                         NFACCT_F_QUOTA_BYTES,
223                                         NFACCT_F_QUOTA_BYTES);
224 }
225
226 stc_error_e nfacct_send_get_all(struct counter_arg *carg)
227 {
228         /* get and reset everything, used when quiting */
229         return internal_nfacct_send_get(carg, NFNL_MSG_ACCT_GET_CTRZERO, NULL,
230                                         0, 0);
231 }
232
233 stc_error_e nfacct_send_get(nfacct_rule_s *rule)
234 {
235         if (rule->intend == NFACCT_BLOCK || rule->intend == NFACCT_WARN)
236                 return nfacct_send_get_quotas(rule->carg, rule->name);
237         else if (rule->intend == NFACCT_COUNTER)
238                 return nfacct_send_get_counters(rule->carg, rule->name);
239
240         return STC_ERROR_INVALID_PARAMETER;
241 }
242
243 static nfacct_rule_direction convert_to_iotype(int type)
244 {
245         return (type < NFACCT_COUNTER_LAST_ELEM &&
246                 type > NFACCT_COUNTER_UNKNOWN) ? type : NFACCT_COUNTER_UNKNOWN;
247 }
248
249 static stc_iface_type_e convert_to_iftype(int type)
250 {
251         return (type < STC_IFACE_LAST_ELEM &&
252                         type > STC_IFACE_UNKNOWN) ? type : STC_IFACE_UNKNOWN;
253 }
254
255 bool recreate_counter_by_name(char *cnt_name, nfacct_rule_s *cnt)
256 {
257         char *iftype_part;
258         char *classid_part;
259         char *io_part;
260         char *ifname_part;
261         char *save_ptr = NULL;
262         char name[NFACCT_NAME_MAX] = {0}; /* parse buffer to avoid cnt_name modification */
263
264         strncpy(name, cnt_name, sizeof(name) - 1);
265
266         switch (name[0]) {
267         case 'c':
268                 cnt->intend  = NFACCT_COUNTER;
269                 break;
270         case 'w':
271                 cnt->intend  = NFACCT_WARN;
272                 break;
273         case 'r':
274                 cnt->intend  = NFACCT_BLOCK;
275                 break;
276         case 'a':
277                 cnt->intend  = NFACCT_ALLOW;
278                 break;
279         case 't':
280                 cnt->intend  = NFACCT_TETH_COUNTER; //LCOV_EXCL_LINE
281                 break; //LCOV_EXCL_LINE
282         default:
283                 return false;
284         }
285
286         STRING_SAVE_COPY(cnt->name, cnt_name);
287
288 #if 0
289         /* ========================================================
290          * NOTE:-
291          * Below parsing for tethering case is not in use
292          * stc-manager needs to ignore this for NFACCT_TETH_COUNTER
293          * this is disbaled for future use.
294          * =======================================================*/
295
296         //LCOV_EXCL_START
297         if (cnt->intend == NFACCT_TETH_COUNTER) {
298                 char ifname_buf[MAX_IFACE_LENGTH];
299                 int ifname_len;
300                 stc_iface_type_e iface;
301                 /* tbnep+:seth_w0; means comes by bt go away by mobile interface,
302                  * it's outgoing traffic, due all tethering is mobile databased */
303                 iftype_part = strchr(name, ':');
304                 ret_value_msg_if(iftype_part == NULL,
305                                  false, "Invalid format of the tethering counter %s", name);
306                 ifname_len = iftype_part - name - 1;
307                 strncpy(ifname_buf, name + 1, ifname_len); /* skip first t */
308                 ifname_buf[ifname_len] = '\0';
309                 iface = get_iftype_by_name(ifname_buf);
310                 /* check first part is it datacall */
311                 if (iface == STC_IFACE_DATACALL) {
312                         strncpy(cnt->ifname, ifname_buf, MAX_IFACE_LENGTH - 1);
313                         cnt->iotype = NFACCT_COUNTER_IN;
314                 } else {
315                         /* +1, due : symbol and till the end of cnt_name */
316                         strncpy(ifname_buf, iftype_part + 1, MAX_IFACE_LENGTH - 1);
317                         iface = get_iftype_by_name(ifname_buf);
318                         if (iface == STC_IFACE_DATACALL) {
319                                 cnt->iotype = NFACCT_COUNTER_OUT;
320                                 strncpy(cnt->ifname, ifname_buf, MAX_IFACE_LENGTH - 1);
321                         }
322                 }
323
324                 if (cnt->iotype == NFACCT_COUNTER_UNKNOWN) {
325                         STC_LOGE("can't determine tethering direction %s", name);
326                         return false;
327                 }
328                 cnt->iftype = STC_IFACE_DATACALL;
329                 cnt->classid = STC_TETHERING_APP_CLASSID;
330                 return true;
331         }
332         //LCOV_EXCL_STOP
333 #endif
334
335         io_part = strtok_r(name, "_", &save_ptr);
336         if (io_part != NULL)
337                 cnt->iotype = convert_to_iotype(atoi(io_part + 1));
338         else
339                 return false;
340
341         iftype_part = strtok_r(NULL, "_", &save_ptr);
342         if (iftype_part != NULL)
343                 cnt->iftype = convert_to_iftype(atoi(iftype_part));
344         else
345                 return false;
346
347         classid_part = strtok_r(NULL, "_", &save_ptr);
348         if (classid_part != NULL)
349                 cnt->classid = atoi(classid_part);
350         else {
351                 cnt->classid = STC_ALL_APP_CLASSID;
352                 return cnt->intend == NFACCT_BLOCK ? true : false;
353         }
354
355         ifname_part = strtok_r(NULL, "\0", &save_ptr);
356         if (ifname_part != NULL)
357                 STRING_SAVE_COPY(cnt->ifname, ifname_part);
358         else
359                 return false;
360
361         return true;
362 }
363
364 static void _process_answer(struct netlink_serialization_params *params)
365 {
366         struct rtattr *na;
367         struct rtattr *attr_list[__NFACCT_MAX] = {0};
368         struct counter_arg *carg = params->carg;
369         struct genl *ans = params->ans;;
370         struct nlmsghdr *nlhdr = &ans->n;
371         int len = GENLMSG_PAYLOAD(nlhdr);
372         int ans_len = carg->ans_len;
373
374         if (len == 0)
375                 return;
376
377         /* parse reply message */
378         na = (struct rtattr *)GENLMSG_DATA(ans);
379
380         while (NLMSG_OK(nlhdr, ans_len)) {
381                 fill_attribute_list(attr_list, NFACCT_MAX,
382                                     na, len);
383                 if (!attr_list[NFACCT_NAME] ||
384                     !attr_list[NFACCT_BYTES])
385                         goto next;
386                 params->eval_attr(attr_list, carg);
387
388 next:
389                 nlhdr = NLMSG_NEXT(nlhdr, ans_len);
390                 if (ans_len < 0)
391                         break;
392                 na = (struct rtattr *)GENLMSG_DATA(nlhdr);
393         }
394
395         if (params->post_eval_attr)
396                 params->post_eval_attr(carg);
397 }
398
399 netlink_serialization_command *
400 netlink_create_command(struct netlink_serialization_params *params)
401 {
402         static netlink_serialization_command command = {0,};
403         command.deserialize_answer = _process_answer;
404         command.params = *params;
405         return &command;
406 }
407
408 static char *get_iptables_cmd(const nfacct_rule_action action)
409 {
410         if (action == NFACCT_ACTION_APPEND)
411                 return APPEND;
412         else if (action == NFACCT_ACTION_DELETE)
413                 return DELETE;
414         else if (action == NFACCT_ACTION_INSERT)
415                 return INSERT;
416
417         return "";
418 }
419
420 static char *get_iptables_chain(const nfacct_rule_direction iotype)
421 {
422         if (iotype == NFACCT_COUNTER_IN)
423                 return STC_IN_CHAIN;
424         else if (iotype == NFACCT_COUNTER_OUT)
425                 return STC_OUT_CHAIN;
426         else if (iotype == NFACCT_COUNTER_FORWARD) //LCOV_EXCL_LINE
427                 return STC_FRWD_CHAIN; //LCOV_EXCL_LINE
428
429         return "";
430 }
431
432 static char *get_iptables_jump(const nfacct_rule_jump jump)
433 {
434         if (jump == NFACCT_JUMP_ACCEPT)
435                 return ACCEPT_RULE;
436         else if (jump == NFACCT_JUMP_REJECT)
437                 return REJECT_RULE;
438
439         return "";
440 }
441
442 /*
443 static char *choose_iftype_name(nfacct_rule_s *rule)
444 {
445         return strlen(rule->ifname) != 0 ? rule->ifname :
446                 get_iftype_name(rule->iftype);
447 }
448 */
449
450 static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule)
451 {
452         stc_error_e ret = STC_ERROR_NONE;
453         iptables_ip_type_e iptype;
454         iptables_rule_s iptables_rule;
455         memset(&iptables_rule, 0, sizeof(iptables_rule_s));
456
457         iptables_rule.nfacct_name = g_strdup(rule->name);
458         iptables_rule.ifname = g_strdup(rule->ifname);
459         iptables_rule.target = g_strdup(get_iptables_jump(rule->jump));
460
461         /* In case of tehering rules use chain 'STC_TETHER' */
462         if (rule->intend == NFACCT_TETH_COUNTER ||
463                         rule->intend == NFACCT_TETH_ALLOW ||
464                         rule->intend == NFACCT_TETH_BLOCK)
465                 iptables_rule.chain = g_strdup(STC_TETHER_CHAIN);
466         else
467                 iptables_rule.chain = g_strdup(get_iptables_chain(rule->iotype));
468
469         iptables_rule.classid = rule->classid;
470         iptables_rule.direction = (rule->iotype & NFACCT_COUNTER_IN) ?
471                                         IPTABLES_DIRECTION_IN : IPTABLES_DIRECTION_OUT;
472         iptype = (iptables_ip_type_e)rule->iptype;
473
474         /* specify the ip range type for source and destination */
475         iptables_rule.s_iprange_type = rule->src_iprange_type;
476         iptables_rule.d_iprange_type = rule->dst_iprange_type;
477
478         /* specify source and destination ip address if any */
479         if (rule->src_ip1)
480                 inet_aton(rule->src_ip1, &iptables_rule.s_ip1);
481         if (rule->src_ip2)
482                 inet_aton(rule->src_ip2, &iptables_rule.s_ip2);
483         if (rule->dst_ip1)
484                 inet_aton(rule->dst_ip1, &iptables_rule.d_ip1);
485         if (rule->dst_ip2)
486                 inet_aton(rule->dst_ip2, &iptables_rule.d_ip2);
487
488         if (rule->action == NFACCT_ACTION_DELETE) {
489                 /* delete interface rule */
490                 ret = iptables_remove(&iptables_rule, iptype);
491         } else {
492                 /* add interface rule */
493                 ret = iptables_add(&iptables_rule, iptype);
494         }
495
496         g_free(iptables_rule.nfacct_name);
497         g_free(iptables_rule.ifname);
498         g_free(iptables_rule.target);
499         g_free(iptables_rule.chain);
500
501         return ret;
502 }
503
504 static stc_error_e produce_app_rule(nfacct_rule_s *rule)
505 {
506         if (rule == NULL)
507                 return STC_ERROR_INVALID_PARAMETER;
508
509         char *set_cmd = get_iptables_cmd(rule->action);
510         char *jump_cmd = get_iptables_jump(rule->jump);
511         char nfacct_buf[sizeof(NFACCT_NAME_MOD) +
512                 3*MAX_DEC_SIZE(int) + 4 + 1];
513         stc_error_e ret = STC_ERROR_NONE;
514         uint32_t classid = rule->classid;
515
516         /* income part */
517         if (rule->iotype & NFACCT_COUNTER_IN) {
518                 rule->quota = rule->rcv_limit;
519                 rule->iotype = NFACCT_COUNTER_IN;
520                 generate_counter_name(rule);
521
522                 /* to support quated counter we need nfacct,
523                  *      don't use it in case of just block without a limit
524                  *      iow, send_limit = 0 and rcv_limit 0 */
525                 if (rule->action != NFACCT_ACTION_DELETE) {
526                         ret = nfacct_send_del(rule);
527                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
528                                          "can't del quota counter");
529
530                         ret = nfacct_send_new(rule);
531                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
532                                          "can't set nfacct counter");
533                         keep_counter(rule);
534                 }
535
536                 /* we have a counter, let's key in a rule, drop in case of
537                  *  send_limit/rcv_limit */
538                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf), NFACCT_NAME_MOD,
539                                rule->name);
540                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
541                                  STC_ERROR_FAIL, "Not enought buffer");
542
543                 /* cgroup extention on FORWARD chain are not allowed
544                  * remove classid info in case of tethering rules */
545                 if (rule->intend == NFACCT_TETH_COUNTER ||
546                                 rule->intend == NFACCT_TETH_ALLOW ||
547                                 rule->intend == NFACCT_TETH_BLOCK) {
548                         classid = rule->classid;
549                         rule->classid = 0;
550                 }
551
552                 ret = exec_iptables_cmd(rule);
553
554                 /* restore the classid info in case of tethering rule */
555                 if (rule->intend == NFACCT_TETH_COUNTER ||
556                                 rule->intend == NFACCT_TETH_ALLOW ||
557                                 rule->intend == NFACCT_TETH_BLOCK)
558                         rule->classid = classid;
559
560                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
561                                  "Can't set conditional block for ingress"
562                                  " traffic, for classid %u, cmd %s, j %s",
563                                  rule->classid, set_cmd, jump_cmd);
564
565                 /* remove in any case */
566                 if (rule->action == NFACCT_ACTION_DELETE) {
567                         /* TODO here and everywhere should be not just a del,
568                          *      here should be get counted value and than
569                          *      set new counter with that value, but it's minor issue,
570                          *      due it's not clear when actual counters was stored,
571                          *      and based on which value settings made such decition */
572                         rule->iptables_rule = nfacct_send_del;
573                         set_finalize_flag(rule);
574                         nfacct_send_get(rule);
575                         ret = nfacct_send_del(rule);
576                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
577                                          "can't del quota counter");
578                 }
579         }
580
581         if (rule->iotype & NFACCT_COUNTER_OUT) {
582                 /* outcome part */
583                 rule->iotype = NFACCT_COUNTER_OUT;
584                 rule->quota = rule->send_limit;
585                 generate_counter_name(rule);
586                 if (rule->action != NFACCT_ACTION_DELETE) {
587                         ret = nfacct_send_del(rule);
588                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
589                                          "can't del quota counter");
590
591                         ret = nfacct_send_new(rule);
592                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
593                                          "can't set quota counter");
594                         keep_counter(rule);
595                 }
596
597                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf), NFACCT_NAME_MOD,
598                                rule->name);
599                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
600                                  STC_ERROR_FAIL, "Not enought buffer");
601
602                 /* cgroup extention on FORWARD chain are not allowed
603                  * remove classid info in case of tethering rules */
604                 if (rule->intend == NFACCT_TETH_COUNTER ||
605                                 rule->intend == NFACCT_TETH_ALLOW ||
606                                 rule->intend == NFACCT_TETH_BLOCK) {
607                         classid = rule->classid;
608                         rule->classid = 0;
609                 }
610
611                 ret = exec_iptables_cmd(rule);
612
613                 /* restore the classid info in case of tethering rule */
614                 if (rule->intend == NFACCT_TETH_COUNTER ||
615                                 rule->intend == NFACCT_TETH_ALLOW ||
616                                 rule->intend == NFACCT_TETH_BLOCK)
617                         rule->classid = classid;
618
619                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
620                                  "Can't set conditional block for engress"
621                                  " traffic, for classid %u, cmd %s, j %s",
622                                  rule->classid, set_cmd, jump_cmd);
623
624                 if (rule->action == NFACCT_ACTION_DELETE) {
625                         rule->iptables_rule = nfacct_send_del;
626                         /* not effective, it's better to replace
627                          * set_finalize_flag by set_property,
628                          * due keep_counter it necessary only for
629                          * setting iptables_rule */
630                         set_finalize_flag(rule);
631                         nfacct_send_get(rule);
632                         ret = nfacct_send_del(rule);
633                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
634                                          "can't del quota counter");
635                 }
636         }
637         return STC_ERROR_NONE;
638 }
639
640 static stc_error_e produce_iface_rule(nfacct_rule_s *rule)
641 {
642         if (rule == NULL)
643                 return STC_ERROR_INVALID_PARAMETER;
644
645         char *set_cmd = get_iptables_cmd(rule->action);
646         char *jump_cmd = get_iptables_jump(rule->jump);
647         char nfacct_buf[sizeof(NFACCT_NAME_MOD) +
648                 3*MAX_DEC_SIZE(int) + 4 + 1];
649         uint32_t classid = rule->classid;
650         stc_error_e ret;
651
652         if (rule->iotype & NFACCT_COUNTER_IN) {
653                 /* income part */
654                 rule->iotype = NFACCT_COUNTER_IN;
655                 rule->quota = rule->rcv_limit;
656                 generate_counter_name(rule);
657
658                 if (rule->action != NFACCT_ACTION_DELETE) {
659                         /* send delete comman in case of creation,
660                          * because nfacct doesn't reset value for nfacct quota
661                          * in case of quota existing */
662                         ret = nfacct_send_del(rule);
663                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
664                                          "can't del quota counter");
665
666                         ret = nfacct_send_new(rule);
667                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
668                                          "can't set quota counter");
669                         keep_counter(rule);
670                 }
671
672                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf),
673                                NFACCT_NAME_MOD, rule->name);
674                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
675                                  STC_ERROR_FAIL, "Not enought buffer");
676
677                 classid = rule->classid;
678                 rule->classid = 0;
679
680                 ret = exec_iptables_cmd(rule);
681                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
682                                  "Can't set conditional block for ingress"
683                                  " traffic, for iftype %d, cmd %s, j %s",
684                                  rule->iftype, set_cmd, jump_cmd);
685
686                 //LCOV_EXCL_START
687                 /* for tethering */
688                 if (rule->intend == NFACCT_WARN ||
689                     rule->intend == NFACCT_BLOCK) {
690                         /* RULE_IFACE_OUT is not a misprint here */
691                         nfacct_rule_direction temp_iotype = rule->iotype;
692
693                         rule->iotype = NFACCT_COUNTER_FORWARD;
694                         ret = exec_iptables_cmd(rule);
695                         rule->iotype = temp_iotype;
696                         ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
697                                          "Can't set forward rule for ingress "
698                                          "traffic, for iftype %d, cmd %s, j %s",
699                                          rule->iftype, set_cmd, jump_cmd);
700                 }
701                 /* tethering */
702
703                 if (rule->action == NFACCT_ACTION_DELETE) {
704                         rule->iptables_rule = nfacct_send_del;
705                         set_finalize_flag(rule);
706                         nfacct_send_get(rule);
707                         ret = nfacct_send_del(rule);
708                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
709                                          "can't del quota counter");
710                 }
711                 //LCOV_EXCL_STOP
712         }
713
714         rule->classid = classid;
715
716         if (rule->iotype & NFACCT_COUNTER_OUT) {
717                 /* outcome part */
718                 rule->iotype = NFACCT_COUNTER_OUT;
719                 rule->quota = rule->send_limit;
720                 generate_counter_name(rule);
721
722                 if (rule->action != NFACCT_ACTION_DELETE) {
723                         /* send delete comman in case of creation,
724                          * because nfacct doesn't reset value for nfacct quota
725                          * in case of quota existing */
726                         ret = nfacct_send_del(rule);
727                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
728                                          "can't del quota counter");
729
730                         ret = nfacct_send_new(rule);
731                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
732                                          "can't set quota counter");
733                         keep_counter(rule);
734                 }
735
736                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf),
737                                NFACCT_NAME_MOD, rule->name);
738                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
739                                  STC_ERROR_FAIL, "Not enough buffer");
740
741                 classid = rule->classid;
742                 rule->classid = 0;
743
744                 ret = exec_iptables_cmd(rule);
745                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
746                                  "Can't set conditional block for "
747                                  "engress traffic, for iftype %d, cmd %s, j %s",
748                                  rule->iftype, set_cmd, jump_cmd);
749
750                 //LCOV_EXCL_START
751                 /* for tethering  */
752                 if (rule->intend == NFACCT_WARN ||
753                     rule->intend == NFACCT_BLOCK) {
754                         nfacct_rule_direction temp_iotype = rule->iotype;
755
756                         rule->iotype = NFACCT_COUNTER_OUT;
757                         ret = exec_iptables_cmd(rule);
758                         rule->iotype = temp_iotype;
759                         ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
760                                          "Can't set forward rule for engress "
761                                          "traffic, for iftype %d, cmd %s, j %s",
762                                          rule->iftype, set_cmd, jump_cmd);
763                 }
764                 /* tethering  */
765
766                 if (rule->action == NFACCT_ACTION_DELETE) {
767                         rule->iptables_rule = nfacct_send_del;
768                         set_finalize_flag(rule);
769                         nfacct_send_get(rule);
770                         ret = nfacct_send_del(rule);
771                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
772                                          "can't del quota counter");
773                 }
774                 //LCOV_EXCL_STOP
775         }
776
777         rule->classid = classid;
778
779         return STC_ERROR_NONE;
780 }
781
782 stc_error_e produce_net_rule(nfacct_rule_s *rule)
783 {
784         stc_error_e ret = STC_ERROR_NONE;
785
786         if (rule == NULL)
787                 return STC_ERROR_INVALID_PARAMETER;
788
789         if (rule->action == NFACCT_ACTION_APPEND &&
790             rule->intend == NFACCT_WARN &&
791             !rule->send_limit && !rule->rcv_limit)
792                 return STC_ERROR_NONE;
793
794         if (rule->classid != STC_ALL_APP_CLASSID &&
795             rule->classid != STC_TETHERING_APP_CLASSID &&
796             rule->classid != STC_TOTAL_DATACALL_CLASSID &&
797             rule->classid != STC_TOTAL_WIFI_CLASSID &&
798             rule->classid != STC_TOTAL_BLUETOOTH_CLASSID &&
799             rule->classid != STC_TOTAL_IPV4_CLASSID &&
800             rule->classid != STC_TOTAL_IPV6_CLASSID)
801                 ret = produce_app_rule(rule);
802         else
803                 ret = produce_iface_rule(rule);
804
805         return ret;
806 }
807
808 void generate_counter_name(nfacct_rule_s *counter)
809 {
810         char warn_symbol = 'c';
811         if (!strlen(counter->ifname)) {
812                 char *iftype_name = get_iftype_name(counter->iftype);
813                 /* trace counter name, maybe name was already generated */
814                 ret_msg_if(iftype_name == NULL,
815                            "Can't get interface name for counter %s, iftype %d)!",
816                            counter->name, counter->iftype);
817                 STRING_SAVE_COPY(counter->ifname, iftype_name);
818         }
819
820         if (counter->intend  == NFACCT_WARN ||
821                         counter->intend == NFACCT_TETH_WARN)
822                 warn_symbol = 'w';
823         else if (counter->intend  == NFACCT_BLOCK ||
824                         counter->intend == NFACCT_TETH_BLOCK)
825                 warn_symbol = 'r';
826         else if (counter->intend  == NFACCT_ALLOW ||
827                         counter->intend == NFACCT_TETH_ALLOW)
828                 warn_symbol = 'a';
829         else if (counter->intend == NFACCT_TETH_COUNTER)
830                 warn_symbol = 't';
831         snprintf(counter->name, NFACCT_NAME_MAX, "%c%d_%d_%d_%s",
832                  warn_symbol, counter->iotype, counter->iftype,
833                  counter->classid, counter->ifname);
834 }
835