cleanup
[linux-2.4.21-pre4.git] / net / bridge / netfilter / ebt_vlan.c
1 /*
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>
5  *    
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.
10  * 
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.
15  *  
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
19  */
20
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>
26
27 static unsigned char debug;
28 #define MODULE_VERSION "0.6"
29
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"
34                    MODULE_VERSION);
35 MODULE_LICENSE("GPL");
36
37
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;
43
44 /*
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.
50  * Parameters:
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 -
57  * Returned values:
58  * 0 - ok (all rule params matched)
59  * 1 - miss (rule params not acceptable to the parsed frame)
60  */
61 static int
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)
66 {
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 */
69
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 */
74
75         /*
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. 
83          */
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;
88
89         /*
90          * Checking VLAN Identifier (VID)
91          */
92         if (GET_BITMASK(EBT_VLAN_ID)) { /* Is VLAN ID parsed? */
93                 EXIT_ON_MISMATCH(id, EBT_VLAN_ID);
94         }
95         /*
96          * Checking user_priority
97          */
98         if (GET_BITMASK(EBT_VLAN_PRIO)) {       /* Is VLAN user_priority parsed? */
99                 EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO);
100         }
101         /*
102          * Checking Encapsulated Proto (Length/Type) field
103          */
104         if (GET_BITMASK(EBT_VLAN_ENCAP)) {      /* Is VLAN Encap parsed? */
105                 EXIT_ON_MISMATCH(encap, EBT_VLAN_ENCAP);
106         }
107         /*
108          * All possible extension parameters was parsed.
109          * If rule never returned by missmatch, then all ok.
110          */
111         return 0;
112 }
113
114 /*
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.
118  * Parameters:
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
124  * Returned values:
125  * 0 - ok (all delivered rule params are correct)
126  * 1 - miss (rule params is out of range, invalid, incompatible, etc.)
127  */
128 static int
129 ebt_check_vlan(const char *tablename,
130                unsigned int hooknr,
131                const struct ebt_entry *e, void *data, unsigned int datalen)
132 {
133         struct ebt_vlan_info *info = (struct ebt_vlan_info *) data;
134
135         /*
136          * Parameters buffer overflow check 
137          */
138         if (datalen != sizeof(struct ebt_vlan_info)) {
139                 DEBUG_MSG
140                     ("passed size %d is not eq to ebt_vlan_info (%d)\n",
141                      datalen, sizeof(struct ebt_vlan_info));
142                 return -EINVAL;
143         }
144
145         /*
146          * Is it 802.1Q frame checked?
147          */
148         if (e->ethproto != __constant_htons(ETH_P_8021Q)) {
149                 DEBUG_MSG
150                     ("passed entry proto %2.4X is not 802.1Q (8100)\n",
151                      (unsigned short) ntohs(e->ethproto));
152                 return -EINVAL;
153         }
154
155         /*
156          * Check for bitmask range 
157          * True if even one bit is out of mask
158          */
159         if (info->bitmask & ~EBT_VLAN_MASK) {
160                 DEBUG_MSG("bitmask %2X is out of mask (%2X)\n",
161                           info->bitmask, EBT_VLAN_MASK);
162                 return -EINVAL;
163         }
164
165         /*
166          * Check for inversion flags range 
167          */
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);
171                 return -EINVAL;
172         }
173
174         /*
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.
181          */
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) {
185                                 DEBUG_MSG
186                                     ("id %d is out of range (1-4096)\n",
187                                      info->id);
188                                 return -EINVAL;
189                         }
190                         /*
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. 
195                          */
196                         info->bitmask &= ~EBT_VLAN_PRIO;
197                 }
198                 /*
199                  * Else, id=0 (null VLAN ID)  => user_priority range (any?)
200                  */
201         }
202
203         if (GET_BITMASK(EBT_VLAN_PRIO)) {
204                 if ((unsigned char) info->prio > 7) {
205                         DEBUG_MSG
206                             ("prio %d is out of range (0-7)\n",
207                              info->prio);
208                         return -EINVAL;
209                 }
210         }
211         /*
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
215          */
216         if (GET_BITMASK(EBT_VLAN_ENCAP)) {
217                 if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) {
218                         DEBUG_MSG
219                             ("encap frame length %d is less than minimal\n",
220                              ntohs(info->encap));
221                         return -EINVAL;
222                 }
223         }
224
225         return 0;
226 }
227
228 static struct ebt_match filter_vlan = {
229         {NULL, NULL},
230         EBT_VLAN_MATCH,
231         ebt_filter_vlan,
232         ebt_check_vlan,
233         NULL,
234         THIS_MODULE
235 };
236
237 /*
238  * Module initialization function.
239  */
240 static int __init init(void)
241 {
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);
246 }
247
248 /*
249  * Module "finalization" function
250  */
251 static void __exit fini(void)
252 {
253         ebt_unregister_match(&filter_vlan);
254 }
255
256 module_init(init);
257 module_exit(fini);
258
259 EXPORT_NO_SYMBOLS;