1 /* Shared library add-on to iptables to add MARK target support. */
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter/xt_MARK.h>
16 /* Function which prints out usage message. */
17 static void MARK_help(void)
20 "MARK target options:\n"
21 " --set-mark value Set nfmark value\n"
22 " --and-mark value Binary AND the nfmark with value\n"
23 " --or-mark value Binary OR the nfmark with value\n");
26 static const struct option MARK_opts[] = {
27 { "set-mark", 1, NULL, '1' },
28 { "and-mark", 1, NULL, '2' },
29 { "or-mark", 1, NULL, '3' },
33 static const struct option mark_tg_opts[] = {
34 {.name = "set-xmark", .has_arg = true, .val = 'X'},
35 {.name = "set-mark", .has_arg = true, .val = '='},
36 {.name = "and-mark", .has_arg = true, .val = '&'},
37 {.name = "or-mark", .has_arg = true, .val = '|'},
38 {.name = "xor-mark", .has_arg = true, .val = '^'},
42 static void mark_tg_help(void)
45 "MARK target options:\n"
46 " --set-xmark value[/mask] Clear bits in mask and XOR value into nfmark\n"
47 " --set-mark value[/mask] Clear bits in mask and OR value into nfmark\n"
48 " --and-mark bits Binary AND the nfmark with bits\n"
49 " --or-mark bits Binary OR the nfmark with bits\n"
50 " --xor-mask bits Binary XOR the nfmark with bits\n"
54 /* Function which parses command options; returns true if it
57 MARK_parse_v0(int c, char **argv, int invert, unsigned int *flags,
58 const void *entry, struct xt_entry_target **target)
60 struct xt_mark_target_info *markinfo
61 = (struct xt_mark_target_info *)(*target)->data;
65 if (string_to_number_l(optarg, 0, 0,
67 exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
69 exit_error(PARAMETER_PROBLEM,
70 "MARK target: Can't specify --set-mark twice");
74 exit_error(PARAMETER_PROBLEM,
75 "MARK target: kernel too old for --and-mark");
77 exit_error(PARAMETER_PROBLEM,
78 "MARK target: kernel too old for --or-mark");
86 static void MARK_check(unsigned int flags)
89 exit_error(PARAMETER_PROBLEM,
90 "MARK target: Parameter --set/and/or-mark"
94 /* Function which parses command options; returns true if it
97 MARK_parse_v1(int c, char **argv, int invert, unsigned int *flags,
98 const void *entry, struct xt_entry_target **target)
100 struct xt_mark_target_info_v1 *markinfo
101 = (struct xt_mark_target_info_v1 *)(*target)->data;
105 markinfo->mode = XT_MARK_SET;
108 markinfo->mode = XT_MARK_AND;
111 markinfo->mode = XT_MARK_OR;
117 if (string_to_number_l(optarg, 0, 0, &markinfo->mark))
118 exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
121 exit_error(PARAMETER_PROBLEM,
122 "MARK target: Can't specify --set-mark twice");
128 static int mark_tg_parse(int c, char **argv, int invert, unsigned int *flags,
129 const void *entry, struct xt_entry_target **target)
131 struct xt_mark_tginfo2 *info = (void *)(*target)->data;
132 unsigned int value, mask = ~0U;
136 case 'X': /* --set-xmark */
137 case '=': /* --set-mark */
138 param_act(P_ONE_ACTION, "MARK", *flags & F_MARK);
139 param_act(P_NO_INVERT, "MARK", "--set-xmark/--set-mark", invert);
140 if (!strtonum(optarg, &end, &value, 0, ~0U))
141 param_act(P_BAD_VALUE, "MARK", "--set-xmark/--set-mark", optarg);
143 if (!strtonum(end + 1, &end, &mask, 0, ~0U))
144 param_act(P_BAD_VALUE, "MARK", "--set-xmark/--set-mark", optarg);
146 param_act(P_BAD_VALUE, "MARK", "--set-xmark/--set-mark", optarg);
151 info->mask = value | mask;
154 case '&': /* --and-mark */
155 param_act(P_ONE_ACTION, "MARK", *flags & F_MARK);
156 param_act(P_NO_INVERT, "MARK", "--and-mark", invert);
157 if (!strtonum(optarg, NULL, &mask, 0, ~0U))
158 param_act(P_BAD_VALUE, "MARK", "--and-mark", optarg);
163 case '|': /* --or-mark */
164 param_act(P_ONE_ACTION, "MARK", *flags & F_MARK);
165 param_act(P_NO_INVERT, "MARK", "--or-mark", invert);
166 if (!strtonum(optarg, NULL, &value, 0, ~0U))
167 param_act(P_BAD_VALUE, "MARK", "--or-mark", optarg);
172 case '^': /* --xor-mark */
173 param_act(P_ONE_ACTION, "MARK", *flags & F_MARK);
174 param_act(P_NO_INVERT, "MARK", "--xor-mark", invert);
175 if (!strtonum(optarg, NULL, &value, 0, ~0U))
176 param_act(P_BAD_VALUE, "MARK", "--xor-mark", optarg);
189 static void mark_tg_check(unsigned int flags)
192 exit_error(PARAMETER_PROBLEM, "MARK: One of the --set-xmark, "
193 "--{and,or,xor,set}-mark options is required");
197 print_mark(unsigned long mark)
199 printf("0x%lx ", mark);
202 /* Prints out the targinfo. */
203 static void MARK_print_v0(const void *ip,
204 const struct xt_entry_target *target, int numeric)
206 const struct xt_mark_target_info *markinfo =
207 (const struct xt_mark_target_info *)target->data;
209 print_mark(markinfo->mark);
212 /* Saves the union ipt_targinfo in parsable form to stdout. */
213 static void MARK_save_v0(const void *ip, const struct xt_entry_target *target)
215 const struct xt_mark_target_info *markinfo =
216 (const struct xt_mark_target_info *)target->data;
218 printf("--set-mark ");
219 print_mark(markinfo->mark);
222 /* Prints out the targinfo. */
223 static void MARK_print_v1(const void *ip, const struct xt_entry_target *target,
226 const struct xt_mark_target_info_v1 *markinfo =
227 (const struct xt_mark_target_info_v1 *)target->data;
229 switch (markinfo->mode) {
240 print_mark(markinfo->mark);
243 static void mark_tg_print(const void *ip, const struct xt_entry_target *target,
246 const struct xt_mark_tginfo2 *info = (const void *)target->data;
249 printf("MARK and 0x%x ", (unsigned int)(u_int32_t)~info->mask);
250 else if (info->mark == info->mask)
251 printf("MARK or 0x%x ", info->mark);
252 else if (info->mask == 0)
253 printf("MARK xor 0x%x ", info->mark);
255 printf("MARK xset 0x%x/0x%x ", info->mark, info->mask);
258 /* Saves the union ipt_targinfo in parsable form to stdout. */
259 static void MARK_save_v1(const void *ip, const struct xt_entry_target *target)
261 const struct xt_mark_target_info_v1 *markinfo =
262 (const struct xt_mark_target_info_v1 *)target->data;
264 switch (markinfo->mode) {
266 printf("--set-mark ");
269 printf("--and-mark ");
272 printf("--or-mark ");
275 print_mark(markinfo->mark);
278 static void mark_tg_save(const void *ip, const struct xt_entry_target *target)
280 const struct xt_mark_tginfo2 *info = (const void *)target->data;
282 printf("--set-xmark 0x%x/0x%x ", info->mark, info->mask);
285 static struct xtables_target mark_target_v0 = {
288 .version = XTABLES_VERSION,
290 .size = XT_ALIGN(sizeof(struct xt_mark_target_info)),
291 .userspacesize = XT_ALIGN(sizeof(struct xt_mark_target_info)),
293 .parse = MARK_parse_v0,
294 .final_check = MARK_check,
295 .print = MARK_print_v0,
296 .save = MARK_save_v0,
297 .extra_opts = MARK_opts,
300 static struct xtables_target mark_target_v1 = {
303 .version = XTABLES_VERSION,
305 .size = XT_ALIGN(sizeof(struct xt_mark_target_info_v1)),
306 .userspacesize = XT_ALIGN(sizeof(struct xt_mark_target_info_v1)),
308 .parse = MARK_parse_v1,
309 .final_check = MARK_check,
310 .print = MARK_print_v1,
311 .save = MARK_save_v1,
312 .extra_opts = MARK_opts,
315 static struct xtables_target mark_target6_v0 = {
318 .version = XTABLES_VERSION,
320 .size = XT_ALIGN(sizeof(struct xt_mark_target_info)),
321 .userspacesize = XT_ALIGN(sizeof(struct xt_mark_target_info)),
323 .parse = MARK_parse_v0,
324 .final_check = MARK_check,
325 .print = MARK_print_v0,
326 .save = MARK_save_v0,
327 .extra_opts = MARK_opts,
330 static struct xtables_target mark_tg_reg_v2 = {
331 .version = XTABLES_VERSION,
335 .size = XT_ALIGN(sizeof(struct xt_mark_tginfo2)),
336 .userspacesize = XT_ALIGN(sizeof(struct xt_mark_tginfo2)),
337 .help = mark_tg_help,
338 .parse = mark_tg_parse,
339 .final_check = mark_tg_check,
340 .print = mark_tg_print,
341 .save = mark_tg_save,
342 .extra_opts = mark_tg_opts,
347 xtables_register_target(&mark_target_v0);
348 xtables_register_target(&mark_target_v1);
349 xtables_register_target(&mark_target6_v0);
350 xtables_register_target(&mark_tg_reg_v2);