0001
0002
0003
0004
0005
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;
0031 unsigned short id;
0032 unsigned char prio;
0033
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
0052
0053
0054
0055
0056
0057
0058
0059 id = TCI & VLAN_VID_MASK;
0060 prio = (TCI >> 13) & 0x7;
0061
0062
0063 if (GET_BITMASK(EBT_VLAN_ID))
0064 EXIT_ON_MISMATCH(id, EBT_VLAN_ID);
0065
0066
0067 if (GET_BITMASK(EBT_VLAN_PRIO))
0068 EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO);
0069
0070
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
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
0090
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
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
0106
0107
0108
0109
0110
0111
0112 if (GET_BITMASK(EBT_VLAN_ID)) {
0113 if (!!info->id) {
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
0120
0121
0122
0123
0124 info->bitmask &= ~EBT_VLAN_PRIO;
0125 }
0126
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
0137
0138
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);