ebt_stp.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * ebt_stp
  3. *
  4. * Authors:
  5. * Bart De Schuymer <bdschuym@pandora.be>
  6. * Stephen Hemminger <shemminger@osdl.org>
  7. *
  8. * July, 2003
  9. */
  10. #include <linux/etherdevice.h>
  11. #include <linux/module.h>
  12. #include <linux/netfilter/x_tables.h>
  13. #include <linux/netfilter_bridge/ebtables.h>
  14. #include <linux/netfilter_bridge/ebt_stp.h>
  15. #define BPDU_TYPE_CONFIG 0
  16. #define BPDU_TYPE_TCN 0x80
  17. struct stp_header {
  18. uint8_t dsap;
  19. uint8_t ssap;
  20. uint8_t ctrl;
  21. uint8_t pid;
  22. uint8_t vers;
  23. uint8_t type;
  24. };
  25. struct stp_config_pdu {
  26. uint8_t flags;
  27. uint8_t root[8];
  28. uint8_t root_cost[4];
  29. uint8_t sender[8];
  30. uint8_t port[2];
  31. uint8_t msg_age[2];
  32. uint8_t max_age[2];
  33. uint8_t hello_time[2];
  34. uint8_t forward_delay[2];
  35. };
  36. #define NR16(p) (p[0] << 8 | p[1])
  37. #define NR32(p) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])
  38. static bool ebt_filter_config(const struct ebt_stp_info *info,
  39. const struct stp_config_pdu *stpc)
  40. {
  41. const struct ebt_stp_config_info *c;
  42. uint16_t v16;
  43. uint32_t v32;
  44. int verdict, i;
  45. c = &info->config;
  46. if ((info->bitmask & EBT_STP_FLAGS) &&
  47. FWINV(c->flags != stpc->flags, EBT_STP_FLAGS))
  48. return false;
  49. if (info->bitmask & EBT_STP_ROOTPRIO) {
  50. v16 = NR16(stpc->root);
  51. if (FWINV(v16 < c->root_priol ||
  52. v16 > c->root_priou, EBT_STP_ROOTPRIO))
  53. return false;
  54. }
  55. if (info->bitmask & EBT_STP_ROOTADDR) {
  56. verdict = 0;
  57. for (i = 0; i < 6; i++)
  58. verdict |= (stpc->root[2+i] ^ c->root_addr[i]) &
  59. c->root_addrmsk[i];
  60. if (FWINV(verdict != 0, EBT_STP_ROOTADDR))
  61. return false;
  62. }
  63. if (info->bitmask & EBT_STP_ROOTCOST) {
  64. v32 = NR32(stpc->root_cost);
  65. if (FWINV(v32 < c->root_costl ||
  66. v32 > c->root_costu, EBT_STP_ROOTCOST))
  67. return false;
  68. }
  69. if (info->bitmask & EBT_STP_SENDERPRIO) {
  70. v16 = NR16(stpc->sender);
  71. if (FWINV(v16 < c->sender_priol ||
  72. v16 > c->sender_priou, EBT_STP_SENDERPRIO))
  73. return false;
  74. }
  75. if (info->bitmask & EBT_STP_SENDERADDR) {
  76. verdict = 0;
  77. for (i = 0; i < 6; i++)
  78. verdict |= (stpc->sender[2+i] ^ c->sender_addr[i]) &
  79. c->sender_addrmsk[i];
  80. if (FWINV(verdict != 0, EBT_STP_SENDERADDR))
  81. return false;
  82. }
  83. if (info->bitmask & EBT_STP_PORT) {
  84. v16 = NR16(stpc->port);
  85. if (FWINV(v16 < c->portl ||
  86. v16 > c->portu, EBT_STP_PORT))
  87. return false;
  88. }
  89. if (info->bitmask & EBT_STP_MSGAGE) {
  90. v16 = NR16(stpc->msg_age);
  91. if (FWINV(v16 < c->msg_agel ||
  92. v16 > c->msg_ageu, EBT_STP_MSGAGE))
  93. return false;
  94. }
  95. if (info->bitmask & EBT_STP_MAXAGE) {
  96. v16 = NR16(stpc->max_age);
  97. if (FWINV(v16 < c->max_agel ||
  98. v16 > c->max_ageu, EBT_STP_MAXAGE))
  99. return false;
  100. }
  101. if (info->bitmask & EBT_STP_HELLOTIME) {
  102. v16 = NR16(stpc->hello_time);
  103. if (FWINV(v16 < c->hello_timel ||
  104. v16 > c->hello_timeu, EBT_STP_HELLOTIME))
  105. return false;
  106. }
  107. if (info->bitmask & EBT_STP_FWDD) {
  108. v16 = NR16(stpc->forward_delay);
  109. if (FWINV(v16 < c->forward_delayl ||
  110. v16 > c->forward_delayu, EBT_STP_FWDD))
  111. return false;
  112. }
  113. return true;
  114. }
  115. static bool
  116. ebt_stp_mt(const struct sk_buff *skb, struct xt_action_param *par)
  117. {
  118. const struct ebt_stp_info *info = par->matchinfo;
  119. const struct stp_header *sp;
  120. struct stp_header _stph;
  121. const uint8_t header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
  122. sp = skb_header_pointer(skb, 0, sizeof(_stph), &_stph);
  123. if (sp == NULL)
  124. return false;
  125. /* The stp code only considers these */
  126. if (memcmp(sp, header, sizeof(header)))
  127. return false;
  128. if (info->bitmask & EBT_STP_TYPE &&
  129. FWINV(info->type != sp->type, EBT_STP_TYPE))
  130. return false;
  131. if (sp->type == BPDU_TYPE_CONFIG &&
  132. info->bitmask & EBT_STP_CONFIG_MASK) {
  133. const struct stp_config_pdu *st;
  134. struct stp_config_pdu _stpc;
  135. st = skb_header_pointer(skb, sizeof(_stph),
  136. sizeof(_stpc), &_stpc);
  137. if (st == NULL)
  138. return false;
  139. return ebt_filter_config(info, st);
  140. }
  141. return true;
  142. }
  143. static int ebt_stp_mt_check(const struct xt_mtchk_param *par)
  144. {
  145. const struct ebt_stp_info *info = par->matchinfo;
  146. const uint8_t bridge_ula[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00};
  147. const uint8_t msk[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  148. const struct ebt_entry *e = par->entryinfo;
  149. if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
  150. !(info->bitmask & EBT_STP_MASK))
  151. return -EINVAL;
  152. /* Make sure the match only receives stp frames */
  153. if (!par->nft_compat &&
  154. (!ether_addr_equal(e->destmac, bridge_ula) ||
  155. !ether_addr_equal(e->destmsk, msk) ||
  156. !(e->bitmask & EBT_DESTMAC)))
  157. return -EINVAL;
  158. return 0;
  159. }
  160. static struct xt_match ebt_stp_mt_reg __read_mostly = {
  161. .name = "stp",
  162. .revision = 0,
  163. .family = NFPROTO_BRIDGE,
  164. .match = ebt_stp_mt,
  165. .checkentry = ebt_stp_mt_check,
  166. .matchsize = sizeof(struct ebt_stp_info),
  167. .me = THIS_MODULE,
  168. };
  169. static int __init ebt_stp_init(void)
  170. {
  171. return xt_register_match(&ebt_stp_mt_reg);
  172. }
  173. static void __exit ebt_stp_fini(void)
  174. {
  175. xt_unregister_match(&ebt_stp_mt_reg);
  176. }
  177. module_init(ebt_stp_init);
  178. module_exit(ebt_stp_fini);
  179. MODULE_DESCRIPTION("Ebtables: Spanning Tree Protocol packet match");
  180. MODULE_LICENSE("GPL");