ebt_stp.c 4.7 KB

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