xref: /aosp_15_r20/external/iptables/extensions/libebt_802_3.c (revision a71a954618bbadd4a345637e5edcf36eec826889)
1 /* 802_3
2  *
3  * Author:
4  * Chris Vitale <[email protected]>
5  *
6  * May 2003
7  *
8  * Adapted by Arturo Borrero Gonzalez <[email protected]>
9  * to use libxtables for ebtables-compat
10  */
11 
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <getopt.h>
17 #include <xtables.h>
18 #include <linux/netfilter_bridge/ebt_802_3.h>
19 
20 #define _802_3_SAP	'1'
21 #define _802_3_TYPE	'2'
22 
23 static const struct option br802_3_opts[] = {
24 	{ .name = "802_3-sap",	.has_arg = true, .val = _802_3_SAP },
25 	{ .name = "802_3-type",	.has_arg = true, .val = _802_3_TYPE },
26 	XT_GETOPT_TABLEEND,
27 };
28 
br802_3_print_help(void)29 static void br802_3_print_help(void)
30 {
31 	printf(
32 "802_3 options:\n"
33 "--802_3-sap [!] protocol       : 802.3 DSAP/SSAP- 1 byte value (hex)\n"
34 "  DSAP and SSAP are always the same.  One SAP applies to both fields\n"
35 "--802_3-type [!] protocol      : 802.3 SNAP Type- 2 byte value (hex)\n"
36 "  Type implies SAP value 0xaa\n");
37 }
38 
br802_3_init(struct xt_entry_match * match)39 static void br802_3_init(struct xt_entry_match *match)
40 {
41 	struct ebt_802_3_info *info = (struct ebt_802_3_info *)match->data;
42 
43 	info->invflags = 0;
44 	info->bitmask = 0;
45 }
46 
47 static int
br802_3_parse(int c,char ** argv,int invert,unsigned int * flags,const void * entry,struct xt_entry_match ** match)48 br802_3_parse(int c, char **argv, int invert, unsigned int *flags,
49 	      const void *entry, struct xt_entry_match **match)
50 {
51 	struct ebt_802_3_info *info = (struct ebt_802_3_info *) (*match)->data;
52 	unsigned int i;
53 	char *end;
54 
55 	switch (c) {
56 	case _802_3_SAP:
57 		if (invert)
58 			info->invflags |= EBT_802_3_SAP;
59 		i = strtoul(optarg, &end, 16);
60 		if (i > 255 || *end != '\0')
61 			xtables_error(PARAMETER_PROBLEM,
62 				      "Problem with specified "
63 					"sap hex value, %x",i);
64 		info->sap = i; /* one byte, so no byte order worries */
65 		info->bitmask |= EBT_802_3_SAP;
66 		break;
67 	case _802_3_TYPE:
68 		if (invert)
69 			info->invflags |= EBT_802_3_TYPE;
70 		i = strtoul(optarg, &end, 16);
71 		if (i > 65535 || *end != '\0') {
72 			xtables_error(PARAMETER_PROBLEM,
73 				      "Problem with the specified "
74 					"type hex value, %x",i);
75 		}
76 		info->type = htons(i);
77 		info->bitmask |= EBT_802_3_TYPE;
78 		break;
79 	default:
80 		return 0;
81 	}
82 
83 	*flags |= info->bitmask;
84 	return 1;
85 }
86 
87 static void
br802_3_final_check(unsigned int flags)88 br802_3_final_check(unsigned int flags)
89 {
90 	if (!flags)
91 		xtables_error(PARAMETER_PROBLEM,
92 			      "You must specify proper arguments");
93 }
94 
br802_3_print(const void * ip,const struct xt_entry_match * match,int numeric)95 static void br802_3_print(const void *ip, const struct xt_entry_match *match,
96 			  int numeric)
97 {
98 	struct ebt_802_3_info *info = (struct ebt_802_3_info *)match->data;
99 
100 	if (info->bitmask & EBT_802_3_SAP) {
101 		printf("--802_3-sap ");
102 		if (info->invflags & EBT_802_3_SAP)
103 			printf("! ");
104 		printf("0x%.2x ", info->sap);
105 	}
106 	if (info->bitmask & EBT_802_3_TYPE) {
107 		printf("--802_3-type ");
108 		if (info->invflags & EBT_802_3_TYPE)
109 			printf("! ");
110 		printf("0x%.4x ", ntohs(info->type));
111 	}
112 }
113 
114 static struct xtables_match br802_3_match =
115 {
116 	.name		= "802_3",
117 	.revision	= 0,
118 	.version	= XTABLES_VERSION,
119 	.family		= NFPROTO_BRIDGE,
120 	.size		= XT_ALIGN(sizeof(struct ebt_802_3_info)),
121 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_802_3_info)),
122 	.init		= br802_3_init,
123 	.help		= br802_3_print_help,
124 	.parse		= br802_3_parse,
125 	.final_check	= br802_3_final_check,
126 	.print		= br802_3_print,
127 	.extra_opts	= br802_3_opts,
128 };
129 
_init(void)130 void _init(void)
131 {
132 	xtables_register_match(&br802_3_match);
133 }
134