xref: /aosp_15_r20/external/iptables/extensions/libxt_helper.c (revision a71a954618bbadd4a345637e5edcf36eec826889)
1 #include <stdio.h>
2 #include <xtables.h>
3 #include <linux/netfilter/xt_helper.h>
4 
5 enum {
6 	O_HELPER = 0,
7 };
8 
helper_help(void)9 static void helper_help(void)
10 {
11 	printf(
12 "helper match options:\n"
13 "[!] --helper string        Match helper identified by string\n");
14 }
15 
16 static const struct xt_option_entry helper_opts[] = {
17 	{.name = "helper", .id = O_HELPER, .type = XTTYPE_STRING,
18 	 .flags = XTOPT_MAND | XTOPT_INVERT | XTOPT_PUT,
19 	 XTOPT_POINTER(struct xt_helper_info, name)},
20 	XTOPT_TABLEEND,
21 };
22 
helper_parse(struct xt_option_call * cb)23 static void helper_parse(struct xt_option_call *cb)
24 {
25 	struct xt_helper_info *info = cb->data;
26 
27 	xtables_option_parse(cb);
28 	if (cb->invert)
29 		info->invert = 1;
30 }
31 
32 static void
helper_print(const void * ip,const struct xt_entry_match * match,int numeric)33 helper_print(const void *ip, const struct xt_entry_match *match, int numeric)
34 {
35 	const struct xt_helper_info *info = (const void *)match->data;
36 
37 	printf(" helper match %s\"%s\"", info->invert ? "! " : "", info->name);
38 }
39 
helper_save(const void * ip,const struct xt_entry_match * match)40 static void helper_save(const void *ip, const struct xt_entry_match *match)
41 {
42 	const struct xt_helper_info *info = (const void *)match->data;
43 
44 	printf("%s --helper", info->invert ? " !" : "");
45 	xtables_save_string(info->name);
46 }
47 
helper_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)48 static int helper_xlate(struct xt_xlate *xl,
49 			const struct xt_xlate_mt_params *params)
50 {
51 	const struct xt_helper_info *info = (const void *)params->match->data;
52 
53 	xt_xlate_add(xl, "ct helper%s \"%s\"",
54 		     info->invert ? " !=" : "", info->name);
55 
56 	return 1;
57 }
58 
59 static struct xtables_match helper_match = {
60 	.family		= NFPROTO_UNSPEC,
61 	.name		= "helper",
62 	.version	= XTABLES_VERSION,
63 	.size		= XT_ALIGN(sizeof(struct xt_helper_info)),
64 	.help		= helper_help,
65 	.print		= helper_print,
66 	.save		= helper_save,
67 	.x6_parse	= helper_parse,
68 	.x6_options	= helper_opts,
69 	.xlate		= helper_xlate,
70 };
71 
_init(void)72 void _init(void)
73 {
74 	xtables_register_match(&helper_match);
75 }
76