xtables.h revision 2f655ede64e07a861e3ec50150f572ed98755013
1#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 <stddef.h>
14#include <stdint.h>
15#include <netinet/in.h>
16#include <net/if.h>
17#include <linux/types.h>
18#include <linux/netfilter.h>
19#include <linux/netfilter/x_tables.h>
20
21#ifndef IPPROTO_SCTP
22#define IPPROTO_SCTP 132
23#endif
24#ifndef IPPROTO_DCCP
25#define IPPROTO_DCCP 33
26#endif
27#ifndef IPPROTO_MH
28#	define IPPROTO_MH 135
29#endif
30#ifndef IPPROTO_UDPLITE
31#define IPPROTO_UDPLITE	136
32#endif
33
34#include <xtables-version.h>
35
36struct in_addr;
37
38/*
39 * .size is here so that there is a somewhat reasonable check
40 * against the chosen .type.
41 */
42#define XTOPT_POINTER(stype, member) \
43	.ptroff = offsetof(stype, member), \
44	.size = sizeof(((stype *)NULL)->member)
45#define XTOPT_TABLEEND {.name = NULL}
46
47/**
48 * Select the format the input has to conform to, as well as the target type
49 * (area pointed to with XTOPT_POINTER). Note that the storing is not always
50 * uniform. @cb->val will be populated with as much as there is space, i.e.
51 * exactly 2 items for ranges, but the target area can receive more values
52 * (e.g. in case of ranges), or less values (e.g. %XTTYPE_HOSTMASK).
53 *
54 * %XTTYPE_NONE:	option takes no argument
55 * %XTTYPE_UINT*:	standard integer
56 * %XTTYPE_UINT*RC:	colon-separated range of standard integers
57 * %XTTYPE_DOUBLE:	double-precision floating point number
58 * %XTTYPE_STRING:	arbitrary string
59 * %XTTYPE_TOSMASK:	8-bit TOS value with optional mask
60 * %XTTYPE_MARKMASK32:	32-bit mark with optional mask
61 * %XTTYPE_SYSLOGLEVEL:	syslog level by name or number
62 * %XTTYPE_HOST:	one host or address (ptr: union nf_inet_addr)
63 * %XTTYPE_HOSTMASK:	one host or address, with an optional prefix length
64 * 			(ptr: union nf_inet_addr; only host portion is stored)
65 * %XTTYPE_PROTOCOL:	protocol number/name from /etc/protocols (ptr: uint8_t)
66 * %XTTYPE_PORT:	16-bit port name or number (supports %XTOPT_NBO)
67 * %XTTYPE_PORTRC:	colon-separated port range (names acceptable),
68 * 			(supports %XTOPT_NBO)
69 * %XTTYPE_PLEN:	prefix length
70 * %XTTYPE_PLENMASK:	prefix length (ptr: union nf_inet_addr)
71 * %XTTYPE_ETHERMAC:	Ethernet MAC address in hex form
72 */
73enum xt_option_type {
74	XTTYPE_NONE,
75	XTTYPE_UINT8,
76	XTTYPE_UINT16,
77	XTTYPE_UINT32,
78	XTTYPE_UINT64,
79	XTTYPE_UINT8RC,
80	XTTYPE_UINT16RC,
81	XTTYPE_UINT32RC,
82	XTTYPE_UINT64RC,
83	XTTYPE_DOUBLE,
84	XTTYPE_STRING,
85	XTTYPE_TOSMASK,
86	XTTYPE_MARKMASK32,
87	XTTYPE_SYSLOGLEVEL,
88	XTTYPE_HOST,
89	XTTYPE_HOSTMASK,
90	XTTYPE_PROTOCOL,
91	XTTYPE_PORT,
92	XTTYPE_PORTRC,
93	XTTYPE_PLEN,
94	XTTYPE_PLENMASK,
95	XTTYPE_ETHERMAC,
96};
97
98/**
99 * %XTOPT_INVERT:	option is invertible (usable with !)
100 * %XTOPT_MAND:		option is mandatory
101 * %XTOPT_MULTI:	option may be specified multiple times
102 * %XTOPT_PUT:		store value into memory at @ptroff
103 * %XTOPT_NBO:		store value in network-byte order
104 * 			(only certain XTTYPEs recognize this)
105 */
106enum xt_option_flags {
107	XTOPT_INVERT = 1 << 0,
108	XTOPT_MAND   = 1 << 1,
109	XTOPT_MULTI  = 1 << 2,
110	XTOPT_PUT    = 1 << 3,
111	XTOPT_NBO    = 1 << 4,
112};
113
114/**
115 * @name:	name of option
116 * @type:	type of input and validation method, see %XTTYPE_*
117 * @id:		unique number (within extension) for option, 0-31
118 * @excl:	bitmask of flags that cannot be used with this option
119 * @also:	bitmask of flags that must be used with this option
120 * @flags:	bitmask of option flags, see %XTOPT_*
121 * @ptroff:	offset into private structure for member
122 * @size:	size of the item pointed to by @ptroff; this is a safeguard
123 * @min:	lowest allowed value (for singular integral types)
124 * @max:	highest allowed value (for singular integral types)
125 */
126struct xt_option_entry {
127	const char *name;
128	enum xt_option_type type;
129	unsigned int id, excl, also, flags;
130	unsigned int ptroff;
131	size_t size;
132	unsigned int min, max;
133};
134
135/**
136 * @arg:	input from command line
137 * @ext_name:	name of extension currently being processed
138 * @entry:	current option being processed
139 * @data:	per-extension kernel data block
140 * @xflags:	options of the extension that have been used
141 * @invert:	whether option was used with !
142 * @nvals:	number of results in uXX_multi
143 * @val:	parsed result
144 * @udata:	per-extension private scratch area
145 * 		(cf. xtables_{match,target}->udata_size)
146 */
147struct xt_option_call {
148	const char *arg, *ext_name;
149	const struct xt_option_entry *entry;
150	void *data;
151	unsigned int xflags;
152	bool invert;
153	uint8_t nvals;
154	union {
155		uint8_t u8, u8_range[2], syslog_level, protocol;
156		uint16_t u16, u16_range[2], port, port_range[2];
157		uint32_t u32, u32_range[2];
158		uint64_t u64, u64_range[2];
159		double dbl;
160		struct {
161			union nf_inet_addr haddr, hmask;
162			uint8_t hlen;
163		};
164		struct {
165			uint8_t tos_value, tos_mask;
166		};
167		struct {
168			uint32_t mark, mask;
169		};
170		uint8_t ethermac[6];
171	} val;
172	/* Wished for a world where the ones below were gone: */
173	union {
174		struct xt_entry_match **match;
175		struct xt_entry_target **target;
176	};
177	void *xt_entry;
178	void *udata;
179};
180
181/**
182 * @ext_name:	name of extension currently being processed
183 * @data:	per-extension (kernel) data block
184 * @udata:	per-extension private scratch area
185 * 		(cf. xtables_{match,target}->udata_size)
186 * @xflags:	options of the extension that have been used
187 */
188struct xt_fcheck_call {
189	const char *ext_name;
190	void *data, *udata;
191	unsigned int xflags;
192};
193
194/**
195 * A "linear"/linked-list based name<->id map, for files similar to
196 * /etc/iproute2/.
197 */
198struct xtables_lmap {
199	char *name;
200	int id;
201	struct xtables_lmap *next;
202};
203
204/* Include file for additions: new matches and targets. */
205struct xtables_match
206{
207	/*
208	 * ABI/API version this module requires. Must be first member,
209	 * as the rest of this struct may be subject to ABI changes.
210	 */
211	const char *version;
212
213	struct xtables_match *next;
214
215	const char *name;
216	const char *real_name;
217
218	/* Revision of match (0 by default). */
219	u_int8_t revision;
220
221	u_int16_t family;
222
223	/* Size of match data. */
224	size_t size;
225
226	/* Size of match data relevent for userspace comparison purposes */
227	size_t userspacesize;
228
229	/* Function which prints out usage message. */
230	void (*help)(void);
231
232	/* Initialize the match. */
233	void (*init)(struct xt_entry_match *m);
234
235	/* Function which parses command options; returns true if it
236           ate an option */
237	/* entry is struct ipt_entry for example */
238	int (*parse)(int c, char **argv, int invert, unsigned int *flags,
239		     const void *entry,
240		     struct xt_entry_match **match);
241
242	/* Final check; exit if not ok. */
243	void (*final_check)(unsigned int flags);
244
245	/* Prints out the match iff non-NULL: put space at end */
246	/* ip is struct ipt_ip * for example */
247	void (*print)(const void *ip,
248		      const struct xt_entry_match *match, int numeric);
249
250	/* Saves the match info in parsable form to stdout. */
251	/* ip is struct ipt_ip * for example */
252	void (*save)(const void *ip, const struct xt_entry_match *match);
253
254	/* Pointer to list of extra command-line options */
255	const struct option *extra_opts;
256
257	/* New parser */
258	void (*x6_parse)(struct xt_option_call *);
259	void (*x6_fcheck)(struct xt_fcheck_call *);
260	const struct xt_option_entry *x6_options;
261
262	/* Size of per-extension instance extra "global" scratch space */
263	size_t udata_size;
264
265	/* Ignore these men behind the curtain: */
266	void *udata;
267	unsigned int option_offset;
268	struct xt_entry_match *m;
269	unsigned int mflags;
270	unsigned int loaded; /* simulate loading so options are merged properly */
271};
272
273struct xtables_target
274{
275	/*
276	 * ABI/API version this module requires. Must be first member,
277	 * as the rest of this struct may be subject to ABI changes.
278	 */
279	const char *version;
280
281	struct xtables_target *next;
282
283
284	const char *name;
285
286	/* Real target behind this, if any. */
287	const char *real_name;
288
289	/* Revision of target (0 by default). */
290	u_int8_t revision;
291
292	u_int16_t family;
293
294
295	/* Size of target data. */
296	size_t size;
297
298	/* Size of target data relevent for userspace comparison purposes */
299	size_t userspacesize;
300
301	/* Function which prints out usage message. */
302	void (*help)(void);
303
304	/* Initialize the target. */
305	void (*init)(struct xt_entry_target *t);
306
307	/* Function which parses command options; returns true if it
308           ate an option */
309	/* entry is struct ipt_entry for example */
310	int (*parse)(int c, char **argv, int invert, unsigned int *flags,
311		     const void *entry,
312		     struct xt_entry_target **targetinfo);
313
314	/* Final check; exit if not ok. */
315	void (*final_check)(unsigned int flags);
316
317	/* Prints out the target iff non-NULL: put space at end */
318	void (*print)(const void *ip,
319		      const struct xt_entry_target *target, int numeric);
320
321	/* Saves the targinfo in parsable form to stdout. */
322	void (*save)(const void *ip,
323		     const struct xt_entry_target *target);
324
325	/* Pointer to list of extra command-line options */
326	const struct option *extra_opts;
327
328	/* New parser */
329	void (*x6_parse)(struct xt_option_call *);
330	void (*x6_fcheck)(struct xt_fcheck_call *);
331	const struct xt_option_entry *x6_options;
332
333	size_t udata_size;
334
335	/* Ignore these men behind the curtain: */
336	void *udata;
337	unsigned int option_offset;
338	struct xt_entry_target *t;
339	unsigned int tflags;
340	unsigned int used;
341	unsigned int loaded; /* simulate loading so options are merged properly */
342};
343
344struct xtables_rule_match {
345	struct xtables_rule_match *next;
346	struct xtables_match *match;
347	/* Multiple matches of the same type: the ones before
348	   the current one are completed from parsing point of view */
349	bool completed;
350};
351
352/**
353 * struct xtables_pprot -
354 *
355 * A few hardcoded protocols for 'all' and in case the user has no
356 * /etc/protocols.
357 */
358struct xtables_pprot {
359	const char *name;
360	u_int8_t num;
361};
362
363enum xtables_tryload {
364	XTF_DONT_LOAD,
365	XTF_DURING_LOAD,
366	XTF_TRY_LOAD,
367	XTF_LOAD_MUST_SUCCEED,
368};
369
370enum xtables_exittype {
371	OTHER_PROBLEM = 1,
372	PARAMETER_PROBLEM,
373	VERSION_PROBLEM,
374	RESOURCE_PROBLEM,
375	XTF_ONLY_ONCE,
376	XTF_NO_INVERT,
377	XTF_BAD_VALUE,
378	XTF_ONE_ACTION,
379};
380
381struct xtables_globals
382{
383	unsigned int option_offset;
384	const char *program_name, *program_version;
385	struct option *orig_opts;
386	struct option *opts;
387	void (*exit_err)(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
388};
389
390#define XT_GETOPT_TABLEEND {.name = NULL, .has_arg = false}
391
392#ifdef __cplusplus
393extern "C" {
394#endif
395
396extern const char *xtables_modprobe_program;
397extern struct xtables_match *xtables_matches;
398extern struct xtables_target *xtables_targets;
399
400extern void xtables_init(void);
401extern void xtables_set_nfproto(uint8_t);
402extern void *xtables_calloc(size_t, size_t);
403extern void *xtables_malloc(size_t);
404extern void *xtables_realloc(void *, size_t);
405
406extern int xtables_insmod(const char *, const char *, bool);
407extern int xtables_load_ko(const char *, bool);
408extern int xtables_set_params(struct xtables_globals *xtp);
409extern void xtables_free_opts(int reset_offset);
410extern struct option *xtables_merge_options(struct option *origopts,
411	struct option *oldopts, const struct option *newopts,
412	unsigned int *option_offset);
413
414extern int xtables_init_all(struct xtables_globals *xtp, uint8_t nfproto);
415extern struct xtables_match *xtables_find_match(const char *name,
416	enum xtables_tryload, struct xtables_rule_match **match);
417extern struct xtables_target *xtables_find_target(const char *name,
418	enum xtables_tryload);
419
420extern void xtables_rule_matches_free(struct xtables_rule_match **matches);
421
422/* Your shared library should call one of these. */
423extern void xtables_register_match(struct xtables_match *me);
424extern void xtables_register_matches(struct xtables_match *, unsigned int);
425extern void xtables_register_target(struct xtables_target *me);
426extern void xtables_register_targets(struct xtables_target *, unsigned int);
427
428extern bool xtables_strtoul(const char *, char **, uintmax_t *,
429	uintmax_t, uintmax_t);
430extern bool xtables_strtoui(const char *, char **, unsigned int *,
431	unsigned int, unsigned int);
432extern int xtables_service_to_port(const char *name, const char *proto);
433extern u_int16_t xtables_parse_port(const char *port, const char *proto);
434extern void
435xtables_parse_interface(const char *arg, char *vianame, unsigned char *mask);
436
437/* this is a special 64bit data type that is 8-byte aligned */
438#define aligned_u64 u_int64_t __attribute__((aligned(8)))
439
440extern struct xtables_globals *xt_params;
441#define xtables_error (xt_params->exit_err)
442
443extern void xtables_param_act(unsigned int, const char *, ...);
444
445extern const char *xtables_ipaddr_to_numeric(const struct in_addr *);
446extern const char *xtables_ipaddr_to_anyname(const struct in_addr *);
447extern const char *xtables_ipmask_to_numeric(const struct in_addr *);
448extern struct in_addr *xtables_numeric_to_ipaddr(const char *);
449extern struct in_addr *xtables_numeric_to_ipmask(const char *);
450extern int xtables_ipmask_to_cidr(const struct in_addr *);
451extern void xtables_ipparse_any(const char *, struct in_addr **,
452	struct in_addr *, unsigned int *);
453extern void xtables_ipparse_multiple(const char *, struct in_addr **,
454	struct in_addr **, unsigned int *);
455
456extern struct in6_addr *xtables_numeric_to_ip6addr(const char *);
457extern const char *xtables_ip6addr_to_numeric(const struct in6_addr *);
458extern const char *xtables_ip6addr_to_anyname(const struct in6_addr *);
459extern const char *xtables_ip6mask_to_numeric(const struct in6_addr *);
460extern int xtables_ip6mask_to_cidr(const struct in6_addr *);
461extern void xtables_ip6parse_any(const char *, struct in6_addr **,
462	struct in6_addr *, unsigned int *);
463extern void xtables_ip6parse_multiple(const char *, struct in6_addr **,
464	struct in6_addr **, unsigned int *);
465
466/**
467 * Print the specified value to standard output, quoting dangerous
468 * characters if required.
469 */
470extern void xtables_save_string(const char *value);
471
472#define FMT_NUMERIC		0x0001
473#define FMT_NOCOUNTS		0x0002
474#define FMT_KILOMEGAGIGA	0x0004
475#define FMT_OPTIONS		0x0008
476#define FMT_NOTABLE		0x0010
477#define FMT_NOTARGET		0x0020
478#define FMT_VIA			0x0040
479#define FMT_NONEWLINE		0x0080
480#define FMT_LINENUMBERS		0x0100
481
482#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
483                        | FMT_NUMERIC | FMT_NOTABLE)
484#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
485
486extern void xtables_print_num(uint64_t number, unsigned int format);
487
488#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
489#	ifdef _INIT
490#		undef _init
491#		define _init _INIT
492#	endif
493	extern void init_extensions(void);
494	extern void init_extensions4(void);
495	extern void init_extensions6(void);
496#else
497#	define _init __attribute__((constructor)) _INIT
498#endif
499
500extern const struct xtables_pprot xtables_chain_protos[];
501extern u_int16_t xtables_parse_protocol(const char *s);
502
503/* kernel revision handling */
504extern int kernel_version;
505extern void get_kernel_version(void);
506#define LINUX_VERSION(x,y,z)	(0x10000*(x) + 0x100*(y) + z)
507#define LINUX_VERSION_MAJOR(x)	(((x)>>16) & 0xFF)
508#define LINUX_VERSION_MINOR(x)	(((x)>> 8) & 0xFF)
509#define LINUX_VERSION_PATCH(x)	( (x)      & 0xFF)
510
511/* xtoptions.c */
512extern void xtables_option_metavalidate(const char *,
513					const struct xt_option_entry *);
514extern struct option *xtables_options_xfrm(struct option *, struct option *,
515					   const struct xt_option_entry *,
516					   unsigned int *);
517extern void xtables_option_parse(struct xt_option_call *);
518extern void xtables_option_tpcall(unsigned int, char **, bool,
519				  struct xtables_target *, void *);
520extern void xtables_option_mpcall(unsigned int, char **, bool,
521				  struct xtables_match *, void *);
522extern void xtables_option_tfcall(struct xtables_target *);
523extern void xtables_option_mfcall(struct xtables_match *);
524extern void xtables_options_fcheck(const char *, unsigned int,
525				   const struct xt_option_entry *);
526
527extern struct xtables_lmap *xtables_lmap_init(const char *);
528extern void xtables_lmap_free(struct xtables_lmap *);
529extern int xtables_lmap_name2id(const struct xtables_lmap *, const char *);
530extern const char *xtables_lmap_id2name(const struct xtables_lmap *, int);
531
532#ifdef XTABLES_INTERNAL
533
534/* Shipped modules rely on this... */
535
536#	ifndef ARRAY_SIZE
537#		define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
538#	endif
539
540extern void _init(void);
541
542#endif
543
544#ifdef __cplusplus
545} /* extern "C" */
546#endif
547
548#endif /* _XTABLES_H */
549