1 /* Shared library add-on to iptables to add NFMARK matching support. */
10 #include <linux/netfilter/xt_mark.h>
16 static void mark_mt_help(void)
19 "mark match options:\n"
20 "[!] --mark value[/mask] Match nfmark value with optional mask\n");
23 static const struct option mark_mt_opts[] = {
24 {.name = "mark", .has_arg = true, .val = '1'},
28 static int mark_mt_parse(int c, char **argv, int invert, unsigned int *flags,
29 const void *entry, struct xt_entry_match **match)
31 struct xt_mark_mtinfo1 *info = (void *)(*match)->data;
32 unsigned int mark, mask = ~0U;
36 case '1': /* --mark */
37 param_act(P_ONLY_ONCE, "mark", "--mark", *flags & F_MARK);
38 if (!strtonum(optarg, &end, &mark, 0, ~0U))
39 param_act(P_BAD_VALUE, "mark", "--mark", optarg);
41 if (!strtonum(end + 1, &end, &mask, 0, ~0U))
42 param_act(P_BAD_VALUE, "mark", "--mark", optarg);
44 param_act(P_BAD_VALUE, "mark", "--mark", optarg);
56 /* Function which parses command options; returns true if it
59 mark_parse(int c, char **argv, int invert, unsigned int *flags,
60 const void *entry, struct xt_entry_match **match)
62 struct xt_mark_info *markinfo = (struct xt_mark_info *)(*match)->data;
67 check_inverse(optarg, &invert, &optind, 0);
68 markinfo->mark = strtoul(optarg, &end, 0);
70 markinfo->mask = strtoul(end+1, &end, 0);
72 markinfo->mask = 0xffffffff;
73 if (*end != '\0' || end == optarg)
74 exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
86 static void print_mark(unsigned int mark, unsigned int mask)
88 if (mask != 0xffffffffU)
89 printf("0x%x/0x%x ", mark, mask);
91 printf("0x%x ", mark);
94 static void mark_mt_check(unsigned int flags)
97 exit_error(PARAMETER_PROBLEM,
98 "mark match: The --mark option is required");
102 mark_mt_print(const void *ip, const struct xt_entry_match *match, int numeric)
104 const struct xt_mark_mtinfo1 *info = (const void *)match->data;
106 printf("mark match ");
109 print_mark(info->mark, info->mask);
112 /* Prints out the matchinfo. */
114 mark_print(const void *ip, const struct xt_entry_match *match, int numeric)
116 struct xt_mark_info *info = (struct xt_mark_info *)match->data;
118 printf("MARK match ");
123 print_mark(info->mark, info->mask);
126 static void mark_mt_save(const void *ip, const struct xt_entry_match *match)
128 const struct xt_mark_mtinfo1 *info = (const void *)match->data;
134 print_mark(info->mark, info->mask);
137 /* Saves the union ipt_matchinfo in parsable form to stdout. */
139 mark_save(const void *ip, const struct xt_entry_match *match)
141 struct xt_mark_info *info = (struct xt_mark_info *)match->data;
147 print_mark(info->mark, info->mask);
150 static struct xtables_match mark_match = {
154 .version = XTABLES_VERSION,
155 .size = XT_ALIGN(sizeof(struct xt_mark_info)),
156 .userspacesize = XT_ALIGN(sizeof(struct xt_mark_info)),
157 .help = mark_mt_help,
159 .final_check = mark_mt_check,
162 .extra_opts = mark_mt_opts,
165 static struct xtables_match mark_mt_reg = {
166 .version = XTABLES_VERSION,
170 .size = XT_ALIGN(sizeof(struct xt_mark_mtinfo1)),
171 .userspacesize = XT_ALIGN(sizeof(struct xt_mark_mtinfo1)),
172 .help = mark_mt_help,
173 .parse = mark_mt_parse,
174 .final_check = mark_mt_check,
175 .print = mark_mt_print,
176 .save = mark_mt_save,
177 .extra_opts = mark_mt_opts,
182 xtables_register_match(&mark_match);
183 xtables_register_match(&mark_mt_reg);