• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ebt_mark_m
2  *
3  * Authors:
4  * Bart De Schuymer <bdschuym@pandora.be>
5  *
6  * July, 2002
7  *
8  * Adapted by Arturo Borrero Gonzalez <arturo@debian.org>
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 <xtables.h>
16 #include <linux/netfilter_bridge/ebt_mark_m.h>
17 
18 enum {
19 	O_MARK = 0,
20 };
21 
22 static const struct xt_option_entry brmark_m_opts[] = {
23 	{ .name = "mark", .id = O_MARK, .type = XTTYPE_STRING,
24 	  .flags = XTOPT_INVERT | XTOPT_MAND },
25 	XTOPT_TABLEEND,
26 };
27 
brmark_m_print_help(void)28 static void brmark_m_print_help(void)
29 {
30 	printf(
31 "mark option:\n"
32 "[!] --mark    [value][/mask]: Match nfmask value (see man page)\n");
33 }
34 
brmark_m_parse(struct xt_option_call * cb)35 static void brmark_m_parse(struct xt_option_call *cb)
36 {
37 	struct ebt_mark_m_info *info = cb->data;
38 	char *end;
39 
40 	xtables_option_parse(cb);
41 
42 	switch (cb->entry->id) {
43 	case O_MARK:
44 		info->invert = cb->invert;
45 		info->mark = strtoul(cb->arg, &end, 0);
46 		info->bitmask = EBT_MARK_AND;
47 		if (*end == '/') {
48 			if (end == cb->arg)
49 				info->bitmask = EBT_MARK_OR;
50 			info->mask = strtoul(end+1, &end, 0);
51 		} else {
52 			info->mask = UINT32_MAX;
53 		}
54 		if (*end != '\0' || end == cb->arg)
55 			xtables_error(PARAMETER_PROBLEM, "Bad mark value '%s'",
56 				      cb->arg);
57 		break;
58 	}
59 }
60 
brmark_m_print(const void * ip,const struct xt_entry_match * match,int numeric)61 static void brmark_m_print(const void *ip, const struct xt_entry_match *match,
62 			   int numeric)
63 {
64 	struct ebt_mark_m_info *info = (struct ebt_mark_m_info *)match->data;
65 
66 	if (info->invert)
67 		printf("! ");
68 	printf("--mark ");
69 	if (info->bitmask == EBT_MARK_OR)
70 		printf("/0x%lx ", info->mask);
71 	else if (info->mask != 0xffffffff)
72 		printf("0x%lx/0x%lx ", info->mark, info->mask);
73 	else
74 		printf("0x%lx ", info->mark);
75 }
76 
brmark_m_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)77 static int brmark_m_xlate(struct xt_xlate *xl,
78 			  const struct xt_xlate_mt_params *params)
79 {
80 	const struct ebt_mark_m_info *info = (const void*)params->match->data;
81 	enum xt_op op = XT_OP_EQ;
82 
83 	if (info->invert)
84 		op = XT_OP_NEQ;
85 
86 	xt_xlate_add(xl, "meta mark ");
87 
88 	if (info->bitmask == EBT_MARK_OR) {
89 		xt_xlate_add(xl, "and 0x%x %s0 ", (uint32_t)info->mask,
90 			     info->invert ? "" : "!= ");
91 	} else if (info->mask != UINT32_MAX) {
92 		xt_xlate_add(xl, "and 0x%x %s0x%x ", (uint32_t)info->mask,
93 			   op == XT_OP_EQ ? "" : "!= ", (uint32_t)info->mark);
94 	} else {
95 		xt_xlate_add(xl, "%s0x%x ",
96 			   op == XT_OP_EQ ? "" : "!= ", (uint32_t)info->mark);
97 	}
98 
99 	return 1;
100 }
101 static struct xtables_match brmark_m_match = {
102 	.name		= "mark_m",
103 	.revision	= 0,
104 	.version	= XTABLES_VERSION,
105 	.family		= NFPROTO_BRIDGE,
106 	.size		= XT_ALIGN(sizeof(struct ebt_mark_m_info)),
107 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_mark_m_info)),
108 	.help		= brmark_m_print_help,
109 	.x6_parse	= brmark_m_parse,
110 	.print		= brmark_m_print,
111 	.xlate		= brmark_m_xlate,
112 	.x6_options	= brmark_m_opts,
113 };
114 
_init(void)115 void _init(void)
116 {
117 	xtables_register_match(&brmark_m_match);
118 }
119