58a36cf07e8d14deef19e7879a1ed23207d09b77
[platform/core/connectivity/stc-iptables.git] / src / helper / helper-iptables.h
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License version 2 as
6  *  published by the Free Software Foundation.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17
18 /*
19  * Manage iptables rule using libiptc
20  */
21 #ifndef __STC_HELPER_IPTABLES_H__
22 #define __STC_HELPER_IPTABLES_H__
23
24 #include <libiptc/libiptc.h>
25
26 #define RULE_SIZE     64
27
28 typedef enum {
29         IPTABLES_CHAIN_INPUT,
30         IPTABLES_CHAIN_OUTPUT
31 } iptables_chain_e;
32
33 typedef enum {
34         IPTABLES_DIRECTION_NONE,
35         IPTABLES_DIRECTION_IN,
36         IPTABLES_DIRECTION_OUT
37 } iptables_direction_e;
38
39 typedef enum {
40         IPTABLES_IP_NONE,
41         IPTABLES_IP_SINGLE,
42         IPTABLES_IP_MASK,
43         IPTABLES_IP_RANGE
44 } iptables_ip_type_e;
45
46 typedef enum {
47         IPTABLES_PORT_NONE,
48         IPTABLES_PORT_SINGLE,
49         IPTABLES_PORT_RANGE
50 } iptables_port_type_e;
51
52 typedef enum {
53         IPTABLES_PROTOCOL_NONE,
54         IPTABLES_PROTOCOL_TCP,
55         IPTABLES_PROTOCOL_UDP,
56         IPTABLES_PROTOCOL_ICMP,
57         IPTABLES_PROTOCOL_ESP,
58         IPTABLES_PROTOCOL_AH,
59         IPTABLES_PROTOCOL_SCTP,
60         IPTABLES_PROTOCOL_MH,
61         IPTABLES_PROTOCOL_ALL,
62 } iptables_protocol_type_e;
63
64 typedef enum {
65         IPTABLES_ACTION_NONE,
66         IPTABLES_ACTION_ACCEPT,
67         IPTABLES_ACTION_DROP,
68         IPTABLES_ACTION_LOG,
69         IPTABLES_ACTION_NFLOG,
70 } iptables_target_action_e;
71
72 typedef struct {
73         char *chain;
74         iptables_direction_e direction;
75         iptables_ip_type_e s_ip_type;
76         iptables_ip_type_e d_ip_type;
77         iptables_port_type_e s_port_type;
78         iptables_port_type_e d_port_type;
79         iptables_protocol_type_e protocol;
80         struct in_addr s_ip1;
81         struct in_addr s_ip2;
82         struct in_addr d_ip1;
83         struct in_addr d_ip2;
84         unsigned int s_port1;
85         unsigned int s_port2;
86         unsigned int d_port1;
87         unsigned int d_port2;
88         char *ifname;
89         int classid;
90         char *nfacct_name;
91         char *target;
92         iptables_target_action_e target_type;
93         unsigned char log_level;
94         char *log_prefix;
95         unsigned int nflog_group;
96         char *nflog_prefix;
97         unsigned int nflog_range;
98         unsigned int nflog_threshold;
99 } iptables_rule_s;
100
101 /**
102  * @desc This function adds a new iptables rule.
103  * @return 0 on success and negative value if error.
104  */
105 int iptables_add_rule(iptables_rule_s *rule);
106
107 /**
108  * @desc This function inserts a new iptables rule.
109  * @return 0 on success and negative value if error.
110  */
111 int iptables_insert_rule(iptables_rule_s *rule);
112
113 /**
114  * @desc This function removes already set iptables rule.
115  * @return 0 on success and negative value if error.
116  */
117 int iptables_remove_rule(iptables_rule_s *rule);
118
119 /**
120  * @desc This function adds a new iptables chain.
121  * @return 0 on success and negative value if error.
122  */
123 int iptables_add_chain(const char *chain);
124
125 /**
126  * @desc This function removes already set iptables chain.
127  * @return 0 on success and negative value if error.
128  */
129 int iptables_remove_chain(const char *chain);
130
131 /**
132  * @desc This function flushes all iptables rules in chain.
133  * @return 0 on success and negative value if error.
134  */
135 int iptables_flush_chain(const char *chain);
136
137 #endif /*__STC_HELPER_IPTABLES_H__*/