Called appropriately waitpid() function
[platform/core/connectivity/stc-manager.git] / src / helper / helper-nfacct-rule.h
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 #ifndef __STC_NFACCT_RULE_H__
18 #define __STC_NFACCT_RULE_H__
19
20 #include "stc-db.h"
21
22 #include <stdbool.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include "helper-nl.h"
27
28 #define NFACCT_NAME_MAX 32
29
30 typedef enum {
31         NFACCT_COUNTER_UNKNOWN,
32         NFACCT_COUNTER_IN = (1 << 1),
33         NFACCT_COUNTER_OUT = (1 << 2),
34         NFACCT_COUNTER_LAST_ELEM
35 } nfacct_rule_direction;
36
37 typedef enum {
38         NFACCT_ACTION_UNKNOWN,
39         NFACCT_ACTION_APPEND,
40         NFACCT_ACTION_DELETE,
41         NFACCT_ACTION_INSERT,
42         NFACCT_ACTION_LAST_ELEM,
43 } nfacct_rule_action;
44
45 typedef enum {
46         NFACCT_JUMP_UNKNOWN,
47         NFACCT_JUMP_ACCEPT,
48         NFACCT_JUMP_REJECT,
49         NFACCT_JUMP_LAST_ELEM,
50 } nfacct_rule_jump;
51
52 typedef enum {
53         NFACCT_COUNTER,
54         NFACCT_WARN,
55         NFACCT_BLOCK,
56         NFACCT_TETH_COUNTER,
57         NFACCT_RULE_LAST_ELEM,
58 } nfacct_rule_intend;
59
60 enum nfnl_acct_flags {
61         NFACCT_F_QUOTA_PKTS     = (1 << 0),
62         NFACCT_F_QUOTA_BYTES    = (1 << 1),
63         NFACCT_F_OVERQUOTA      = (1 << 2), /* can't be set from userspace */
64 };
65
66 /**
67  * it's better to have
68  * base nfacct_rule with following fields:
69  *  name, ifname, pid, classid, iftype, intend, carg, iptables_rule
70  *
71  *  and inherited nfacct_rule_counter and nfacct_rule_restriction
72  *  with additional field:
73  *      quota, quota_id, roaming, rst_state
74  *
75  * But ANSI C doesn't support inheritance.
76  */
77 struct nfacct_rule {
78         char name[NFACCT_NAME_MAX];
79         char ifname[MAX_IFACE_LENGTH];
80
81         pid_t pid;
82         uint32_t classid;
83         stc_iface_type_e iftype;
84         nfacct_rule_direction iotype;
85         nfacct_rule_intend intend;
86         struct counter_arg *carg;
87         stc_error_e(*iptables_rule)(struct nfacct_rule *counter);
88         int64_t quota;
89         int quota_id;
90         stc_roaming_type_e roaming;
91         stc_restriction_state_e rst_state;
92
93         /**
94          * in most cases jump is evalutation based
95          * on intend, but not always
96          */
97         nfacct_rule_jump jump;
98 };
99
100 typedef struct nfacct_rule nfacct_rule_s;
101
102 struct counter_arg;
103
104 void generate_counter_name(nfacct_rule_s *counter);
105 bool recreate_counter_by_name(char *cnt_name, nfacct_rule_s *counter);
106
107 stc_error_e nfacct_send_get_all(struct counter_arg *carg);
108 stc_error_e nfacct_send_get_counters(struct counter_arg *carg,
109                                      const char *name);
110 stc_error_e nfacct_send_get(nfacct_rule_s *rule);
111 stc_error_e nfacct_send_del(nfacct_rule_s *counter);
112
113 stc_error_e exec_iptables_cmd(const char *cmd_buf, pid_t *pid);
114 stc_error_e produce_net_rule(nfacct_rule_s *rule,
115                              const int64_t send_limit,
116                              const int64_t rcv_limit,
117                              const nfacct_rule_action action,
118                              const nfacct_rule_jump jump,
119                              const nfacct_rule_direction iotype);
120
121 netlink_serialization_command *
122 netlink_create_command(struct netlink_serialization_params *params);
123
124 #endif /* __STC_NFACCT_RULE_H__ */
125