xtables.h 16 KB

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