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 <getopt.h>
13 #include <ctype.h>
14 #include <xtables.h>
15 #include <netinet/if_ether.h>
16 #include <linux/netfilter_bridge/ebt_vlan.h>
17 #include <linux/if_ether.h>
18 #include "iptables/nft.h"
19 #include "iptables/nft-bridge.h"
20
21 #define NAME_VLAN_ID "id"
22 #define NAME_VLAN_PRIO "prio"
23 #define NAME_VLAN_ENCAP "encap"
24
25 #define VLAN_ID '1'
26 #define VLAN_PRIO '2'
27 #define VLAN_ENCAP '3'
28
29 static const struct option brvlan_opts[] = {
30 {"vlan-id" , required_argument, NULL, VLAN_ID},
31 {"vlan-prio" , required_argument, NULL, VLAN_PRIO},
32 {"vlan-encap", required_argument, NULL, VLAN_ENCAP},
33 XT_GETOPT_TABLEEND,
34 };
35
36 /*
37 * option inverse flags definition
38 */
39 #define OPT_VLAN_ID 0x01
40 #define OPT_VLAN_PRIO 0x02
41 #define OPT_VLAN_ENCAP 0x04
42 #define OPT_VLAN_FLAGS (OPT_VLAN_ID | OPT_VLAN_PRIO | OPT_VLAN_ENCAP)
43
brvlan_print_help(void)44 static void brvlan_print_help(void)
45 {
46 printf(
47 "vlan options:\n"
48 "--vlan-id [!] id : vlan-tagged frame identifier, 0,1-4096 (integer)\n"
49 "--vlan-prio [!] prio : Priority-tagged frame's user priority, 0-7 (integer)\n"
50 "--vlan-encap [!] encap : Encapsulated frame protocol (hexadecimal or name)\n");
51 }
52
53 static int
brvlan_parse(int c,char ** argv,int invert,unsigned int * flags,const void * entry,struct xt_entry_match ** match)54 brvlan_parse(int c, char **argv, int invert, unsigned int *flags,
55 const void *entry, struct xt_entry_match **match)
56 {
57 struct ebt_vlan_info *vlaninfo = (struct ebt_vlan_info *) (*match)->data;
58 struct xt_ethertypeent *ethent;
59 char *end;
60 struct ebt_vlan_info local;
61
62 switch (c) {
63 case VLAN_ID:
64 EBT_CHECK_OPTION(flags, OPT_VLAN_ID);
65 if (invert)
66 vlaninfo->invflags |= EBT_VLAN_ID;
67 local.id = strtoul(optarg, &end, 10);
68 if (local.id > 4094 || *end != '\0')
69 xtables_error(PARAMETER_PROBLEM, "Invalid --vlan-id range ('%s')", optarg);
70 vlaninfo->id = local.id;
71 vlaninfo->bitmask |= EBT_VLAN_ID;
72 break;
73 case VLAN_PRIO:
74 EBT_CHECK_OPTION(flags, OPT_VLAN_PRIO);
75 if (invert)
76 vlaninfo->invflags |= EBT_VLAN_PRIO;
77 local.prio = strtoul(optarg, &end, 10);
78 if (local.prio >= 8 || *end != '\0')
79 xtables_error(PARAMETER_PROBLEM, "Invalid --vlan-prio range ('%s')", optarg);
80 vlaninfo->prio = local.prio;
81 vlaninfo->bitmask |= EBT_VLAN_PRIO;
82 break;
83 case VLAN_ENCAP:
84 EBT_CHECK_OPTION(flags, OPT_VLAN_ENCAP);
85 if (invert)
86 vlaninfo->invflags |= EBT_VLAN_ENCAP;
87 local.encap = strtoul(optarg, &end, 16);
88 if (*end != '\0') {
89 ethent = xtables_getethertypebyname(optarg);
90 if (ethent == NULL)
91 xtables_error(PARAMETER_PROBLEM, "Unknown --vlan-encap value ('%s')", optarg);
92 local.encap = ethent->e_ethertype;
93 }
94 if (local.encap < ETH_ZLEN)
95 xtables_error(PARAMETER_PROBLEM, "Invalid --vlan-encap range ('%s')", optarg);
96 vlaninfo->encap = htons(local.encap);
97 vlaninfo->bitmask |= EBT_VLAN_ENCAP;
98 break;
99 default:
100 return 0;
101
102 }
103 return 1;
104 }
105
brvlan_print(const void * ip,const struct xt_entry_match * match,int numeric)106 static void brvlan_print(const void *ip, const struct xt_entry_match *match,
107 int numeric)
108 {
109 struct ebt_vlan_info *vlaninfo = (struct ebt_vlan_info *) match->data;
110
111 if (vlaninfo->bitmask & EBT_VLAN_ID) {
112 printf("--vlan-id %s%d ", (vlaninfo->invflags & EBT_VLAN_ID) ? "! " : "", vlaninfo->id);
113 }
114 if (vlaninfo->bitmask & EBT_VLAN_PRIO) {
115 printf("--vlan-prio %s%d ", (vlaninfo->invflags & EBT_VLAN_PRIO) ? "! " : "", vlaninfo->prio);
116 }
117 if (vlaninfo->bitmask & EBT_VLAN_ENCAP) {
118 printf("--vlan-encap %s", (vlaninfo->invflags & EBT_VLAN_ENCAP) ? "! " : "");
119 printf("%4.4X ", ntohs(vlaninfo->encap));
120 }
121 }
122
brvlan_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)123 static int brvlan_xlate(struct xt_xlate *xl,
124 const struct xt_xlate_mt_params *params)
125 {
126 const struct ebt_vlan_info *vlaninfo = (const void*)params->match->data;
127
128 if (vlaninfo->bitmask & EBT_VLAN_ID)
129 xt_xlate_add(xl, "vlan id %s%d ", (vlaninfo->invflags & EBT_VLAN_ID) ? "!= " : "", vlaninfo->id);
130
131 if (vlaninfo->bitmask & EBT_VLAN_PRIO)
132 xt_xlate_add(xl, "vlan pcp %s%d ", (vlaninfo->invflags & EBT_VLAN_PRIO) ? "!= " : "", vlaninfo->prio);
133
134 if (vlaninfo->bitmask & EBT_VLAN_ENCAP)
135 xt_xlate_add(xl, "vlan type %s0x%4.4x ", (vlaninfo->invflags & EBT_VLAN_ENCAP) ? "!= " : "", ntohs(vlaninfo->encap));
136
137 return 1;
138 }
139
140 static struct xtables_match brvlan_match = {
141 .name = "vlan",
142 .version = XTABLES_VERSION,
143 .family = NFPROTO_BRIDGE,
144 .size = XT_ALIGN(sizeof(struct ebt_vlan_info)),
145 .userspacesize = XT_ALIGN(sizeof(struct ebt_vlan_info)),
146 .help = brvlan_print_help,
147 .parse = brvlan_parse,
148 .print = brvlan_print,
149 .xlate = brvlan_xlate,
150 .extra_opts = brvlan_opts,
151 };
152
_init(void)153 void _init(void)
154 {
155 xtables_register_match(&brvlan_match);
156 }
157