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
= UINT32_MAX
;
36 case '1': /* --mark */
37 xtables_param_act(XTF_ONLY_ONCE
, "mark", "--mark", *flags
& F_MARK
);
38 if (!xtables_strtoui(optarg
, &end
, &mark
, 0, UINT32_MAX
))
39 xtables_param_act(XTF_BAD_VALUE
, "mark", "--mark", optarg
);
41 if (!xtables_strtoui(end
+ 1, &end
, &mask
, 0, UINT32_MAX
))
42 xtables_param_act(XTF_BAD_VALUE
, "mark", "--mark", optarg
);
44 xtables_param_act(XTF_BAD_VALUE
, "mark", "--mark", optarg
);
57 mark_parse(int c
, char **argv
, int invert
, unsigned int *flags
,
58 const void *entry
, struct xt_entry_match
**match
)
60 struct xt_mark_info
*markinfo
= (struct xt_mark_info
*)(*match
)->data
;
65 xtables_check_inverse(optarg
, &invert
, &optind
, 0);
66 markinfo
->mark
= strtoul(optarg
, &end
, 0);
68 markinfo
->mask
= strtoul(end
+1, &end
, 0);
70 markinfo
->mask
= 0xffffffff;
71 if (*end
!= '\0' || end
== optarg
)
72 xtables_error(PARAMETER_PROBLEM
, "Bad MARK value \"%s\"", optarg
);
84 static void print_mark(unsigned int mark
, unsigned int mask
)
86 if (mask
!= 0xffffffffU
)
87 printf("0x%x/0x%x ", mark
, mask
);
89 printf("0x%x ", mark
);
92 static void mark_mt_check(unsigned int flags
)
95 xtables_error(PARAMETER_PROBLEM
,
96 "mark match: The --mark option is required");
100 mark_mt_print(const void *ip
, const struct xt_entry_match
*match
, int numeric
)
102 const struct xt_mark_mtinfo1
*info
= (const void *)match
->data
;
104 printf("mark match ");
107 print_mark(info
->mark
, info
->mask
);
111 mark_print(const void *ip
, const struct xt_entry_match
*match
, int numeric
)
113 const struct xt_mark_info
*info
= (const void *)match
->data
;
115 printf("MARK match ");
120 print_mark(info
->mark
, info
->mask
);
123 static void mark_mt_save(const void *ip
, const struct xt_entry_match
*match
)
125 const struct xt_mark_mtinfo1
*info
= (const void *)match
->data
;
131 print_mark(info
->mark
, info
->mask
);
135 mark_save(const void *ip
, const struct xt_entry_match
*match
)
137 const struct xt_mark_info
*info
= (const void *)match
->data
;
143 print_mark(info
->mark
, info
->mask
);
146 static struct xtables_match mark_match
= {
150 .version
= XTABLES_VERSION
,
151 .size
= XT_ALIGN(sizeof(struct xt_mark_info
)),
152 .userspacesize
= XT_ALIGN(sizeof(struct xt_mark_info
)),
153 .help
= mark_mt_help
,
155 .final_check
= mark_mt_check
,
158 .extra_opts
= mark_mt_opts
,
161 static struct xtables_match mark_mt_reg
= {
162 .version
= XTABLES_VERSION
,
166 .size
= XT_ALIGN(sizeof(struct xt_mark_mtinfo1
)),
167 .userspacesize
= XT_ALIGN(sizeof(struct xt_mark_mtinfo1
)),
168 .help
= mark_mt_help
,
169 .parse
= mark_mt_parse
,
170 .final_check
= mark_mt_check
,
171 .print
= mark_mt_print
,
172 .save
= mark_mt_save
,
173 .extra_opts
= mark_mt_opts
,
178 xtables_register_match(&mark_match
);
179 xtables_register_match(&mark_mt_reg
);