2019-05-19 07:51:43 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* Description: EBTables 802.1Q match extension kernelspace module.
|
|
|
|
* Authors: Nick Fedchik <nick@fedchik.org.ua>
|
|
|
|
* Bart De Schuymer <bdschuym@pandora.be>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#include <linux/if_vlan.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
2008-10-08 03:35:13 -06:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/netfilter_bridge/ebtables.h>
|
|
|
|
#include <linux/netfilter_bridge/ebt_vlan.h>
|
|
|
|
|
|
|
|
#define MODULE_VERS "0.6"
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Nick Fedchik <nick@fedchik.org.ua>");
|
2008-01-31 05:00:30 -07:00
|
|
|
MODULE_DESCRIPTION("Ebtables: 802.1Q VLAN tag match");
|
2005-04-16 16:20:36 -06:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
#define GET_BITMASK(_BIT_MASK_) info->bitmask & _BIT_MASK_
|
2008-10-08 03:35:13 -06:00
|
|
|
#define EXIT_ON_MISMATCH(_MATCH_,_MASK_) {if (!((info->_MATCH_ == _MATCH_)^!!(info->invflags & _MASK_))) return false; }
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-10-08 03:35:13 -06:00
|
|
|
static bool
|
2009-07-07 12:42:08 -06:00
|
|
|
ebt_vlan_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct ebt_vlan_info *info = par->matchinfo;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
unsigned short TCI; /* Whole TCI, given from parsed frame */
|
|
|
|
unsigned short id; /* VLAN ID, given from frame TCI */
|
|
|
|
unsigned char prio; /* user_priority, given from frame TCI */
|
|
|
|
/* VLAN encapsulated Type/Length field, given from orig frame */
|
2006-11-14 22:11:51 -07:00
|
|
|
__be16 encap;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2015-01-13 09:13:44 -07:00
|
|
|
if (skb_vlan_tag_present(skb)) {
|
|
|
|
TCI = skb_vlan_tag_get(skb);
|
2010-10-20 07:56:01 -06:00
|
|
|
encap = skb->protocol;
|
|
|
|
} else {
|
|
|
|
const struct vlan_hdr *fp;
|
|
|
|
struct vlan_hdr _frame;
|
|
|
|
|
|
|
|
fp = skb_header_pointer(skb, 0, sizeof(_frame), &_frame);
|
|
|
|
if (fp == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
TCI = ntohs(fp->h_vlan_TCI);
|
|
|
|
encap = fp->h_vlan_encapsulated_proto;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* Tag Control Information (TCI) consists of the following elements:
|
|
|
|
* - User_priority. The user_priority field is three bits in length,
|
|
|
|
* interpreted as a binary number.
|
|
|
|
* - Canonical Format Indicator (CFI). The Canonical Format Indicator
|
|
|
|
* (CFI) is a single bit flag value. Currently ignored.
|
|
|
|
* - VLAN Identifier (VID). The VID is encoded as
|
2015-10-26 03:10:41 -06:00
|
|
|
* an unsigned binary number.
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
id = TCI & VLAN_VID_MASK;
|
|
|
|
prio = (TCI >> 13) & 0x7;
|
|
|
|
|
|
|
|
/* Checking VLAN Identifier (VID) */
|
|
|
|
if (GET_BITMASK(EBT_VLAN_ID))
|
|
|
|
EXIT_ON_MISMATCH(id, EBT_VLAN_ID);
|
|
|
|
|
|
|
|
/* Checking user_priority */
|
|
|
|
if (GET_BITMASK(EBT_VLAN_PRIO))
|
|
|
|
EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO);
|
|
|
|
|
|
|
|
/* Checking Encapsulated Proto (Length/Type) field */
|
|
|
|
if (GET_BITMASK(EBT_VLAN_ENCAP))
|
|
|
|
EXIT_ON_MISMATCH(encap, EBT_VLAN_ENCAP);
|
|
|
|
|
2008-10-08 03:35:13 -06:00
|
|
|
return true;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2010-03-19 10:16:42 -06:00
|
|
|
static int ebt_vlan_mt_check(const struct xt_mtchk_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
struct ebt_vlan_info *info = par->matchinfo;
|
|
|
|
const struct ebt_entry *e = par->entryinfo;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* Is it 802.1Q frame checked? */
|
|
|
|
if (e->ethproto != htons(ETH_P_8021Q)) {
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("passed entry proto %2.4X is not 802.1Q (8100)\n",
|
|
|
|
ntohs(e->ethproto));
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for bitmask range
|
2015-10-26 03:10:41 -06:00
|
|
|
* True if even one bit is out of mask
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
if (info->bitmask & ~EBT_VLAN_MASK) {
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("bitmask %2X is out of mask (%2X)\n",
|
|
|
|
info->bitmask, EBT_VLAN_MASK);
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for inversion flags range */
|
|
|
|
if (info->invflags & ~EBT_VLAN_MASK) {
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("inversion flags %2X is out of mask (%2X)\n",
|
|
|
|
info->invflags, EBT_VLAN_MASK);
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reserved VLAN ID (VID) values
|
|
|
|
* -----------------------------
|
2007-02-09 07:24:35 -07:00
|
|
|
* 0 - The null VLAN ID.
|
2005-04-16 16:20:36 -06:00
|
|
|
* 1 - The default Port VID (PVID)
|
2007-02-09 07:24:35 -07:00
|
|
|
* 0x0FFF - Reserved for implementation use.
|
2015-10-26 03:10:41 -06:00
|
|
|
* if_vlan.h: VLAN_N_VID 4096.
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
if (GET_BITMASK(EBT_VLAN_ID)) {
|
|
|
|
if (!!info->id) { /* if id!=0 => check vid range */
|
2010-10-20 07:56:02 -06:00
|
|
|
if (info->id > VLAN_N_VID) {
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("id %d is out of range (1-4096)\n",
|
|
|
|
info->id);
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
/* Note: This is valid VLAN-tagged frame point.
|
2007-02-09 07:24:35 -07:00
|
|
|
* Any value of user_priority are acceptable,
|
2005-04-16 16:20:36 -06:00
|
|
|
* but should be ignored according to 802.1Q Std.
|
2015-10-26 03:10:41 -06:00
|
|
|
* So we just drop the prio flag.
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
info->bitmask &= ~EBT_VLAN_PRIO;
|
|
|
|
}
|
|
|
|
/* Else, id=0 (null VLAN ID) => user_priority range (any?) */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GET_BITMASK(EBT_VLAN_PRIO)) {
|
|
|
|
if ((unsigned char) info->prio > 7) {
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("prio %d is out of range (0-7)\n",
|
|
|
|
info->prio);
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Check for encapsulated proto range - it is possible to be
|
|
|
|
* any value for u_short range.
|
2015-10-26 03:10:41 -06:00
|
|
|
* if_ether.h: ETH_ZLEN 60 - Min. octets in frame sans FCS
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
if (GET_BITMASK(EBT_VLAN_ENCAP)) {
|
|
|
|
if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) {
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("encap frame length %d is less than "
|
|
|
|
"minimal\n", ntohs(info->encap));
|
2010-03-23 09:35:56 -06:00
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-23 09:35:56 -06:00
|
|
|
return 0;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-10-08 03:35:15 -06:00
|
|
|
static struct xt_match ebt_vlan_mt_reg __read_mostly = {
|
|
|
|
.name = "vlan",
|
2008-10-08 03:35:14 -06:00
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_BRIDGE,
|
2008-10-08 03:35:15 -06:00
|
|
|
.match = ebt_vlan_mt,
|
|
|
|
.checkentry = ebt_vlan_mt_check,
|
2010-02-15 10:16:26 -07:00
|
|
|
.matchsize = sizeof(struct ebt_vlan_info),
|
2005-04-16 16:20:36 -06:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2006-03-28 17:37:06 -07:00
|
|
|
static int __init ebt_vlan_init(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("ebtables 802.1Q extension module v" MODULE_VERS "\n");
|
2008-10-08 03:35:15 -06:00
|
|
|
return xt_register_match(&ebt_vlan_mt_reg);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2006-03-28 17:37:06 -07:00
|
|
|
static void __exit ebt_vlan_fini(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:15 -06:00
|
|
|
xt_unregister_match(&ebt_vlan_mt_reg);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2006-03-28 17:37:06 -07:00
|
|
|
module_init(ebt_vlan_init);
|
|
|
|
module_exit(ebt_vlan_fini);
|