ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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 <bdschuym@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_VERS "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_VERS);
35 MODULE_LICENSE("GPL");
36
37
38 #define DEBUG_MSG(args...) if (debug) printk (KERN_DEBUG "ebt_vlan: " 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 EBT_NOMATCH;}
43
44 static int
45 ebt_filter_vlan(const struct sk_buff *skb,
46                 const struct net_device *in,
47                 const struct net_device *out,
48                 const void *data, unsigned int datalen)
49 {
50         struct ebt_vlan_info *info = (struct ebt_vlan_info *) data;
51         struct vlan_hdr frame;
52
53         unsigned short TCI;     /* Whole TCI, given from parsed frame */
54         unsigned short id;      /* VLAN ID, given from frame TCI */
55         unsigned char prio;     /* user_priority, given from frame TCI */
56         /* VLAN encapsulated Type/Length field, given from orig frame */
57         unsigned short encap;
58
59         if (skb_copy_bits(skb, 0, &frame, sizeof(frame)))
60                 return EBT_NOMATCH;
61
62         /* Tag Control Information (TCI) consists of the following elements:
63          * - User_priority. The user_priority field is three bits in length,
64          * interpreted as a binary number.
65          * - Canonical Format Indicator (CFI). The Canonical Format Indicator
66          * (CFI) is a single bit flag value. Currently ignored.
67          * - VLAN Identifier (VID). The VID is encoded as
68          * an unsigned binary number. */
69         TCI = ntohs(frame.h_vlan_TCI);
70         id = TCI & VLAN_VID_MASK;
71         prio = (TCI >> 13) & 0x7;
72         encap = frame.h_vlan_encapsulated_proto;
73
74         /* Checking VLAN Identifier (VID) */
75         if (GET_BITMASK(EBT_VLAN_ID))
76                 EXIT_ON_MISMATCH(id, EBT_VLAN_ID);
77
78         /* Checking user_priority */
79         if (GET_BITMASK(EBT_VLAN_PRIO))
80                 EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO);
81
82         /* Checking Encapsulated Proto (Length/Type) field */
83         if (GET_BITMASK(EBT_VLAN_ENCAP))
84                 EXIT_ON_MISMATCH(encap, EBT_VLAN_ENCAP);
85
86         return EBT_MATCH;
87 }
88
89 static int
90 ebt_check_vlan(const char *tablename,
91                unsigned int hooknr,
92                const struct ebt_entry *e, void *data, unsigned int datalen)
93 {
94         struct ebt_vlan_info *info = (struct ebt_vlan_info *) data;
95
96         /* Parameters buffer overflow check */
97         if (datalen != EBT_ALIGN(sizeof(struct ebt_vlan_info))) {
98                 DEBUG_MSG
99                     ("passed size %d is not eq to ebt_vlan_info (%Zd)\n",
100                      datalen, sizeof(struct ebt_vlan_info));
101                 return -EINVAL;
102         }
103
104         /* Is it 802.1Q frame checked? */
105         if (e->ethproto != __constant_htons(ETH_P_8021Q)) {
106                 DEBUG_MSG
107                     ("passed entry proto %2.4X is not 802.1Q (8100)\n",
108                      (unsigned short) ntohs(e->ethproto));
109                 return -EINVAL;
110         }
111
112         /* Check for bitmask range
113          * True if even one bit is out of mask */
114         if (info->bitmask & ~EBT_VLAN_MASK) {
115                 DEBUG_MSG("bitmask %2X is out of mask (%2X)\n",
116                           info->bitmask, EBT_VLAN_MASK);
117                 return -EINVAL;
118         }
119
120         /* Check for inversion flags range */
121         if (info->invflags & ~EBT_VLAN_MASK) {
122                 DEBUG_MSG("inversion flags %2X is out of mask (%2X)\n",
123                           info->invflags, EBT_VLAN_MASK);
124                 return -EINVAL;
125         }
126
127         /* Reserved VLAN ID (VID) values
128          * -----------------------------
129          * 0 - The null VLAN ID. 
130          * 1 - The default Port VID (PVID)
131          * 0x0FFF - Reserved for implementation use. 
132          * if_vlan.h: VLAN_GROUP_ARRAY_LEN 4096. */
133         if (GET_BITMASK(EBT_VLAN_ID)) {
134                 if (!!info->id) { /* if id!=0 => check vid range */
135                         if (info->id > VLAN_GROUP_ARRAY_LEN) {
136                                 DEBUG_MSG
137                                     ("id %d is out of range (1-4096)\n",
138                                      info->id);
139                                 return -EINVAL;
140                         }
141                         /* Note: This is valid VLAN-tagged frame point.
142                          * Any value of user_priority are acceptable, 
143                          * but should be ignored according to 802.1Q Std.
144                          * So we just drop the prio flag. */
145                         info->bitmask &= ~EBT_VLAN_PRIO;
146                 }
147                 /* Else, id=0 (null VLAN ID)  => user_priority range (any?) */
148         }
149
150         if (GET_BITMASK(EBT_VLAN_PRIO)) {
151                 if ((unsigned char) info->prio > 7) {
152                         DEBUG_MSG("prio %d is out of range (0-7)\n",
153                              info->prio);
154                         return -EINVAL;
155                 }
156         }
157         /* Check for encapsulated proto range - it is possible to be
158          * any value for u_short range.
159          * if_ether.h:  ETH_ZLEN        60   -  Min. octets in frame sans FCS */
160         if (GET_BITMASK(EBT_VLAN_ENCAP)) {
161                 if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) {
162                         DEBUG_MSG
163                             ("encap frame length %d is less than minimal\n",
164                              ntohs(info->encap));
165                         return -EINVAL;
166                 }
167         }
168
169         return 0;
170 }
171
172 static struct ebt_match filter_vlan = {
173         .name           = EBT_VLAN_MATCH,
174         .match          = ebt_filter_vlan,
175         .check          = ebt_check_vlan,
176         .me             = THIS_MODULE,
177 };
178
179 static int __init init(void)
180 {
181         DEBUG_MSG("ebtables 802.1Q extension module v"
182                   MODULE_VERS "\n");
183         DEBUG_MSG("module debug=%d\n", !!debug);
184         return ebt_register_match(&filter_vlan);
185 }
186
187 static void __exit fini(void)
188 {
189         ebt_unregister_match(&filter_vlan);
190 }
191
192 module_init(init);
193 module_exit(fini);