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 |
Jeff Kirsher | e664eab | 2013-12-06 09:13:42 -0800 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <linux/if_ether.h> |
| 21 | #include <linux/if_vlan.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/moduleparam.h> |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 24 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/netfilter_bridge/ebtables.h> |
| 26 | #include <linux/netfilter_bridge/ebt_vlan.h> |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #define MODULE_VERS "0.6" |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | MODULE_AUTHOR("Nick Fedchik <nick@fedchik.org.ua>"); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 31 | MODULE_DESCRIPTION("Ebtables: 802.1Q VLAN tag match"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | MODULE_LICENSE("GPL"); |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #define GET_BITMASK(_BIT_MASK_) info->bitmask & _BIT_MASK_ |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 35 | #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] | 36 | |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 37 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 38 | ebt_vlan_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 40 | const struct ebt_vlan_info *info = par->matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | unsigned short TCI; /* Whole TCI, given from parsed frame */ |
| 43 | unsigned short id; /* VLAN ID, given from frame TCI */ |
| 44 | unsigned char prio; /* user_priority, given from frame TCI */ |
| 45 | /* VLAN encapsulated Type/Length field, given from orig frame */ |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 46 | __be16 encap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 48 | if (skb_vlan_tag_present(skb)) { |
| 49 | TCI = skb_vlan_tag_get(skb); |
Jesse Gross | 1393791 | 2010-10-20 13:56:01 +0000 | [diff] [blame] | 50 | encap = skb->protocol; |
| 51 | } else { |
| 52 | const struct vlan_hdr *fp; |
| 53 | struct vlan_hdr _frame; |
| 54 | |
| 55 | fp = skb_header_pointer(skb, 0, sizeof(_frame), &_frame); |
| 56 | if (fp == NULL) |
| 57 | return false; |
| 58 | |
| 59 | TCI = ntohs(fp->h_vlan_TCI); |
| 60 | encap = fp->h_vlan_encapsulated_proto; |
| 61 | } |
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 |
Ian Morris | 7f495ad | 2015-10-26 09:10:41 +0000 | [diff] [blame] | 69 | * an unsigned binary number. |
| 70 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | id = TCI & VLAN_VID_MASK; |
| 72 | prio = (TCI >> 13) & 0x7; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jan Engelhardt | 8cc784e | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 86 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 89 | static int ebt_vlan_mt_check(const struct xt_mtchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 91 | struct ebt_vlan_info *info = par->matchinfo; |
| 92 | const struct ebt_entry *e = par->entryinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* Is it 802.1Q frame checked? */ |
| 95 | if (e->ethproto != htons(ETH_P_8021Q)) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 96 | pr_debug("passed entry proto %2.4X is not 802.1Q (8100)\n", |
| 97 | ntohs(e->ethproto)); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 98 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* Check for bitmask range |
Ian Morris | 7f495ad | 2015-10-26 09:10:41 +0000 | [diff] [blame] | 102 | * True if even one bit is out of mask |
| 103 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | if (info->bitmask & ~EBT_VLAN_MASK) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 105 | pr_debug("bitmask %2X is out of mask (%2X)\n", |
| 106 | info->bitmask, EBT_VLAN_MASK); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 107 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* Check for inversion flags range */ |
| 111 | if (info->invflags & ~EBT_VLAN_MASK) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 112 | pr_debug("inversion flags %2X is out of mask (%2X)\n", |
| 113 | info->invflags, EBT_VLAN_MASK); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 114 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* Reserved VLAN ID (VID) values |
| 118 | * ----------------------------- |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 119 | * 0 - The null VLAN ID. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | * 1 - The default Port VID (PVID) |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 121 | * 0x0FFF - Reserved for implementation use. |
Ian Morris | 7f495ad | 2015-10-26 09:10:41 +0000 | [diff] [blame] | 122 | * if_vlan.h: VLAN_N_VID 4096. |
| 123 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (GET_BITMASK(EBT_VLAN_ID)) { |
| 125 | if (!!info->id) { /* if id!=0 => check vid range */ |
Jesse Gross | b738127 | 2010-10-20 13:56:02 +0000 | [diff] [blame] | 126 | if (info->id > VLAN_N_VID) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 127 | pr_debug("id %d is out of range (1-4096)\n", |
| 128 | info->id); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 129 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | /* Note: This is valid VLAN-tagged frame point. |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 132 | * Any value of user_priority are acceptable, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | * but should be ignored according to 802.1Q Std. |
Ian Morris | 7f495ad | 2015-10-26 09:10:41 +0000 | [diff] [blame] | 134 | * So we just drop the prio flag. |
| 135 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | info->bitmask &= ~EBT_VLAN_PRIO; |
| 137 | } |
| 138 | /* Else, id=0 (null VLAN ID) => user_priority range (any?) */ |
| 139 | } |
| 140 | |
| 141 | if (GET_BITMASK(EBT_VLAN_PRIO)) { |
| 142 | if ((unsigned char) info->prio > 7) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 143 | pr_debug("prio %d is out of range (0-7)\n", |
| 144 | info->prio); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 145 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | /* Check for encapsulated proto range - it is possible to be |
| 149 | * any value for u_short range. |
Ian Morris | 7f495ad | 2015-10-26 09:10:41 +0000 | [diff] [blame] | 150 | * if_ether.h: ETH_ZLEN 60 - Min. octets in frame sans FCS |
| 151 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (GET_BITMASK(EBT_VLAN_ENCAP)) { |
| 153 | if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 154 | pr_debug("encap frame length %d is less than " |
| 155 | "minimal\n", ntohs(info->encap)); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 156 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 160 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 163 | static struct xt_match ebt_vlan_mt_reg __read_mostly = { |
| 164 | .name = "vlan", |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 165 | .revision = 0, |
| 166 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 167 | .match = ebt_vlan_mt, |
| 168 | .checkentry = ebt_vlan_mt_check, |
Florian Westphal | fc0e3df | 2010-02-15 18:16:26 +0100 | [diff] [blame] | 169 | .matchsize = 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 | { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 175 | pr_debug("ebtables 802.1Q extension module v" MODULE_VERS "\n"); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 176 | return xt_register_match(&ebt_vlan_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 179 | static void __exit ebt_vlan_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 181 | xt_unregister_match(&ebt_vlan_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 184 | module_init(ebt_vlan_init); |
| 185 | module_exit(ebt_vlan_fini); |