ebt_vlan.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/if_ether.h>
  20. #include <linux/if_vlan.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/netfilter/x_tables.h>
  24. #include <linux/netfilter_bridge/ebtables.h>
  25. #include <linux/netfilter_bridge/ebt_vlan.h>
  26. #define MODULE_VERS "0.6"
  27. MODULE_AUTHOR("Nick Fedchik <nick@fedchik.org.ua>");
  28. MODULE_DESCRIPTION("Ebtables: 802.1Q VLAN tag match");
  29. MODULE_LICENSE("GPL");
  30. #define GET_BITMASK(_BIT_MASK_) info->bitmask & _BIT_MASK_
  31. #define EXIT_ON_MISMATCH(_MATCH_,_MASK_) {if (!((info->_MATCH_ == _MATCH_)^!!(info->invflags & _MASK_))) return false; }
  32. static bool
  33. ebt_vlan_mt(const struct sk_buff *skb, struct xt_action_param *par)
  34. {
  35. const struct ebt_vlan_info *info = par->matchinfo;
  36. unsigned short TCI; /* Whole TCI, given from parsed frame */
  37. unsigned short id; /* VLAN ID, given from frame TCI */
  38. unsigned char prio; /* user_priority, given from frame TCI */
  39. /* VLAN encapsulated Type/Length field, given from orig frame */
  40. __be16 encap;
  41. if (skb_vlan_tag_present(skb)) {
  42. TCI = skb_vlan_tag_get(skb);
  43. encap = skb->protocol;
  44. } else {
  45. const struct vlan_hdr *fp;
  46. struct vlan_hdr _frame;
  47. fp = skb_header_pointer(skb, 0, sizeof(_frame), &_frame);
  48. if (fp == NULL)
  49. return false;
  50. TCI = ntohs(fp->h_vlan_TCI);
  51. encap = fp->h_vlan_encapsulated_proto;
  52. }
  53. /* Tag Control Information (TCI) consists of the following elements:
  54. * - User_priority. The user_priority field is three bits in length,
  55. * interpreted as a binary number.
  56. * - Canonical Format Indicator (CFI). The Canonical Format Indicator
  57. * (CFI) is a single bit flag value. Currently ignored.
  58. * - VLAN Identifier (VID). The VID is encoded as
  59. * an unsigned binary number. */
  60. id = TCI & VLAN_VID_MASK;
  61. prio = (TCI >> 13) & 0x7;
  62. /* Checking VLAN Identifier (VID) */
  63. if (GET_BITMASK(EBT_VLAN_ID))
  64. EXIT_ON_MISMATCH(id, EBT_VLAN_ID);
  65. /* Checking user_priority */
  66. if (GET_BITMASK(EBT_VLAN_PRIO))
  67. EXIT_ON_MISMATCH(prio, EBT_VLAN_PRIO);
  68. /* Checking Encapsulated Proto (Length/Type) field */
  69. if (GET_BITMASK(EBT_VLAN_ENCAP))
  70. EXIT_ON_MISMATCH(encap, EBT_VLAN_ENCAP);
  71. return true;
  72. }
  73. static int ebt_vlan_mt_check(const struct xt_mtchk_param *par)
  74. {
  75. struct ebt_vlan_info *info = par->matchinfo;
  76. const struct ebt_entry *e = par->entryinfo;
  77. /* Is it 802.1Q frame checked? */
  78. if (e->ethproto != htons(ETH_P_8021Q)) {
  79. pr_debug("passed entry proto %2.4X is not 802.1Q (8100)\n",
  80. ntohs(e->ethproto));
  81. return -EINVAL;
  82. }
  83. /* Check for bitmask range
  84. * True if even one bit is out of mask */
  85. if (info->bitmask & ~EBT_VLAN_MASK) {
  86. pr_debug("bitmask %2X is out of mask (%2X)\n",
  87. info->bitmask, EBT_VLAN_MASK);
  88. return -EINVAL;
  89. }
  90. /* Check for inversion flags range */
  91. if (info->invflags & ~EBT_VLAN_MASK) {
  92. pr_debug("inversion flags %2X is out of mask (%2X)\n",
  93. info->invflags, EBT_VLAN_MASK);
  94. return -EINVAL;
  95. }
  96. /* Reserved VLAN ID (VID) values
  97. * -----------------------------
  98. * 0 - The null VLAN ID.
  99. * 1 - The default Port VID (PVID)
  100. * 0x0FFF - Reserved for implementation use.
  101. * if_vlan.h: VLAN_N_VID 4096. */
  102. if (GET_BITMASK(EBT_VLAN_ID)) {
  103. if (!!info->id) { /* if id!=0 => check vid range */
  104. if (info->id > VLAN_N_VID) {
  105. pr_debug("id %d is out of range (1-4096)\n",
  106. info->id);
  107. return -EINVAL;
  108. }
  109. /* Note: This is valid VLAN-tagged frame point.
  110. * Any value of user_priority are acceptable,
  111. * but should be ignored according to 802.1Q Std.
  112. * So we just drop the prio flag. */
  113. info->bitmask &= ~EBT_VLAN_PRIO;
  114. }
  115. /* Else, id=0 (null VLAN ID) => user_priority range (any?) */
  116. }
  117. if (GET_BITMASK(EBT_VLAN_PRIO)) {
  118. if ((unsigned char) info->prio > 7) {
  119. pr_debug("prio %d is out of range (0-7)\n",
  120. info->prio);
  121. return -EINVAL;
  122. }
  123. }
  124. /* Check for encapsulated proto range - it is possible to be
  125. * any value for u_short range.
  126. * if_ether.h: ETH_ZLEN 60 - Min. octets in frame sans FCS */
  127. if (GET_BITMASK(EBT_VLAN_ENCAP)) {
  128. if ((unsigned short) ntohs(info->encap) < ETH_ZLEN) {
  129. pr_debug("encap frame length %d is less than "
  130. "minimal\n", ntohs(info->encap));
  131. return -EINVAL;
  132. }
  133. }
  134. return 0;
  135. }
  136. static struct xt_match ebt_vlan_mt_reg __read_mostly = {
  137. .name = "vlan",
  138. .revision = 0,
  139. .family = NFPROTO_BRIDGE,
  140. .match = ebt_vlan_mt,
  141. .checkentry = ebt_vlan_mt_check,
  142. .matchsize = sizeof(struct ebt_vlan_info),
  143. .me = THIS_MODULE,
  144. };
  145. static int __init ebt_vlan_init(void)
  146. {
  147. pr_debug("ebtables 802.1Q extension module v" MODULE_VERS "\n");
  148. return xt_register_match(&ebt_vlan_mt_reg);
  149. }
  150. static void __exit ebt_vlan_fini(void)
  151. {
  152. xt_unregister_match(&ebt_vlan_mt_reg);
  153. }
  154. module_init(ebt_vlan_init);
  155. module_exit(ebt_vlan_fini);