Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/moduleparam.h> |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 25 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/netfilter_bridge/ebtables.h> |
| 27 | #include <linux/netfilter_bridge/ebt_vlan.h> |
| 28 | |
| 29 | static int debug; |
| 30 | #define MODULE_VERS "0.6" |
| 31 | |
| 32 | module_param(debug, int, 0); |
| 33 | MODULE_PARM_DESC(debug, "debug=1 is turn on debug messages"); |
| 34 | MODULE_AUTHOR("Nick Fedchik <nick@fedchik.org.ua>"); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 35 | MODULE_DESCRIPTION("Ebtables: 802.1Q VLAN tag match"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | MODULE_LICENSE("GPL"); |
| 37 | |
| 38 | |
| 39 | #define DEBUG_MSG(args...) if (debug) printk (KERN_DEBUG "ebt_vlan: " args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define GET_BITMASK(_BIT_MASK_) info->bitmask & _BIT_MASK_ |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 41 | #define EXIT_ON_MISMATCH(_MATCH_,_MASK_) {if (!((info->_MATCH_ == _MATCH_)^!!(info->invflags & _MASK_))) return false; } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 43 | static bool |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | ebt_filter_vlan(const struct sk_buff *skb, |
| 45 | const struct net_device *in, |
| 46 | const struct net_device *out, |
| 47 | const void *data, unsigned int datalen) |
| 48 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 49 | const struct ebt_vlan_info *info = data; |
| 50 | const struct vlan_hdr *fp; |
| 51 | struct vlan_hdr _frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 57 | __be16 encap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | fp = skb_header_pointer(skb, 0, sizeof(_frame), &_frame); |
| 60 | if (fp == NULL) |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 61 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | /* Tag Control Information (TCI) consists of the following elements: |
| 64 | * - User_priority. The user_priority field is three bits in length, |
| 65 | * interpreted as a binary number. |
| 66 | * - Canonical Format Indicator (CFI). The Canonical Format Indicator |
| 67 | * (CFI) is a single bit flag value. Currently ignored. |
| 68 | * - VLAN Identifier (VID). The VID is encoded as |
| 69 | * an unsigned binary number. */ |
| 70 | TCI = ntohs(fp->h_vlan_TCI); |
| 71 | id = TCI & VLAN_VID_MASK; |
| 72 | prio = (TCI >> 13) & 0x7; |
| 73 | encap = fp->h_vlan_encapsulated_proto; |
| 74 | |
| 75 | /* Checking VLAN Identifier (VID) */ |
| 76 | if (GET_BITMASK(EBT_VLAN_ID)) |
| 77 | EXIT_ON_MISMATCH(id, EBT_VLAN_ID); |
| 78 | |
| 79 | /* Checking user_priority */ |
| 80 | if (GET_BITMASK(EBT_VLAN_PRIO)) |
| 81 | EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO); |
| 82 | |
| 83 | /* Checking Encapsulated Proto (Length/Type) field */ |
| 84 | if (GET_BITMASK(EBT_VLAN_ENCAP)) |
| 85 | EXIT_ON_MISMATCH(encap, EBT_VLAN_ENCAP); |
| 86 | |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame^] | 87 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 90 | static bool |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | ebt_check_vlan(const char *tablename, |
| 92 | unsigned int hooknr, |
| 93 | const struct ebt_entry *e, void *data, unsigned int datalen) |
| 94 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 95 | struct ebt_vlan_info *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* Is it 802.1Q frame checked? */ |
| 98 | if (e->ethproto != htons(ETH_P_8021Q)) { |
| 99 | DEBUG_MSG |
| 100 | ("passed entry proto %2.4X is not 802.1Q (8100)\n", |
| 101 | (unsigned short) ntohs(e->ethproto)); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 102 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* Check for bitmask range |
| 106 | * True if even one bit is out of mask */ |
| 107 | if (info->bitmask & ~EBT_VLAN_MASK) { |
| 108 | DEBUG_MSG("bitmask %2X is out of mask (%2X)\n", |
| 109 | info->bitmask, EBT_VLAN_MASK); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 110 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* Check for inversion flags range */ |
| 114 | if (info->invflags & ~EBT_VLAN_MASK) { |
| 115 | DEBUG_MSG("inversion flags %2X is out of mask (%2X)\n", |
| 116 | info->invflags, EBT_VLAN_MASK); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 117 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* Reserved VLAN ID (VID) values |
| 121 | * ----------------------------- |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 122 | * 0 - The null VLAN ID. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | * 1 - The default Port VID (PVID) |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 124 | * 0x0FFF - Reserved for implementation use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * if_vlan.h: VLAN_GROUP_ARRAY_LEN 4096. */ |
| 126 | if (GET_BITMASK(EBT_VLAN_ID)) { |
| 127 | if (!!info->id) { /* if id!=0 => check vid range */ |
| 128 | if (info->id > VLAN_GROUP_ARRAY_LEN) { |
| 129 | DEBUG_MSG |
| 130 | ("id %d is out of range (1-4096)\n", |
| 131 | info->id); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 132 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | /* Note: This is valid VLAN-tagged frame point. |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 135 | * Any value of user_priority are acceptable, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | * but should be ignored according to 802.1Q Std. |
| 137 | * So we just drop the prio flag. */ |
| 138 | info->bitmask &= ~EBT_VLAN_PRIO; |
| 139 | } |
| 140 | /* Else, id=0 (null VLAN ID) => user_priority range (any?) */ |
| 141 | } |
| 142 | |
| 143 | if (GET_BITMASK(EBT_VLAN_PRIO)) { |
| 144 | if ((unsigned char) info->prio > 7) { |
| 145 | DEBUG_MSG("prio %d is out of range (0-7)\n", |
| 146 | info->prio); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 147 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | /* Check for encapsulated proto range - it is possible to be |
| 151 | * any value for u_short range. |
| 152 | * if_ether.h: ETH_ZLEN 60 - Min. octets in frame sans FCS */ |
| 153 | if (GET_BITMASK(EBT_VLAN_ENCAP)) { |
| 154 | if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) { |
| 155 | DEBUG_MSG |
| 156 | ("encap frame length %d is less than minimal\n", |
| 157 | ntohs(info->encap)); |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 158 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 162 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jan Engelhardt | 30083c9 | 2008-01-31 04:00:59 -0800 | [diff] [blame] | 165 | static struct ebt_match filter_vlan __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | .name = EBT_VLAN_MATCH, |
| 167 | .match = ebt_filter_vlan, |
| 168 | .check = ebt_check_vlan, |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 169 | .matchsize = XT_ALIGN(sizeof(struct ebt_vlan_info)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | .me = THIS_MODULE, |
| 171 | }; |
| 172 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 173 | static int __init ebt_vlan_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
| 175 | DEBUG_MSG("ebtables 802.1Q extension module v" |
| 176 | MODULE_VERS "\n"); |
| 177 | DEBUG_MSG("module debug=%d\n", !!debug); |
| 178 | return ebt_register_match(&filter_vlan); |
| 179 | } |
| 180 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 181 | static void __exit ebt_vlan_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | ebt_unregister_match(&filter_vlan); |
| 184 | } |
| 185 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 186 | module_init(ebt_vlan_init); |
| 187 | module_exit(ebt_vlan_fini); |