blob: 5d782dab98562201f66b99e4b2bffe92d1985504 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#ifndef _LIBIPTC_H
2#define _LIBIPTC_H
3/* Library which manipulates filtering rules. */
4
5#include <linux/types.h>
6#include <libiptc/ipt_kernel_headers.h>
7#ifdef __cplusplus
8# include <climits>
9#else
10# include <limits.h> /* INT_MAX in ip_tables.h */
11#endif
12#include <linux/netfilter_ipv4/ip_tables.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18#ifndef IPT_MIN_ALIGN
19/* ipt_entry has pointers and u_int64_t's in it, so if you align to
20 it, you'll also align to any crazy matches and targets someone
21 might write */
22#define IPT_MIN_ALIGN (__alignof__(struct ipt_entry))
23#endif
24
25#define IPT_ALIGN(s) (((s) + ((IPT_MIN_ALIGN)-1)) & ~((IPT_MIN_ALIGN)-1))
26
27struct iptc_handle;
28
29typedef char ipt_chainlabel[32];
30
31#define IPTC_LABEL_ACCEPT "ACCEPT"
32#define IPTC_LABEL_DROP "DROP"
33#define IPTC_LABEL_QUEUE "QUEUE"
34#define IPTC_LABEL_RETURN "RETURN"
35
36/* Does this chain exist? */
37int iptc_is_chain(const char *chain, struct iptc_handle *const handle);
38
39/* Take a snapshot of the rules. Returns NULL on error. */
40struct iptc_handle *iptc_init(const char *tablename);
41
42/* Cleanup after iptc_init(). */
43void iptc_free(struct iptc_handle *h);
44
45/* Iterator functions to run through the chains. Returns NULL at end. */
46const char *iptc_first_chain(struct iptc_handle *handle);
47const char *iptc_next_chain(struct iptc_handle *handle);
48
49/* Get first rule in the given chain: NULL for empty chain. */
50const struct ipt_entry *iptc_first_rule(const char *chain,
51 struct iptc_handle *handle);
52
53/* Returns NULL when rules run out. */
54const struct ipt_entry *iptc_next_rule(const struct ipt_entry *prev,
55 struct iptc_handle *handle);
56
57/* Returns a pointer to the target name of this entry. */
58const char *iptc_get_target(const struct ipt_entry *e,
59 struct iptc_handle *handle);
60
61/* Is this a built-in chain? */
62int iptc_builtin(const char *chain, struct iptc_handle *const handle);
63
64/* Get the policy of a given built-in chain */
65const char *iptc_get_policy(const char *chain,
66 struct ipt_counters *counter,
67 struct iptc_handle *handle);
68
69/* These functions return TRUE for OK or 0 and set errno. If errno ==
70 0, it means there was a version error (ie. upgrade libiptc). */
71/* Rule numbers start at 1 for the first rule. */
72
73/* Insert the entry `e' in chain `chain' into position `rulenum'. */
74int iptc_insert_entry(const ipt_chainlabel chain,
75 const struct ipt_entry *e,
76 unsigned int rulenum,
77 struct iptc_handle *handle);
78
79/* Atomically replace rule `rulenum' in `chain' with `e'. */
80int iptc_replace_entry(const ipt_chainlabel chain,
81 const struct ipt_entry *e,
82 unsigned int rulenum,
83 struct iptc_handle *handle);
84
85/* Append entry `e' to chain `chain'. Equivalent to insert with
86 rulenum = length of chain. */
87int iptc_append_entry(const ipt_chainlabel chain,
88 const struct ipt_entry *e,
89 struct iptc_handle *handle);
90
91/* Delete the first rule in `chain' which matches `e', subject to
92 matchmask (array of length == origfw) */
93int iptc_delete_entry(const ipt_chainlabel chain,
94 const struct ipt_entry *origfw,
95 unsigned char *matchmask,
96 struct iptc_handle *handle);
97
98/* Delete the rule in position `rulenum' in `chain'. */
99int iptc_delete_num_entry(const ipt_chainlabel chain,
100 unsigned int rulenum,
101 struct iptc_handle *handle);
102
103/* Check the packet `e' on chain `chain'. Returns the verdict, or
104 NULL and sets errno. */
105const char *iptc_check_packet(const ipt_chainlabel chain,
106 struct ipt_entry *entry,
107 struct iptc_handle *handle);
108
109/* Flushes the entries in the given chain (ie. empties chain). */
110int iptc_flush_entries(const ipt_chainlabel chain,
111 struct iptc_handle *handle);
112
113/* Zeroes the counters in a chain. */
114int iptc_zero_entries(const ipt_chainlabel chain,
115 struct iptc_handle *handle);
116
117/* Creates a new chain. */
118int iptc_create_chain(const ipt_chainlabel chain,
119 struct iptc_handle *handle);
120
121/* Deletes a chain. */
122int iptc_delete_chain(const ipt_chainlabel chain,
123 struct iptc_handle *handle);
124
125/* Renames a chain. */
126int iptc_rename_chain(const ipt_chainlabel oldname,
127 const ipt_chainlabel newname,
128 struct iptc_handle *handle);
129
130/* Sets the policy on a built-in chain. */
131int iptc_set_policy(const ipt_chainlabel chain,
132 const ipt_chainlabel policy,
133 struct ipt_counters *counters,
134 struct iptc_handle *handle);
135
136/* Get the number of references to this chain */
137int iptc_get_references(unsigned int *ref,
138 const ipt_chainlabel chain,
139 struct iptc_handle *handle);
140
141/* read packet and byte counters for a specific rule */
142struct ipt_counters *iptc_read_counter(const ipt_chainlabel chain,
143 unsigned int rulenum,
144 struct iptc_handle *handle);
145
146/* zero packet and byte counters for a specific rule */
147int iptc_zero_counter(const ipt_chainlabel chain,
148 unsigned int rulenum,
149 struct iptc_handle *handle);
150
151/* set packet and byte counters for a specific rule */
152int iptc_set_counter(const ipt_chainlabel chain,
153 unsigned int rulenum,
154 struct ipt_counters *counters,
155 struct iptc_handle *handle);
156
157/* Makes the actual changes. */
158int iptc_commit(struct iptc_handle *handle);
159
160/* Get raw socket. */
161int iptc_get_raw_socket(void);
162
163/* Translates errno numbers into more human-readable form than strerror. */
164const char *iptc_strerror(int err);
165
166extern void dump_entries(struct iptc_handle *const);
167
168#ifdef __cplusplus
169}
170#endif
171
172
173#endif /* _LIBIPTC_H */