xref: /aosp_15_r20/external/iptables/extensions/libebt_mark_m.c (revision a71a954618bbadd4a345637e5edcf36eec826889)
1 /* ebt_mark_m
2  *
3  * Authors:
4  * Bart De Schuymer <[email protected]>
5  *
6  * July, 2002
7  *
8  * Adapted by Arturo Borrero Gonzalez <[email protected]>
9  * to use libxtables for ebtables-compat in 2015.
10  */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <getopt.h>
16 #include <xtables.h>
17 #include <linux/netfilter_bridge/ebt_mark_m.h>
18 
19 #define MARK '1'
20 
21 static const struct option brmark_m_opts[] = {
22 	{ .name = "mark",	.has_arg = true, .val = MARK },
23 	XT_GETOPT_TABLEEND,
24 };
25 
brmark_m_print_help(void)26 static void brmark_m_print_help(void)
27 {
28 	printf(
29 "mark option:\n"
30 "--mark    [!] [value][/mask]: Match nfmask value (see man page)\n");
31 }
32 
brmark_m_init(struct xt_entry_match * match)33 static void brmark_m_init(struct xt_entry_match *match)
34 {
35 	struct ebt_mark_m_info *info = (struct ebt_mark_m_info *)match->data;
36 
37 	info->mark = 0;
38 	info->mask = 0;
39 	info->invert = 0;
40 	info->bitmask = 0;
41 }
42 
43 #define OPT_MARK 0x01
44 static int
brmark_m_parse(int c,char ** argv,int invert,unsigned int * flags,const void * entry,struct xt_entry_match ** match)45 brmark_m_parse(int c, char **argv, int invert, unsigned int *flags,
46 	       const void *entry, struct xt_entry_match **match)
47 {
48 	struct ebt_mark_m_info *info = (struct ebt_mark_m_info *)
49 				       (*match)->data;
50 	char *end;
51 
52 	switch (c) {
53 	case MARK:
54 		if (invert)
55 			info->invert = 1;
56 		info->mark = strtoul(optarg, &end, 0);
57 		info->bitmask = EBT_MARK_AND;
58 		if (*end == '/') {
59 			if (end == optarg)
60 				info->bitmask = EBT_MARK_OR;
61 			info->mask = strtoul(end+1, &end, 0);
62 		} else {
63 			info->mask = 0xffffffff;
64 		}
65 		if (*end != '\0' || end == optarg)
66 			xtables_error(PARAMETER_PROBLEM, "Bad mark value '%s'",
67 				      optarg);
68 		break;
69 	default:
70 		return 0;
71 	}
72 
73 	*flags |= info->bitmask;
74 	return 1;
75 }
76 
brmark_m_final_check(unsigned int flags)77 static void brmark_m_final_check(unsigned int flags)
78 {
79 	if (!flags)
80 		xtables_error(PARAMETER_PROBLEM,
81 			      "You must specify proper arguments");
82 }
83 
brmark_m_print(const void * ip,const struct xt_entry_match * match,int numeric)84 static void brmark_m_print(const void *ip, const struct xt_entry_match *match,
85 			   int numeric)
86 {
87 	struct ebt_mark_m_info *info = (struct ebt_mark_m_info *)match->data;
88 
89 	printf("--mark ");
90 	if (info->invert)
91 		printf("! ");
92 	if (info->bitmask == EBT_MARK_OR)
93 		printf("/0x%lx ", info->mask);
94 	else if (info->mask != 0xffffffff)
95 		printf("0x%lx/0x%lx ", info->mark, info->mask);
96 	else
97 		printf("0x%lx ", info->mark);
98 }
99 
brmark_m_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)100 static int brmark_m_xlate(struct xt_xlate *xl,
101 			  const struct xt_xlate_mt_params *params)
102 {
103 	const struct ebt_mark_m_info *info = (const void*)params->match->data;
104 	enum xt_op op = XT_OP_EQ;
105 
106 	if (info->invert)
107 		op = XT_OP_NEQ;
108 
109 	xt_xlate_add(xl, "meta mark ");
110 
111 	if (info->bitmask == EBT_MARK_OR) {
112 		xt_xlate_add(xl, "and 0x%x %s0 ", (uint32_t)info->mask,
113 			     info->invert ? "" : "!= ");
114 	} else if (info->mask != 0xffffffffU) {
115 		xt_xlate_add(xl, "and 0x%x %s0x%x ", (uint32_t)info->mask,
116 			   op == XT_OP_EQ ? "" : "!= ", (uint32_t)info->mark);
117 	} else {
118 		xt_xlate_add(xl, "%s0x%x ",
119 			   op == XT_OP_EQ ? "" : "!= ", (uint32_t)info->mark);
120 	}
121 
122 	return 1;
123 }
124 static struct xtables_match brmark_m_match = {
125 	.name		= "mark_m",
126 	.revision	= 0,
127 	.version	= XTABLES_VERSION,
128 	.family		= NFPROTO_BRIDGE,
129 	.size		= XT_ALIGN(sizeof(struct ebt_mark_m_info)),
130 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_mark_m_info)),
131 	.init		= brmark_m_init,
132 	.help		= brmark_m_print_help,
133 	.parse		= brmark_m_parse,
134 	.final_check	= brmark_m_final_check,
135 	.print		= brmark_m_print,
136 	.xlate		= brmark_m_xlate,
137 	.extra_opts	= brmark_m_opts,
138 };
139 
_init(void)140 void _init(void)
141 {
142 	xtables_register_match(&brmark_m_match);
143 }
144