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 <getopt.h>
16 #include <xtables.h>
17 #include <linux/netfilter_bridge/ebt_mark_m.h>
18 
19 #define MARK '1'
20 
21 static 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 
100 static struct xtables_match brmark_m_match = {
101 	.name		= "mark_m",
102 	.revision	= 0,
103 	.version	= XTABLES_VERSION,
104 	.family		= NFPROTO_BRIDGE,
105 	.size		= XT_ALIGN(sizeof(struct ebt_mark_m_info)),
106 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_mark_m_info)),
107 	.init		= brmark_m_init,
108 	.help		= brmark_m_print_help,
109 	.parse		= brmark_m_parse,
110 	.final_check	= brmark_m_final_check,
111 	.print		= brmark_m_print,
112 	.extra_opts	= brmark_m_opts,
113 };
114 
_init(void)115 void _init(void)
116 {
117 	xtables_register_match(&brmark_m_match);
118 }
119