Creating seperate chains[STC_IN, STC_OUT, STC_FRWD] for STC Framework's rules.
[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");
139                 return STC_ERROR_OUT_OF_MEMORY;
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         if (STC_DEBUG_LOG)
146                 STC_LOGD("counter name %s", counter->name);
147
148         /* padding */
149         add_uint64_attr(req, 0, NFACCT_PKTS);
150         add_uint64_attr(req, 0, NFACCT_BYTES);
151         if (counter->quota) {
152                 STC_LOGD("quota bytes %"PRId64, counter->quota);
153
154                 add_uint32_attr(req, htobe32(NFACCT_F_QUOTA_BYTES),
155                                 NFACCT_FLAGS);
156                 add_uint64_attr(req, htobe64(counter->quota), NFACCT_QUOTA);
157         }
158
159         ret = send_nfacct_request(counter->carg->sock, req);
160         FREE(req);
161         return ret;
162 }
163
164 stc_error_e nfacct_send_del(nfacct_rule_s *counter)
165 {
166         int ret = STC_ERROR_NONE;
167         struct genl *req = MALLOC0(struct genl, 1);
168         if (req == NULL) {
169                 STC_LOGE("Failed allocate memory to genl request message");
170                 return STC_ERROR_OUT_OF_MEMORY;
171         }
172
173         if (STC_DEBUG_LOG)
174                 STC_LOGD("send remove request for %s", counter->name);
175
176         prepare_netlink_msg(req, NFNL_MSG_ACCT_DEL, NLM_F_ACK);
177         add_string_attr(req, counter->name, NFACCT_NAME);
178
179         ret = send_nfacct_request(counter->carg->sock, req);
180         FREE(req);
181         return ret;
182 }
183 #define NFACCT_F_QUOTAS (NFACCT_F_QUOTA_BYTES | NFACCT_F_QUOTA_PKTS)
184
185 static stc_error_e internal_nfacct_send_get(struct counter_arg *carg,
186                                             enum nfnl_acct_msg_types get_type,
187                                             const char *name,
188                                             int mask, int filter)
189 {
190         int ret = STC_ERROR_NONE;
191         struct nlattr *na;
192         int flag = !name ? NLM_F_DUMP : 0;
193         struct genl *req = MALLOC0(struct genl, 1);
194         if (req == NULL) {
195                 STC_LOGE("Failed allocate memory to genl request message");
196                 return STC_ERROR_OUT_OF_MEMORY;
197         }
198
199         prepare_netlink_msg(req, get_type, flag);
200         /* due we don't get counter with quota any where else,
201          * here we will request just counters by default */
202         if (name)
203                 add_string_attr(req, name, NFACCT_NAME);
204
205         na = start_nest_attr(req, NFACCT_FILTER);
206         add_uint32_attr(req, htonl(mask), NFACCT_FILTER_ATTR_MASK);
207         add_uint32_attr(req, htonl(filter), NFACCT_FILTER_ATTR_VALUE);
208         end_nest_attr(req, na);
209
210         ret = send_nfacct_request(carg->sock, req);
211         FREE(req);
212         return ret;
213 }
214
215 stc_error_e nfacct_send_get_counters(struct counter_arg *carg, const char *name)
216 {
217         /* get and reset countes value */
218         return internal_nfacct_send_get(carg, NFNL_MSG_ACCT_GET_CTRZERO, name,
219                                         NFACCT_F_QUOTAS, 0);
220 }
221
222 stc_error_e nfacct_send_get_quotas(struct counter_arg *carg, const char *name)
223 {
224         /* just get counters */
225         return internal_nfacct_send_get(carg, NFNL_MSG_ACCT_GET, name,
226                                         NFACCT_F_QUOTA_BYTES,
227                                         NFACCT_F_QUOTA_BYTES);
228 }
229
230 stc_error_e nfacct_send_get_all(struct counter_arg *carg)
231 {
232         /* get and reset everything, used when quiting */
233         return internal_nfacct_send_get(carg, NFNL_MSG_ACCT_GET_CTRZERO, NULL,
234                                         0, 0);
235 }
236
237 stc_error_e nfacct_send_get(nfacct_rule_s *rule)
238 {
239         if (rule->intend == NFACCT_BLOCK || rule->intend == NFACCT_WARN)
240                 return nfacct_send_get_quotas(rule->carg, rule->name);
241         else if (rule->intend == NFACCT_COUNTER)
242                 return nfacct_send_get_counters(rule->carg, rule->name);
243
244         return STC_ERROR_INVALID_PARAMETER;
245 }
246
247 static nfacct_rule_direction convert_to_iotype(int type)
248 {
249         return (type < NFACCT_COUNTER_LAST_ELEM &&
250                 type > NFACCT_COUNTER_UNKNOWN) ? type : NFACCT_COUNTER_UNKNOWN;
251 }
252
253 static stc_iface_type_e convert_to_iftype(int type)
254 {
255         return (type < STC_IFACE_LAST_ELEM &&
256                 type > STC_IFACE_UNKNOWN) ? type : STC_IFACE_UNKNOWN;
257 }
258
259 bool recreate_counter_by_name(char *cnt_name, nfacct_rule_s *cnt)
260 {
261         char *iftype_part;
262         char *classid_part;
263         char *io_part;
264         char *ifname_part;
265         char *save_ptr = NULL;
266         char name[NFACCT_NAME_MAX] = {0}; /* parse buffer to avoid cnt_name modification */
267
268         strncpy(name, cnt_name, sizeof(name) - 1);
269
270         switch (name[0]) {
271         case 'c':
272                 cnt->intend  = NFACCT_COUNTER;
273                 break;
274         case 'w':
275                 cnt->intend  = NFACCT_WARN;
276                 break;
277         case 'r':
278                 cnt->intend  = NFACCT_BLOCK;
279                 break;
280         case 't':
281                 cnt->intend  = NFACCT_TETH_COUNTER;
282                 break;
283         default:
284                 return false;
285         }
286
287         STRING_SAVE_COPY(cnt->name, cnt_name);
288
289         if (cnt->intend == NFACCT_TETH_COUNTER) {
290                 char ifname_buf[MAX_IFACE_LENGTH];
291                 int ifname_len;
292                 stc_iface_type_e iface;
293                 /* tbnep+:seth_w0; means comes by bt go away by mobile interface,
294                  * it's outgoing traffic, due all tethering is mobile databased */
295                 iftype_part = strchr(name, ':');
296                 ret_value_msg_if(iftype_part == NULL,
297                                  false, "Invalid format of the tethering counter %s", name);
298                 ifname_len = iftype_part - name - 1;
299                 strncpy(ifname_buf, name + 1, ifname_len); /* skip first t */
300                 ifname_buf[ifname_len] = '\0';
301                 iface = get_iftype_by_name(ifname_buf);
302                 /* check first part is it datacall */
303                 if (iface == STC_IFACE_DATACALL) {
304                         strncpy(cnt->ifname, ifname_buf, MAX_IFACE_LENGTH - 1);
305                         cnt->iotype = NFACCT_COUNTER_IN;
306                 } else {
307                         /* +1, due : symbol and till the end of cnt_name */
308                         strncpy(ifname_buf, iftype_part + 1, MAX_IFACE_LENGTH - 1);
309                         iface = get_iftype_by_name(ifname_buf);
310                         if (iface == STC_IFACE_DATACALL) {
311                                 cnt->iotype = NFACCT_COUNTER_OUT;
312                                 strncpy(cnt->ifname, ifname_buf, MAX_IFACE_LENGTH - 1);
313                         }
314                 }
315
316                 if (cnt->iotype == NFACCT_COUNTER_UNKNOWN) {
317                         STC_LOGE("can't determine tethering direction %s", name);
318                         return false;
319                 }
320                 cnt->iftype = STC_IFACE_DATACALL;
321                 cnt->classid = STC_TETHERING_APP_CLASSID;
322                 return true;
323         }
324
325         io_part = strtok_r(name, "_", &save_ptr);
326         if (io_part != NULL)
327                 cnt->iotype = convert_to_iotype(atoi(io_part + 1));
328         else
329                 return false;
330
331         iftype_part = strtok_r(NULL, "_", &save_ptr);
332         if (iftype_part != NULL)
333                 cnt->iftype = convert_to_iftype(atoi(iftype_part));
334         else
335                 return false;
336
337         classid_part = strtok_r(NULL, "_", &save_ptr);
338         if (classid_part != NULL)
339                 cnt->classid = atoi(classid_part);
340         else {
341                 cnt->classid = STC_ALL_APP_CLASSID;
342                 return cnt->intend == NFACCT_BLOCK ? true : false;
343         }
344
345         ifname_part = strtok_r(NULL, "\0", &save_ptr);
346         if (ifname_part != NULL)
347                 STRING_SAVE_COPY(cnt->ifname, ifname_part);
348         else
349                 return false;
350
351         return true;
352 }
353
354 static void _process_answer(struct netlink_serialization_params *params)
355 {
356         struct rtattr *na;
357         struct rtattr *attr_list[__NFACCT_MAX] = {0};
358         struct counter_arg *carg = params->carg;
359         struct genl *ans = params->ans;;
360         struct nlmsghdr *nlhdr = &ans->n;
361         int len = GENLMSG_PAYLOAD(nlhdr);
362         int ans_len = carg->ans_len;
363
364         if (len == 0)
365                 return;
366
367         /* parse reply message */
368         na = (struct rtattr *)GENLMSG_DATA(ans);
369
370         while (NLMSG_OK(nlhdr, ans_len)) {
371                 fill_attribute_list(attr_list, NFACCT_MAX,
372                                     na, len);
373                 if (!attr_list[NFACCT_NAME] ||
374                     !attr_list[NFACCT_BYTES])
375                         goto next;
376                 params->eval_attr(attr_list, carg);
377
378 next:
379                 nlhdr = NLMSG_NEXT(nlhdr, ans_len);
380                 if (ans_len < 0)
381                         break;
382                 na = (struct rtattr *)GENLMSG_DATA(nlhdr);
383         }
384
385         if (params->post_eval_attr)
386                 params->post_eval_attr(carg);
387 }
388
389 netlink_serialization_command *
390 netlink_create_command(struct netlink_serialization_params *params)
391 {
392         static netlink_serialization_command command = {0,};
393         command.deserialize_answer = _process_answer;
394         command.params = *params;
395         return &command;
396 }
397
398 static char *get_iptables_cmd(const nfacct_rule_action action)
399 {
400         if (action == NFACCT_ACTION_APPEND)
401                 return APPEND;
402         else if (action == NFACCT_ACTION_DELETE)
403                 return DELETE;
404         else if (action == NFACCT_ACTION_INSERT)
405                 return INSERT;
406
407         return "";
408 }
409
410 static char *get_iptables_chain(const nfacct_rule_direction iotype)
411 {
412         if (iotype == NFACCT_COUNTER_IN)
413                 return STC_IN_CHAIN;
414         else if (iotype == NFACCT_COUNTER_OUT)
415                 return STC_OUT_CHAIN;
416         else if (iotype == NFACCT_COUNTER_FORWARD)
417                 return STC_FRWD_CHAIN;
418
419         return "";
420 }
421
422 static char *get_iptables_jump(const nfacct_rule_jump jump)
423 {
424         if (jump == NFACCT_JUMP_ACCEPT)
425                 return ACCEPT_RULE;
426         else if (jump == NFACCT_JUMP_REJECT)
427                 return REJECT_RULE;
428
429         return "";
430 }
431
432 static char *choose_iftype_name(nfacct_rule_s *rule)
433 {
434         return strlen(rule->ifname) != 0 ? rule->ifname :
435                 get_iftype_name(rule->iftype);
436 }
437
438 static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule)
439 {
440         stc_error_e ret = STC_ERROR_NONE;
441         iptables_rule_s iptables_rule;
442         memset(&iptables_rule, 0, sizeof(iptables_rule_s));
443
444         iptables_rule.nfacct_name = g_strdup(rule->name);
445         iptables_rule.ifname = g_strdup(rule->ifname);
446         iptables_rule.target = g_strdup(get_iptables_jump(rule->jump));
447         iptables_rule.chain = g_strdup(get_iptables_chain(rule->iotype));
448         iptables_rule.classid = rule->classid;
449         iptables_rule.direction = (rule->iotype & NFACCT_COUNTER_IN) ? 0 : 1;
450
451         if (rule->action == NFACCT_ACTION_DELETE) {
452                 /* delete interface rule */
453                 ret = iptables_remove(&iptables_rule);
454         } else {
455                 /* add interface rule */
456                 ret = iptables_add(&iptables_rule);
457         }
458
459         g_free(iptables_rule.nfacct_name);
460         g_free(iptables_rule.ifname);
461         g_free(iptables_rule.target);
462         g_free(iptables_rule.chain);
463
464         return ret;
465 }
466
467 static stc_error_e produce_app_rule(nfacct_rule_s *rule)
468 {
469         if (rule == NULL)
470                 return STC_ERROR_INVALID_PARAMETER;
471
472         char *set_cmd = get_iptables_cmd(rule->action);
473         char *jump_cmd = get_iptables_jump(rule->jump);
474         char nfacct_buf[sizeof(NFACCT_NAME_MOD) +
475                 3*MAX_DEC_SIZE(int) + 4];
476         stc_error_e ret = STC_ERROR_NONE;
477
478         /* income part */
479         if (rule->iotype & NFACCT_COUNTER_IN) {
480                 rule->quota = rule->rcv_limit;
481                 rule->iotype = NFACCT_COUNTER_IN;
482                 generate_counter_name(rule);
483
484                 /* to support quated counter we need nfacct,
485                  *      don't use it in case of just block without a limit
486                  *      iow, send_limit = 0 and rcv_limit 0 */
487                 if (rule->action != NFACCT_ACTION_DELETE) {
488                         ret = nfacct_send_del(rule);
489                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
490                                          "can't del quota counter");
491
492                         ret = nfacct_send_new(rule);
493                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
494                                          "can't set nfacct counter");
495                         keep_counter(rule);
496                 }
497
498                 /* we have a counter, let's key in a rule, drop in case of
499                  *  send_limit/rcv_limit */
500                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf), NFACCT_NAME_MOD,
501                                rule->name);
502                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
503                                  STC_ERROR_FAIL, "Not enought buffer");
504
505                 ret = exec_iptables_cmd(rule);
506                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
507                                  "Can't set conditional block for ingress"
508                                  " traffic, for classid %u, cmd %s, j %s",
509                                  rule->classid, set_cmd, jump_cmd);
510
511                 /* remove in any case */
512                 if (rule->action == NFACCT_ACTION_DELETE) {
513                         /* TODO here and everywhere should be not just a del,
514                          *      here should be get counted value and than
515                          *      set new counter with that value, but it's minor issue,
516                          *      due it's not clear when actual counters was stored,
517                          *      and based on which value settings made such decition */
518                         rule->iptables_rule = nfacct_send_del;
519                         set_finalize_flag(rule);
520                         nfacct_send_get(rule);
521                         ret = nfacct_send_del(rule);
522                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
523                                          "can't del quota counter");
524                 }
525         }
526
527         if (rule->iotype & NFACCT_COUNTER_OUT) {
528                 /* outcome part */
529                 rule->iotype = NFACCT_COUNTER_OUT;
530                 rule->quota = rule->send_limit;
531                 generate_counter_name(rule);
532                 if (rule->action != NFACCT_ACTION_DELETE) {
533                         ret = nfacct_send_del(rule);
534                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
535                                          "can't del quota counter");
536
537                         ret = nfacct_send_new(rule);
538                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
539                                          "can't set quota counter");
540                         keep_counter(rule);
541                 }
542
543                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf), NFACCT_NAME_MOD,
544                                rule->name);
545                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
546                                  STC_ERROR_FAIL, "Not enought buffer");
547
548                 ret = exec_iptables_cmd(rule);
549                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
550                                  "Can't set conditional block for engress"
551                                  " traffic, for classid %u, cmd %s, j %s",
552                                  rule->classid, set_cmd, jump_cmd);
553
554                 if (rule->action == NFACCT_ACTION_DELETE) {
555                         rule->iptables_rule = nfacct_send_del;
556                         /* not effective, it's better to replace
557                          * set_finalize_flag by set_property,
558                          * due keep_counter it necessary only for
559                          * setting iptables_rule */
560                         set_finalize_flag(rule);
561                         nfacct_send_get(rule);
562                         ret = nfacct_send_del(rule);
563                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
564                                          "can't del quota counter");
565                 }
566         }
567         return STC_ERROR_NONE;
568 }
569
570 static stc_error_e produce_iface_rule(nfacct_rule_s *rule)
571 {
572         if (rule == NULL)
573                 return STC_ERROR_INVALID_PARAMETER;
574
575         char *set_cmd = get_iptables_cmd(rule->action);
576         char *jump_cmd = get_iptables_jump(rule->jump);
577         char nfacct_buf[sizeof(NFACCT_NAME_MOD) +
578                 3*MAX_DEC_SIZE(int) + 4];
579         stc_error_e ret;
580
581         rule->classid = 0;
582
583         if (rule->iotype & NFACCT_COUNTER_IN) {
584                 /* income part */
585                 rule->iotype = NFACCT_COUNTER_IN;
586                 rule->quota = rule->rcv_limit;
587                 generate_counter_name(rule);
588
589                 if (rule->action != NFACCT_ACTION_DELETE) {
590                         /* send delete comman in case of creation,
591                          * because nfacct doesn't reset value for nfacct quota
592                          * in case of quota existing */
593                         ret = nfacct_send_del(rule);
594                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
595                                          "can't del quota counter");
596
597                         ret = nfacct_send_new(rule);
598                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
599                                          "can't set quota counter");
600                         keep_counter(rule);
601                 }
602
603                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf),
604                                NFACCT_NAME_MOD, rule->name);
605                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
606                                  STC_ERROR_FAIL, "Not enought buffer");
607
608                 ret = exec_iptables_cmd(rule);
609                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
610                                  "Can't set conditional block for ingress"
611                                  " traffic, for iftype %d, cmd %s, j %s",
612                                  rule->iftype, set_cmd, jump_cmd);
613
614                 /* for tethering */
615                 if (rule->intend == NFACCT_WARN ||
616                     rule->intend == NFACCT_BLOCK) {
617                         /* RULE_IFACE_OUT is not a misprint here */
618                         nfacct_rule_direction temp_iotype = rule->iotype;
619
620                         rule->iotype = NFACCT_COUNTER_FORWARD;
621                         ret = exec_iptables_cmd(rule);
622                         rule->iotype = temp_iotype;
623                         ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
624                                          "Can't set forward rule for ingress "
625                                          "traffic, for iftype %d, cmd %s, j %s",
626                                          rule->iftype, set_cmd, jump_cmd);
627                 }
628                 /* tethering */
629
630                 if (rule->action == NFACCT_ACTION_DELETE) {
631                         rule->iptables_rule = nfacct_send_del;
632                         set_finalize_flag(rule);
633                         nfacct_send_get(rule);
634                         ret = nfacct_send_del(rule);
635                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
636                                          "can't del quota counter");
637                 }
638         }
639
640         if (rule->iotype & NFACCT_COUNTER_OUT) {
641                 /* outcome part */
642                 rule->iotype = NFACCT_COUNTER_OUT;
643                 rule->quota = rule->send_limit;
644                 generate_counter_name(rule);
645
646                 if (rule->action != NFACCT_ACTION_DELETE) {
647                         /* send delete comman in case of creation,
648                          * because nfacct doesn't reset value for nfacct quota
649                          * in case of quota existing */
650                         ret = nfacct_send_del(rule);
651                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
652                                          "can't del quota counter");
653
654                         ret = nfacct_send_new(rule);
655                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
656                                          "can't set quota counter");
657                         keep_counter(rule);
658                 }
659
660                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf),
661                                NFACCT_NAME_MOD, rule->name);
662                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
663                                  STC_ERROR_FAIL, "Not enough buffer");
664
665                 ret = exec_iptables_cmd(rule);
666                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
667                                  "Can't set conditional block for "
668                                  "engress traffic, for iftype %d, cmd %s, j %s",
669                                  rule->iftype, set_cmd, jump_cmd);
670
671                 /* for tethering  */
672                 if (rule->intend == NFACCT_WARN ||
673                     rule->intend == NFACCT_BLOCK) {
674                         nfacct_rule_direction temp_iotype = rule->iotype;
675
676                         rule->iotype = NFACCT_COUNTER_OUT;
677                         ret = exec_iptables_cmd(rule);
678                         rule->iotype = temp_iotype;
679                         ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
680                                          "Can't set forward rule for engress "
681                                          "traffic, for iftype %d, cmd %s, j %s",
682                                          rule->iftype, set_cmd, jump_cmd);
683                 }
684                 /* tethering  */
685
686                 if (rule->action == NFACCT_ACTION_DELETE) {
687                         rule->iptables_rule = nfacct_send_del;
688                         set_finalize_flag(rule);
689                         nfacct_send_get(rule);
690                         ret = nfacct_send_del(rule);
691                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
692                                          "can't del quota counter");
693                 }
694         }
695         return STC_ERROR_NONE;
696 }
697
698 stc_error_e produce_net_rule(nfacct_rule_s *rule)
699 {
700         stc_error_e ret = STC_ERROR_NONE;
701
702         if (rule == NULL)
703                 return STC_ERROR_INVALID_PARAMETER;
704
705         if (rule->action == NFACCT_ACTION_APPEND &&
706             rule->intend == NFACCT_WARN &&
707             !rule->send_limit && !rule->rcv_limit)
708                 return STC_ERROR_NONE;
709
710         if (rule->classid != STC_ALL_APP_CLASSID &&
711             rule->classid != STC_TETHERING_APP_CLASSID &&
712             rule->classid != STC_TOTAL_DATACALL_CLASSID &&
713             rule->classid != STC_TOTAL_WIFI_CLASSID &&
714             rule->classid != STC_TOTAL_BLUETOOTH_CLASSID &&
715             rule->classid != STC_TOTAL_IPV4_CLASSID &&
716             rule->classid != STC_TOTAL_IPV6_CLASSID)
717                 ret = produce_app_rule(rule);
718         else
719                 ret = produce_iface_rule(rule);
720
721         return ret;
722 }
723
724 void generate_counter_name(nfacct_rule_s *counter)
725 {
726         char warn_symbol = 'c';
727         if (!strlen(counter->ifname)) {
728                 char *iftype_name = get_iftype_name(counter->iftype);
729                 /* trace counter name, maybe name was already generated */
730                 ret_msg_if(iftype_name == NULL,
731                            "Can't get interface name for counter %s, iftype %d)!",
732                            counter->name, counter->iftype);
733                 STRING_SAVE_COPY(counter->ifname, iftype_name);
734         }
735
736         if (counter->intend  == NFACCT_WARN)
737                 warn_symbol = 'w';
738         else if (counter->intend  == NFACCT_BLOCK)
739                 warn_symbol = 'r';
740         snprintf(counter->name, NFACCT_NAME_MAX, "%c%d_%d_%d_%s",
741                  warn_symbol, counter->iotype, counter->iftype,
742                  counter->classid, counter->ifname);
743 }
744