blob: b8248fa03d11e08a389f4523defa3d7dd88deb42 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#ifndef _XTABLES_H
2#define _XTABLES_H
3
4/*
5 * Changing any structs/functions may incur a needed change
6 * in libxtables_vcurrent/vage too.
7 */
8
9#include <sys/socket.h> /* PF_* */
10#include <sys/types.h>
11#include <limits.h>
12#include <stdbool.h>
13#include <netinet/in.h>
14#include <net/if.h>
15#include <linux/types.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/x_tables.h>
18
19#ifndef IPPROTO_SCTP
20#define IPPROTO_SCTP 132
21#endif
22#ifndef IPPROTO_DCCP
23#define IPPROTO_DCCP 33
24#endif
25#ifndef IPPROTO_MH
26# define IPPROTO_MH 135
27#endif
28#ifndef IPPROTO_UDPLITE
29#define IPPROTO_UDPLITE 136
30#endif
31
32#define XTABLES_VERSION "libxtables.so.@libxtables_vmajor@"
33#define XTABLES_VERSION_CODE @libxtables_vmajor@
34
35struct in_addr;
36
37/* Include file for additions: new matches and targets. */
38struct xtables_match
39{
40 /*
41 * ABI/API version this module requires. Must be first member,
42 * as the rest of this struct may be subject to ABI changes.
43 */
44 const char *version;
45
46 struct xtables_match *next;
47
48 const char *name;
49
50 /* Revision of match (0 by default). */
51 u_int8_t revision;
52
53 u_int16_t family;
54
55 /* Size of match data. */
56 size_t size;
57
58 /* Size of match data relevent for userspace comparison purposes */
59 size_t userspacesize;
60
61 /* Function which prints out usage message. */
62 void (*help)(void);
63
64 /* Initialize the match. */
65 void (*init)(struct xt_entry_match *m);
66
67 /* Function which parses command options; returns true if it
68 ate an option */
69 /* entry is struct ipt_entry for example */
70 int (*parse)(int c, char **argv, int invert, unsigned int *flags,
71 const void *entry,
72 struct xt_entry_match **match);
73
74 /* Final check; exit if not ok. */
75 void (*final_check)(unsigned int flags);
76
77 /* Prints out the match iff non-NULL: put space at end */
78 /* ip is struct ipt_ip * for example */
79 void (*print)(const void *ip,
80 const struct xt_entry_match *match, int numeric);
81
82 /* Saves the match info in parsable form to stdout. */
83 /* ip is struct ipt_ip * for example */
84 void (*save)(const void *ip, const struct xt_entry_match *match);
85
86 /* Pointer to list of extra command-line options */
87 const struct option *extra_opts;
88
89 /* Ignore these men behind the curtain: */
90 unsigned int option_offset;
91 struct xt_entry_match *m;
92 unsigned int mflags;
93};
94
95struct xtables_target
96{
97 /*
98 * ABI/API version this module requires. Must be first member,
99 * as the rest of this struct may be subject to ABI changes.
100 */
101 const char *version;
102
103 struct xtables_target *next;
104
105
106 const char *name;
107
108 /* Revision of target (0 by default). */
109 u_int8_t revision;
110
111 u_int16_t family;
112
113
114 /* Size of target data. */
115 size_t size;
116
117 /* Size of target data relevent for userspace comparison purposes */
118 size_t userspacesize;
119
120 /* Function which prints out usage message. */
121 void (*help)(void);
122
123 /* Initialize the target. */
124 void (*init)(struct xt_entry_target *t);
125
126 /* Function which parses command options; returns true if it
127 ate an option */
128 /* entry is struct ipt_entry for example */
129 int (*parse)(int c, char **argv, int invert, unsigned int *flags,
130 const void *entry,
131 struct xt_entry_target **targetinfo);
132
133 /* Final check; exit if not ok. */
134 void (*final_check)(unsigned int flags);
135
136 /* Prints out the target iff non-NULL: put space at end */
137 void (*print)(const void *ip,
138 const struct xt_entry_target *target, int numeric);
139
140 /* Saves the targinfo in parsable form to stdout. */
141 void (*save)(const void *ip,
142 const struct xt_entry_target *target);
143
144 /* Pointer to list of extra command-line options */
145 const struct option *extra_opts;
146
147 /* Ignore these men behind the curtain: */
148 unsigned int option_offset;
149 struct xt_entry_target *t;
150 unsigned int tflags;
151 unsigned int used;
152};
153
154struct xtables_rule_match {
155 struct xtables_rule_match *next;
156 struct xtables_match *match;
157 /* Multiple matches of the same type: the ones before
158 the current one are completed from parsing point of view */
159 bool completed;
160};
161
162/**
163 * struct xtables_pprot -
164 *
165 * A few hardcoded protocols for 'all' and in case the user has no
166 * /etc/protocols.
167 */
168struct xtables_pprot {
169 const char *name;
170 u_int8_t num;
171};
172
173enum xtables_tryload {
174 XTF_DONT_LOAD,
175 XTF_DURING_LOAD,
176 XTF_TRY_LOAD,
177 XTF_LOAD_MUST_SUCCEED,
178};
179
180enum xtables_exittype {
181 OTHER_PROBLEM = 1,
182 PARAMETER_PROBLEM,
183 VERSION_PROBLEM,
184 RESOURCE_PROBLEM,
185 XTF_ONLY_ONCE,
186 XTF_NO_INVERT,
187 XTF_BAD_VALUE,
188 XTF_ONE_ACTION,
189};
190
191struct xtables_globals
192{
193 unsigned int option_offset;
194 const char *program_name, *program_version;
195 struct option *orig_opts;
196 struct option *opts;
197 void (*exit_err)(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
198};
199
200extern const char *xtables_modprobe_program;
201extern struct xtables_match *xtables_matches;
202extern struct xtables_target *xtables_targets;
203
204extern void xtables_init(void);
205extern void xtables_set_nfproto(uint8_t);
206extern void *xtables_calloc(size_t, size_t);
207extern void *xtables_malloc(size_t);
208
209extern int xtables_insmod(const char *, const char *, bool);
210extern int xtables_load_ko(const char *, bool);
211extern int xtables_set_params(struct xtables_globals *xtp);
212extern void xtables_set_revision(char *name, u_int8_t revision);
213extern void xtables_free_opts(int reset_offset);
214extern struct option *xtables_merge_options(struct option *oldopts,
215 const struct option *newopts, unsigned int *option_offset);
216
217extern int xtables_init_all(struct xtables_globals *xtp, uint8_t nfproto);
218extern struct xtables_match *xtables_find_match(const char *name,
219 enum xtables_tryload, struct xtables_rule_match **match);
220extern struct xtables_target *xtables_find_target(const char *name,
221 enum xtables_tryload);
222
223/* Your shared library should call one of these. */
224extern void xtables_register_match(struct xtables_match *me);
225extern void xtables_register_target(struct xtables_target *me);
226
227extern bool xtables_strtoul(const char *, char **, unsigned long *,
228 unsigned long, unsigned long);
229extern bool xtables_strtoui(const char *, char **, unsigned int *,
230 unsigned int, unsigned int);
231extern int xtables_service_to_port(const char *name, const char *proto);
232extern u_int16_t xtables_parse_port(const char *port, const char *proto);
233extern void
234xtables_parse_interface(const char *arg, char *vianame, unsigned char *mask);
235
236/* this is a special 64bit data type that is 8-byte aligned */
237#define aligned_u64 u_int64_t __attribute__((aligned(8)))
238
239int xtables_check_inverse(const char option[], int *invert,
240 int *my_optind, int argc);
241extern struct xtables_globals *xt_params;
242#define xtables_error (xt_params->exit_err)
243
244extern void xtables_param_act(unsigned int, const char *, ...);
245
246extern const char *xtables_ipaddr_to_numeric(const struct in_addr *);
247extern const char *xtables_ipaddr_to_anyname(const struct in_addr *);
248extern const char *xtables_ipmask_to_numeric(const struct in_addr *);
249extern struct in_addr *xtables_numeric_to_ipaddr(const char *);
250extern struct in_addr *xtables_numeric_to_ipmask(const char *);
251extern void xtables_ipparse_any(const char *, struct in_addr **,
252 struct in_addr *, unsigned int *);
253
254extern struct in6_addr *xtables_numeric_to_ip6addr(const char *);
255extern const char *xtables_ip6addr_to_numeric(const struct in6_addr *);
256extern const char *xtables_ip6addr_to_anyname(const struct in6_addr *);
257extern const char *xtables_ip6mask_to_numeric(const struct in6_addr *);
258extern void xtables_ip6parse_any(const char *, struct in6_addr **,
259 struct in6_addr *, unsigned int *);
260
261/**
262 * Print the specified value to standard output, quoting dangerous
263 * characters if required.
264 */
265extern void xtables_save_string(const char *value);
266
267#ifdef NO_SHARED_LIBS
268# ifdef _INIT
269# undef _init
270# define _init _INIT
271# endif
272 extern void init_extensions(void);
273#else
274# define _init __attribute__((constructor)) _INIT
275#endif
276
277extern const struct xtables_pprot xtables_chain_protos[];
278extern u_int16_t xtables_parse_protocol(const char *s);
279
280#ifdef XTABLES_INTERNAL
281
282/* Shipped modules rely on this... */
283
284# ifndef ARRAY_SIZE
285# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
286# endif
287
288extern void _init(void);
289
290#endif
291
292#endif /* _XTABLES_H */