Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Description: EBTables 802.1Q match extension kernelspace module.
0004  * Authors: Nick Fedchik <nick@fedchik.org.ua>
0005  *          Bart De Schuymer <bdschuym@pandora.be>
0006  */
0007 
0008 #include <linux/if_ether.h>
0009 #include <linux/if_vlan.h>
0010 #include <linux/module.h>
0011 #include <linux/moduleparam.h>
0012 #include <linux/netfilter/x_tables.h>
0013 #include <linux/netfilter_bridge/ebtables.h>
0014 #include <linux/netfilter_bridge/ebt_vlan.h>
0015 
0016 #define MODULE_VERS "0.6"
0017 
0018 MODULE_AUTHOR("Nick Fedchik <nick@fedchik.org.ua>");
0019 MODULE_DESCRIPTION("Ebtables: 802.1Q VLAN tag match");
0020 MODULE_LICENSE("GPL");
0021 
0022 #define GET_BITMASK(_BIT_MASK_) info->bitmask & _BIT_MASK_
0023 #define EXIT_ON_MISMATCH(_MATCH_,_MASK_) {if (!((info->_MATCH_ == _MATCH_)^!!(info->invflags & _MASK_))) return false; }
0024 
0025 static bool
0026 ebt_vlan_mt(const struct sk_buff *skb, struct xt_action_param *par)
0027 {
0028     const struct ebt_vlan_info *info = par->matchinfo;
0029 
0030     unsigned short TCI; /* Whole TCI, given from parsed frame */
0031     unsigned short id;  /* VLAN ID, given from frame TCI */
0032     unsigned char prio; /* user_priority, given from frame TCI */
0033     /* VLAN encapsulated Type/Length field, given from orig frame */
0034     __be16 encap;
0035 
0036     if (skb_vlan_tag_present(skb)) {
0037         TCI = skb_vlan_tag_get(skb);
0038         encap = skb->protocol;
0039     } else {
0040         const struct vlan_hdr *fp;
0041         struct vlan_hdr _frame;
0042 
0043         fp = skb_header_pointer(skb, 0, sizeof(_frame), &_frame);
0044         if (fp == NULL)
0045             return false;
0046 
0047         TCI = ntohs(fp->h_vlan_TCI);
0048         encap = fp->h_vlan_encapsulated_proto;
0049     }
0050 
0051     /* Tag Control Information (TCI) consists of the following elements:
0052      * - User_priority. The user_priority field is three bits in length,
0053      * interpreted as a binary number.
0054      * - Canonical Format Indicator (CFI). The Canonical Format Indicator
0055      * (CFI) is a single bit flag value. Currently ignored.
0056      * - VLAN Identifier (VID). The VID is encoded as
0057      * an unsigned binary number.
0058      */
0059     id = TCI & VLAN_VID_MASK;
0060     prio = (TCI >> 13) & 0x7;
0061 
0062     /* Checking VLAN Identifier (VID) */
0063     if (GET_BITMASK(EBT_VLAN_ID))
0064         EXIT_ON_MISMATCH(id, EBT_VLAN_ID);
0065 
0066     /* Checking user_priority */
0067     if (GET_BITMASK(EBT_VLAN_PRIO))
0068         EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO);
0069 
0070     /* Checking Encapsulated Proto (Length/Type) field */
0071     if (GET_BITMASK(EBT_VLAN_ENCAP))
0072         EXIT_ON_MISMATCH(encap, EBT_VLAN_ENCAP);
0073 
0074     return true;
0075 }
0076 
0077 static int ebt_vlan_mt_check(const struct xt_mtchk_param *par)
0078 {
0079     struct ebt_vlan_info *info = par->matchinfo;
0080     const struct ebt_entry *e = par->entryinfo;
0081 
0082     /* Is it 802.1Q frame checked? */
0083     if (e->ethproto != htons(ETH_P_8021Q)) {
0084         pr_debug("passed entry proto %2.4X is not 802.1Q (8100)\n",
0085              ntohs(e->ethproto));
0086         return -EINVAL;
0087     }
0088 
0089     /* Check for bitmask range
0090      * True if even one bit is out of mask
0091      */
0092     if (info->bitmask & ~EBT_VLAN_MASK) {
0093         pr_debug("bitmask %2X is out of mask (%2X)\n",
0094              info->bitmask, EBT_VLAN_MASK);
0095         return -EINVAL;
0096     }
0097 
0098     /* Check for inversion flags range */
0099     if (info->invflags & ~EBT_VLAN_MASK) {
0100         pr_debug("inversion flags %2X is out of mask (%2X)\n",
0101              info->invflags, EBT_VLAN_MASK);
0102         return -EINVAL;
0103     }
0104 
0105     /* Reserved VLAN ID (VID) values
0106      * -----------------------------
0107      * 0 - The null VLAN ID.
0108      * 1 - The default Port VID (PVID)
0109      * 0x0FFF - Reserved for implementation use.
0110      * if_vlan.h: VLAN_N_VID 4096.
0111      */
0112     if (GET_BITMASK(EBT_VLAN_ID)) {
0113         if (!!info->id) { /* if id!=0 => check vid range */
0114             if (info->id > VLAN_N_VID) {
0115                 pr_debug("id %d is out of range (1-4096)\n",
0116                      info->id);
0117                 return -EINVAL;
0118             }
0119             /* Note: This is valid VLAN-tagged frame point.
0120              * Any value of user_priority are acceptable,
0121              * but should be ignored according to 802.1Q Std.
0122              * So we just drop the prio flag.
0123              */
0124             info->bitmask &= ~EBT_VLAN_PRIO;
0125         }
0126         /* Else, id=0 (null VLAN ID)  => user_priority range (any?) */
0127     }
0128 
0129     if (GET_BITMASK(EBT_VLAN_PRIO)) {
0130         if ((unsigned char) info->prio > 7) {
0131             pr_debug("prio %d is out of range (0-7)\n",
0132                  info->prio);
0133             return -EINVAL;
0134         }
0135     }
0136     /* Check for encapsulated proto range - it is possible to be
0137      * any value for u_short range.
0138      * if_ether.h:  ETH_ZLEN        60   -  Min. octets in frame sans FCS
0139      */
0140     if (GET_BITMASK(EBT_VLAN_ENCAP)) {
0141         if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) {
0142             pr_debug("encap frame length %d is less than "
0143                  "minimal\n", ntohs(info->encap));
0144             return -EINVAL;
0145         }
0146     }
0147 
0148     return 0;
0149 }
0150 
0151 static struct xt_match ebt_vlan_mt_reg __read_mostly = {
0152     .name       = "vlan",
0153     .revision   = 0,
0154     .family     = NFPROTO_BRIDGE,
0155     .match      = ebt_vlan_mt,
0156     .checkentry = ebt_vlan_mt_check,
0157     .matchsize  = sizeof(struct ebt_vlan_info),
0158     .me     = THIS_MODULE,
0159 };
0160 
0161 static int __init ebt_vlan_init(void)
0162 {
0163     pr_debug("ebtables 802.1Q extension module v" MODULE_VERS "\n");
0164     return xt_register_match(&ebt_vlan_mt_reg);
0165 }
0166 
0167 static void __exit ebt_vlan_fini(void)
0168 {
0169     xt_unregister_match(&ebt_vlan_mt_reg);
0170 }
0171 
0172 module_init(ebt_vlan_init);
0173 module_exit(ebt_vlan_fini);