90364d34681c025d926a603cb211b8bb324a1933
[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         //LCOV_EXCL_START
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         //LCOV_EXCL_STOP
325
326         io_part = strtok_r(name, "_", &save_ptr);
327         if (io_part != NULL)
328                 cnt->iotype = convert_to_iotype(atoi(io_part + 1));
329         else
330                 return false;
331
332         iftype_part = strtok_r(NULL, "_", &save_ptr);
333         if (iftype_part != NULL)
334                 cnt->iftype = convert_to_iftype(atoi(iftype_part));
335         else
336                 return false;
337
338         classid_part = strtok_r(NULL, "_", &save_ptr);
339         if (classid_part != NULL)
340                 cnt->classid = atoi(classid_part);
341         else {
342                 cnt->classid = STC_ALL_APP_CLASSID;
343                 return cnt->intend == NFACCT_BLOCK ? true : false;
344         }
345
346         ifname_part = strtok_r(NULL, "\0", &save_ptr);
347         if (ifname_part != NULL)
348                 STRING_SAVE_COPY(cnt->ifname, ifname_part);
349         else
350                 return false;
351
352         return true;
353 }
354
355 static void _process_answer(struct netlink_serialization_params *params)
356 {
357         struct rtattr *na;
358         struct rtattr *attr_list[__NFACCT_MAX] = {0};
359         struct counter_arg *carg = params->carg;
360         struct genl *ans = params->ans;;
361         struct nlmsghdr *nlhdr = &ans->n;
362         int len = GENLMSG_PAYLOAD(nlhdr);
363         int ans_len = carg->ans_len;
364
365         if (len == 0)
366                 return;
367
368         /* parse reply message */
369         na = (struct rtattr *)GENLMSG_DATA(ans);
370
371         while (NLMSG_OK(nlhdr, ans_len)) {
372                 fill_attribute_list(attr_list, NFACCT_MAX,
373                                     na, len);
374                 if (!attr_list[NFACCT_NAME] ||
375                     !attr_list[NFACCT_BYTES])
376                         goto next;
377                 params->eval_attr(attr_list, carg);
378
379 next:
380                 nlhdr = NLMSG_NEXT(nlhdr, ans_len);
381                 if (ans_len < 0)
382                         break;
383                 na = (struct rtattr *)GENLMSG_DATA(nlhdr);
384         }
385
386         if (params->post_eval_attr)
387                 params->post_eval_attr(carg);
388 }
389
390 netlink_serialization_command *
391 netlink_create_command(struct netlink_serialization_params *params)
392 {
393         static netlink_serialization_command command = {0,};
394         command.deserialize_answer = _process_answer;
395         command.params = *params;
396         return &command;
397 }
398
399 static char *get_iptables_cmd(const nfacct_rule_action action)
400 {
401         if (action == NFACCT_ACTION_APPEND)
402                 return APPEND;
403         else if (action == NFACCT_ACTION_DELETE)
404                 return DELETE;
405         else if (action == NFACCT_ACTION_INSERT)
406                 return INSERT;
407
408         return "";
409 }
410
411 static char *get_iptables_chain(const nfacct_rule_direction iotype)
412 {
413         if (iotype == NFACCT_COUNTER_IN)
414                 return STC_IN_CHAIN;
415         else if (iotype == NFACCT_COUNTER_OUT)
416                 return STC_OUT_CHAIN;
417         else if (iotype == NFACCT_COUNTER_FORWARD) //LCOV_EXCL_LINE
418                 return STC_FRWD_CHAIN; //LCOV_EXCL_LINE
419
420         return "";
421 }
422
423 static char *get_iptables_jump(const nfacct_rule_jump jump)
424 {
425         if (jump == NFACCT_JUMP_ACCEPT)
426                 return ACCEPT_RULE;
427         else if (jump == NFACCT_JUMP_REJECT)
428                 return REJECT_RULE;
429
430         return "";
431 }
432
433 /*
434 static char *choose_iftype_name(nfacct_rule_s *rule)
435 {
436         return strlen(rule->ifname) != 0 ? rule->ifname :
437                 get_iftype_name(rule->iftype);
438 }
439 */
440
441 static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule)
442 {
443         stc_error_e ret = STC_ERROR_NONE;
444         iptables_ip_type_e iptype;
445         iptables_rule_s iptables_rule;
446         memset(&iptables_rule, 0, sizeof(iptables_rule_s));
447
448         iptables_rule.nfacct_name = g_strdup(rule->name);
449         iptables_rule.ifname = g_strdup(rule->ifname);
450         iptables_rule.target = g_strdup(get_iptables_jump(rule->jump));
451         iptables_rule.chain = g_strdup(get_iptables_chain(rule->iotype));
452         iptables_rule.classid = rule->classid;
453         iptables_rule.direction = (rule->iotype & NFACCT_COUNTER_IN) ?
454                                         IPTABLES_DIRECTION_IN : IPTABLES_DIRECTION_OUT;
455         iptype = (iptables_ip_type_e)rule->iptype;
456
457         if (rule->action == NFACCT_ACTION_DELETE) {
458                 /* delete interface rule */
459                 ret = iptables_remove(&iptables_rule, iptype);
460         } else {
461                 /* add interface rule */
462                 ret = iptables_add(&iptables_rule, iptype);
463         }
464
465         g_free(iptables_rule.nfacct_name);
466         g_free(iptables_rule.ifname);
467         g_free(iptables_rule.target);
468         g_free(iptables_rule.chain);
469
470         return ret;
471 }
472
473 static stc_error_e produce_app_rule(nfacct_rule_s *rule)
474 {
475         if (rule == NULL)
476                 return STC_ERROR_INVALID_PARAMETER;
477
478         char *set_cmd = get_iptables_cmd(rule->action);
479         char *jump_cmd = get_iptables_jump(rule->jump);
480         char nfacct_buf[sizeof(NFACCT_NAME_MOD) +
481                 3*MAX_DEC_SIZE(int) + 4 + 1];
482         stc_error_e ret = STC_ERROR_NONE;
483
484         /* income part */
485         if (rule->iotype & NFACCT_COUNTER_IN) {
486                 rule->quota = rule->rcv_limit;
487                 rule->iotype = NFACCT_COUNTER_IN;
488                 generate_counter_name(rule);
489
490                 /* to support quated counter we need nfacct,
491                  *      don't use it in case of just block without a limit
492                  *      iow, send_limit = 0 and rcv_limit 0 */
493                 if (rule->action != NFACCT_ACTION_DELETE) {
494                         ret = nfacct_send_del(rule);
495                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
496                                          "can't del quota counter");
497
498                         ret = nfacct_send_new(rule);
499                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
500                                          "can't set nfacct counter");
501                         keep_counter(rule);
502                 }
503
504                 /* we have a counter, let's key in a rule, drop in case of
505                  *  send_limit/rcv_limit */
506                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf), NFACCT_NAME_MOD,
507                                rule->name);
508                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
509                                  STC_ERROR_FAIL, "Not enought buffer");
510
511                 ret = exec_iptables_cmd(rule);
512                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
513                                  "Can't set conditional block for ingress"
514                                  " traffic, for classid %u, cmd %s, j %s",
515                                  rule->classid, set_cmd, jump_cmd);
516
517                 /* remove in any case */
518                 if (rule->action == NFACCT_ACTION_DELETE) {
519                         /* TODO here and everywhere should be not just a del,
520                          *      here should be get counted value and than
521                          *      set new counter with that value, but it's minor issue,
522                          *      due it's not clear when actual counters was stored,
523                          *      and based on which value settings made such decition */
524                         rule->iptables_rule = nfacct_send_del;
525                         set_finalize_flag(rule);
526                         nfacct_send_get(rule);
527                         ret = nfacct_send_del(rule);
528                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
529                                          "can't del quota counter");
530                 }
531         }
532
533         if (rule->iotype & NFACCT_COUNTER_OUT) {
534                 /* outcome part */
535                 rule->iotype = NFACCT_COUNTER_OUT;
536                 rule->quota = rule->send_limit;
537                 generate_counter_name(rule);
538                 if (rule->action != NFACCT_ACTION_DELETE) {
539                         ret = nfacct_send_del(rule);
540                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
541                                          "can't del quota counter");
542
543                         ret = nfacct_send_new(rule);
544                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
545                                          "can't set quota counter");
546                         keep_counter(rule);
547                 }
548
549                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf), NFACCT_NAME_MOD,
550                                rule->name);
551                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
552                                  STC_ERROR_FAIL, "Not enought buffer");
553
554                 ret = exec_iptables_cmd(rule);
555                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
556                                  "Can't set conditional block for engress"
557                                  " traffic, for classid %u, cmd %s, j %s",
558                                  rule->classid, set_cmd, jump_cmd);
559
560                 if (rule->action == NFACCT_ACTION_DELETE) {
561                         rule->iptables_rule = nfacct_send_del;
562                         /* not effective, it's better to replace
563                          * set_finalize_flag by set_property,
564                          * due keep_counter it necessary only for
565                          * setting iptables_rule */
566                         set_finalize_flag(rule);
567                         nfacct_send_get(rule);
568                         ret = nfacct_send_del(rule);
569                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
570                                          "can't del quota counter");
571                 }
572         }
573         return STC_ERROR_NONE;
574 }
575
576 static stc_error_e produce_iface_rule(nfacct_rule_s *rule)
577 {
578         if (rule == NULL)
579                 return STC_ERROR_INVALID_PARAMETER;
580
581         char *set_cmd = get_iptables_cmd(rule->action);
582         char *jump_cmd = get_iptables_jump(rule->jump);
583         char nfacct_buf[sizeof(NFACCT_NAME_MOD) +
584                 3*MAX_DEC_SIZE(int) + 4 + 1];
585         uint32_t classid = rule->classid;
586         stc_error_e ret;
587
588         if (rule->iotype & NFACCT_COUNTER_IN) {
589                 /* income part */
590                 rule->iotype = NFACCT_COUNTER_IN;
591                 rule->quota = rule->rcv_limit;
592                 generate_counter_name(rule);
593
594                 if (rule->action != NFACCT_ACTION_DELETE) {
595                         /* send delete comman in case of creation,
596                          * because nfacct doesn't reset value for nfacct quota
597                          * in case of quota existing */
598                         ret = nfacct_send_del(rule);
599                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
600                                          "can't del quota counter");
601
602                         ret = nfacct_send_new(rule);
603                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
604                                          "can't set quota counter");
605                         keep_counter(rule);
606                 }
607
608                 ret = snprintf(nfacct_buf, sizeof(nfacct_buf),
609                                NFACCT_NAME_MOD, rule->name);
610                 ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
611                                  STC_ERROR_FAIL, "Not enought buffer");
612
613                 classid = rule->classid;
614                 rule->classid = 0;
615
616                 ret = exec_iptables_cmd(rule);
617                 ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
618                                  "Can't set conditional block for ingress"
619                                  " traffic, for iftype %d, cmd %s, j %s",
620                                  rule->iftype, set_cmd, jump_cmd);
621
622                 //LCOV_EXCL_START
623                 /* for tethering */
624                 if (rule->intend == NFACCT_WARN ||
625                     rule->intend == NFACCT_BLOCK) {
626                         /* RULE_IFACE_OUT is not a misprint here */
627                         nfacct_rule_direction temp_iotype = rule->iotype;
628
629                         rule->iotype = NFACCT_COUNTER_FORWARD;
630                         ret = exec_iptables_cmd(rule);
631                         rule->iotype = temp_iotype;
632                         ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
633                                          "Can't set forward rule for ingress "
634                                          "traffic, for iftype %d, cmd %s, j %s",
635                                          rule->iftype, set_cmd, jump_cmd);
636                 }
637                 /* tethering */
638
639                 if (rule->action == NFACCT_ACTION_DELETE) {
640                         rule->iptables_rule = nfacct_send_del;
641                         set_finalize_flag(rule);
642                         nfacct_send_get(rule);
643                         ret = nfacct_send_del(rule);
644                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
645                                          "can't del quota counter");
646                 }
647                 //LCOV_EXCL_STOP
648         }
649
650         rule->classid = classid;
651
652         if (rule->iotype & NFACCT_COUNTER_OUT) {
653                 /* outcome part */
654                 rule->iotype = NFACCT_COUNTER_OUT;
655                 rule->quota = rule->send_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 enough 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 "
683                                  "engress 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                         nfacct_rule_direction temp_iotype = rule->iotype;
691
692                         rule->iotype = NFACCT_COUNTER_OUT;
693                         ret = exec_iptables_cmd(rule);
694                         rule->iotype = temp_iotype;
695                         ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
696                                          "Can't set forward rule for engress "
697                                          "traffic, for iftype %d, cmd %s, j %s",
698                                          rule->iftype, set_cmd, jump_cmd);
699                 }
700                 /* tethering  */
701
702                 if (rule->action == NFACCT_ACTION_DELETE) {
703                         rule->iptables_rule = nfacct_send_del;
704                         set_finalize_flag(rule);
705                         nfacct_send_get(rule);
706                         ret = nfacct_send_del(rule);
707                         ret_value_msg_if(ret != STC_ERROR_NONE, ret,
708                                          "can't del quota counter");
709                 }
710                 //LCOV_EXCL_STOP
711         }
712         return STC_ERROR_NONE;
713 }
714
715 stc_error_e produce_net_rule(nfacct_rule_s *rule)
716 {
717         stc_error_e ret = STC_ERROR_NONE;
718
719         if (rule == NULL)
720                 return STC_ERROR_INVALID_PARAMETER;
721
722         if (rule->action == NFACCT_ACTION_APPEND &&
723             rule->intend == NFACCT_WARN &&
724             !rule->send_limit && !rule->rcv_limit)
725                 return STC_ERROR_NONE;
726
727         if (rule->classid != STC_ALL_APP_CLASSID &&
728             rule->classid != STC_TETHERING_APP_CLASSID &&
729             rule->classid != STC_TOTAL_DATACALL_CLASSID &&
730             rule->classid != STC_TOTAL_WIFI_CLASSID &&
731             rule->classid != STC_TOTAL_BLUETOOTH_CLASSID &&
732             rule->classid != STC_TOTAL_IPV4_CLASSID &&
733             rule->classid != STC_TOTAL_IPV6_CLASSID)
734                 ret = produce_app_rule(rule);
735         else
736                 ret = produce_iface_rule(rule);
737
738         return ret;
739 }
740
741 void generate_counter_name(nfacct_rule_s *counter)
742 {
743         char warn_symbol = 'c';
744         if (!strlen(counter->ifname)) {
745                 char *iftype_name = get_iftype_name(counter->iftype);
746                 /* trace counter name, maybe name was already generated */
747                 ret_msg_if(iftype_name == NULL,
748                            "Can't get interface name for counter %s, iftype %d)!",
749                            counter->name, counter->iftype);
750                 STRING_SAVE_COPY(counter->ifname, iftype_name);
751         }
752
753         if (counter->intend  == NFACCT_WARN)
754                 warn_symbol = 'w';
755         else if (counter->intend  == NFACCT_BLOCK)
756                 warn_symbol = 'r';
757         else if (counter->intend  == NFACCT_ALLOW)
758                 warn_symbol = 'a';
759         snprintf(counter->name, NFACCT_NAME_MAX, "%c%d_%d_%d_%s",
760                  warn_symbol, counter->iotype, counter->iftype,
761                  counter->classid, counter->ifname);
762 }
763