2 * Description: EBTables 802.1Q match extension kernelspace module.
3 * Authors: Nick Fedchik <nick@fedchik.org.ua>
4 * Bart De Schuymer <bart.de.schuymer@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/if_ether.h>
22 #include <linux/if_vlan.h>
23 #include <linux/module.h>
24 #include <linux/netfilter_bridge/ebtables.h>
25 #include <linux/netfilter_bridge/ebt_vlan.h>
27 static unsigned char debug;
28 #define MODULE_VERSION "0.6"
30 MODULE_PARM(debug, "0-1b");
31 MODULE_PARM_DESC(debug, "debug=1 is turn on debug messages");
32 MODULE_AUTHOR("Nick Fedchik <nick@fedchik.org.ua>");
33 MODULE_DESCRIPTION("802.1Q match module (ebtables extension), v"
35 MODULE_LICENSE("GPL");
38 #define DEBUG_MSG(...) if (debug) printk (KERN_DEBUG "ebt_vlan: " __VA_ARGS__)
39 #define INV_FLAG(_inv_flag_) (info->invflags & _inv_flag_) ? "!" : ""
40 #define GET_BITMASK(_BIT_MASK_) info->bitmask & _BIT_MASK_
41 #define SET_BITMASK(_BIT_MASK_) info->bitmask |= _BIT_MASK_
42 #define EXIT_ON_MISMATCH(_MATCH_,_MASK_) if (!((info->_MATCH_ == _MATCH_)^!!(info->invflags & _MASK_))) return 1;
45 * Function description: ebt_filter_vlan() is main engine for
46 * checking passed 802.1Q frame according to
47 * the passed extension parameters (in the *data buffer)
48 * ebt_filter_vlan() is called after successfull check the rule params
49 * by ebt_check_vlan() function.
51 * const struct sk_buff *skb - pointer to passed ethernet frame buffer
52 * const void *data - pointer to passed extension parameters
53 * unsigned int datalen - length of passed *data buffer
54 * const struct net_device *in -
55 * const struct net_device *out -
56 * const struct ebt_counter *c -
58 * 0 - ok (all rule params matched)
59 * 1 - miss (rule params not acceptable to the parsed frame)
62 ebt_filter_vlan(const struct sk_buff *skb,
63 const struct net_device *in,
64 const struct net_device *out,
65 const void *data, unsigned int datalen)
67 struct ebt_vlan_info *info = (struct ebt_vlan_info *) data; /* userspace data */
68 struct vlan_ethhdr *frame = (struct vlan_ethhdr *) skb->mac.raw; /* Passed tagged frame */
70 unsigned short TCI; /* Whole TCI, given from parsed frame */
71 unsigned short id; /* VLAN ID, given from frame TCI */
72 unsigned char prio; /* user_priority, given from frame TCI */
73 unsigned short encap; /* VLAN encapsulated Type/Length field, given from orig frame */
76 * Tag Control Information (TCI) consists of the following elements:
77 * - User_priority. The user_priority field is three bits in length,
78 * interpreted as a binary number.
79 * - Canonical Format Indicator (CFI). The Canonical Format Indicator
80 * (CFI) is a single bit flag value. Currently ignored.
81 * - VLAN Identifier (VID). The VID is encoded as
82 * an unsigned binary number.
84 TCI = ntohs(frame->h_vlan_TCI);
85 id = TCI & VLAN_VID_MASK;
86 prio = (TCI >> 13) & 0x7;
87 encap = frame->h_vlan_encapsulated_proto;
90 * Checking VLAN Identifier (VID)
92 if (GET_BITMASK(EBT_VLAN_ID)) { /* Is VLAN ID parsed? */
93 EXIT_ON_MISMATCH(id, EBT_VLAN_ID);
96 * Checking user_priority
98 if (GET_BITMASK(EBT_VLAN_PRIO)) { /* Is VLAN user_priority parsed? */
99 EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO);
102 * Checking Encapsulated Proto (Length/Type) field
104 if (GET_BITMASK(EBT_VLAN_ENCAP)) { /* Is VLAN Encap parsed? */
105 EXIT_ON_MISMATCH(encap, EBT_VLAN_ENCAP);
108 * All possible extension parameters was parsed.
109 * If rule never returned by missmatch, then all ok.
115 * Function description: ebt_vlan_check() is called when userspace
116 * delivers the table entry to the kernel,
117 * and to check that userspace doesn't give a bad table.
119 * const char *tablename - table name string
120 * unsigned int hooknr - hook number
121 * const struct ebt_entry *e - ebtables entry basic set
122 * const void *data - pointer to passed extension parameters
123 * unsigned int datalen - length of passed *data buffer
125 * 0 - ok (all delivered rule params are correct)
126 * 1 - miss (rule params is out of range, invalid, incompatible, etc.)
129 ebt_check_vlan(const char *tablename,
131 const struct ebt_entry *e, void *data, unsigned int datalen)
133 struct ebt_vlan_info *info = (struct ebt_vlan_info *) data;
136 * Parameters buffer overflow check
138 if (datalen != sizeof(struct ebt_vlan_info)) {
140 ("passed size %d is not eq to ebt_vlan_info (%d)\n",
141 datalen, sizeof(struct ebt_vlan_info));
146 * Is it 802.1Q frame checked?
148 if (e->ethproto != __constant_htons(ETH_P_8021Q)) {
150 ("passed entry proto %2.4X is not 802.1Q (8100)\n",
151 (unsigned short) ntohs(e->ethproto));
156 * Check for bitmask range
157 * True if even one bit is out of mask
159 if (info->bitmask & ~EBT_VLAN_MASK) {
160 DEBUG_MSG("bitmask %2X is out of mask (%2X)\n",
161 info->bitmask, EBT_VLAN_MASK);
166 * Check for inversion flags range
168 if (info->invflags & ~EBT_VLAN_MASK) {
169 DEBUG_MSG("inversion flags %2X is out of mask (%2X)\n",
170 info->invflags, EBT_VLAN_MASK);
175 * Reserved VLAN ID (VID) values
176 * -----------------------------
177 * 0 - The null VLAN ID.
178 * 1 - The default Port VID (PVID)
179 * 0x0FFF - Reserved for implementation use.
180 * if_vlan.h: VLAN_GROUP_ARRAY_LEN 4096.
182 if (GET_BITMASK(EBT_VLAN_ID)) { /* when vlan-id param was spec-ed */
183 if (!!info->id) { /* if id!=0 => check vid range */
184 if (info->id > VLAN_GROUP_ARRAY_LEN) {
186 ("id %d is out of range (1-4096)\n",
191 * Note: This is valid VLAN-tagged frame point.
192 * Any value of user_priority are acceptable,
193 * but should be ignored according to 802.1Q Std.
194 * So we just drop the prio flag.
196 info->bitmask &= ~EBT_VLAN_PRIO;
199 * Else, id=0 (null VLAN ID) => user_priority range (any?)
203 if (GET_BITMASK(EBT_VLAN_PRIO)) {
204 if ((unsigned char) info->prio > 7) {
206 ("prio %d is out of range (0-7)\n",
212 * Check for encapsulated proto range - it is possible to be
213 * any value for u_short range.
214 * if_ether.h: ETH_ZLEN 60 - Min. octets in frame sans FCS
216 if (GET_BITMASK(EBT_VLAN_ENCAP)) {
217 if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) {
219 ("encap frame length %d is less than minimal\n",
228 static struct ebt_match filter_vlan = {
238 * Module initialization function.
240 static int __init init(void)
242 DEBUG_MSG("ebtables 802.1Q extension module v"
243 MODULE_VERSION "\n");
244 DEBUG_MSG("module debug=%d\n", !!debug);
245 return ebt_register_match(&filter_vlan);
249 * Module "finalization" function
251 static void __exit fini(void)
253 ebt_unregister_match(&filter_vlan);