• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ebt_vlan
2  *
3  * Authors:
4  * Bart De Schuymer <bdschuym@pandora.be>
5  * Nick Fedchik <nick@fedchik.org.ua>
6  * June, 2002
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <xtables.h>
14 #include <netinet/if_ether.h>
15 #include <linux/netfilter_bridge/ebt_vlan.h>
16 #include <linux/if_ether.h>
17 #include "iptables/nft.h"
18 #include "iptables/nft-bridge.h"
19 
20 static const struct xt_option_entry brvlan_opts[] =
21 {
22 	{ .name = "vlan-id", .id = EBT_VLAN_ID, .type = XTTYPE_UINT16,
23 	  .max = 4094, .flags = XTOPT_INVERT | XTOPT_PUT,
24 	  XTOPT_POINTER(struct ebt_vlan_info, id) },
25 	{ .name = "vlan-prio", .id = EBT_VLAN_PRIO, .type = XTTYPE_UINT8,
26 	  .max = 7, .flags = XTOPT_INVERT | XTOPT_PUT,
27 	  XTOPT_POINTER(struct ebt_vlan_info, prio) },
28 	{ .name = "vlan-encap", .id = EBT_VLAN_ENCAP, .type = XTTYPE_STRING,
29 	  .flags = XTOPT_INVERT },
30 	XTOPT_TABLEEND,
31 };
32 
brvlan_print_help(void)33 static void brvlan_print_help(void)
34 {
35 	printf(
36 "vlan options:\n"
37 "[!] --vlan-id id       : vlan-tagged frame identifier, 0,1-4096 (integer)\n"
38 "[!] --vlan-prio prio   : Priority-tagged frame's user priority, 0-7 (integer)\n"
39 "[!] --vlan-encap encap : Encapsulated frame protocol (hexadecimal or name)\n");
40 }
41 
brvlan_parse(struct xt_option_call * cb)42 static void brvlan_parse(struct xt_option_call *cb)
43 {
44 	struct ebt_vlan_info *vlaninfo = cb->data;
45 	struct xt_ethertypeent *ethent;
46 	char *end;
47 
48 	xtables_option_parse(cb);
49 
50 	vlaninfo->bitmask |= cb->entry->id;
51 	if (cb->invert)
52 		vlaninfo->invflags |= cb->entry->id;
53 
54 	if (cb->entry->id == EBT_VLAN_ENCAP) {
55 		vlaninfo->encap = strtoul(cb->arg, &end, 16);
56 		if (*end != '\0') {
57 			ethent = xtables_getethertypebyname(cb->arg);
58 			if (ethent == NULL)
59 				xtables_error(PARAMETER_PROBLEM,
60 					      "Unknown --vlan-encap value ('%s')",
61 					      cb->arg);
62 			vlaninfo->encap = ethent->e_ethertype;
63 		}
64 		if (vlaninfo->encap < ETH_ZLEN)
65 			xtables_error(PARAMETER_PROBLEM,
66 				      "Invalid --vlan-encap range ('%s')",
67 				      cb->arg);
68 		vlaninfo->encap = htons(vlaninfo->encap);
69 	}
70 }
71 
brvlan_print(const void * ip,const struct xt_entry_match * match,int numeric)72 static void brvlan_print(const void *ip, const struct xt_entry_match *match,
73 			 int numeric)
74 {
75 	struct ebt_vlan_info *vlaninfo = (struct ebt_vlan_info *) match->data;
76 
77 	if (vlaninfo->bitmask & EBT_VLAN_ID) {
78 		printf("%s--vlan-id %d ",
79 		       (vlaninfo->invflags & EBT_VLAN_ID) ? "! " : "",
80 		       vlaninfo->id);
81 	}
82 	if (vlaninfo->bitmask & EBT_VLAN_PRIO) {
83 		printf("%s--vlan-prio %d ",
84 		       (vlaninfo->invflags & EBT_VLAN_PRIO) ? "! " : "",
85 		       vlaninfo->prio);
86 	}
87 	if (vlaninfo->bitmask & EBT_VLAN_ENCAP) {
88 		printf("%s--vlan-encap %4.4X ",
89 		       (vlaninfo->invflags & EBT_VLAN_ENCAP) ? "! " : "",
90 		       ntohs(vlaninfo->encap));
91 	}
92 }
93 
brvlan_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)94 static int brvlan_xlate(struct xt_xlate *xl,
95 			const struct xt_xlate_mt_params *params)
96 {
97 	const struct ebt_vlan_info *vlaninfo = (const void*)params->match->data;
98 
99 	if (vlaninfo->bitmask & EBT_VLAN_ID)
100 		xt_xlate_add(xl, "vlan id %s%d ", (vlaninfo->invflags & EBT_VLAN_ID) ? "!= " : "", vlaninfo->id);
101 
102 	if (vlaninfo->bitmask & EBT_VLAN_PRIO)
103 		xt_xlate_add(xl, "vlan pcp %s%d ", (vlaninfo->invflags & EBT_VLAN_PRIO) ? "!= " : "", vlaninfo->prio);
104 
105 	if (vlaninfo->bitmask & EBT_VLAN_ENCAP)
106 		xt_xlate_add(xl, "vlan type %s0x%4.4x ", (vlaninfo->invflags & EBT_VLAN_ENCAP) ? "!= " : "", ntohs(vlaninfo->encap));
107 
108 	return 1;
109 }
110 
111 static struct xtables_match brvlan_match = {
112 	.name		= "vlan",
113 	.version	= XTABLES_VERSION,
114 	.family		= NFPROTO_BRIDGE,
115 	.size		= XT_ALIGN(sizeof(struct ebt_vlan_info)),
116 	.userspacesize	= XT_ALIGN(sizeof(struct ebt_vlan_info)),
117 	.help		= brvlan_print_help,
118 	.x6_parse	= brvlan_parse,
119 	.print		= brvlan_print,
120 	.xlate		= brvlan_xlate,
121 	.x6_options	= brvlan_opts,
122 };
123 
_init(void)124 void _init(void)
125 {
126 	xtables_register_match(&brvlan_match);
127 }
128