0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/etherdevice.h>
0012 #include <linux/module.h>
0013 #include <linux/netfilter/x_tables.h>
0014 #include <linux/netfilter_bridge/ebtables.h>
0015 #include <linux/netfilter_bridge/ebt_stp.h>
0016
0017 #define BPDU_TYPE_CONFIG 0
0018
0019 struct stp_header {
0020 u8 dsap;
0021 u8 ssap;
0022 u8 ctrl;
0023 u8 pid;
0024 u8 vers;
0025 u8 type;
0026 };
0027
0028 struct stp_config_pdu {
0029 u8 flags;
0030 u8 root[8];
0031 u8 root_cost[4];
0032 u8 sender[8];
0033 u8 port[2];
0034 u8 msg_age[2];
0035 u8 max_age[2];
0036 u8 hello_time[2];
0037 u8 forward_delay[2];
0038 };
0039
0040 #define NR16(p) (p[0] << 8 | p[1])
0041 #define NR32(p) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])
0042
0043 static bool ebt_filter_config(const struct ebt_stp_info *info,
0044 const struct stp_config_pdu *stpc)
0045 {
0046 const struct ebt_stp_config_info *c;
0047 u16 v16;
0048 u32 v32;
0049
0050 c = &info->config;
0051 if ((info->bitmask & EBT_STP_FLAGS) &&
0052 NF_INVF(info, EBT_STP_FLAGS, c->flags != stpc->flags))
0053 return false;
0054 if (info->bitmask & EBT_STP_ROOTPRIO) {
0055 v16 = NR16(stpc->root);
0056 if (NF_INVF(info, EBT_STP_ROOTPRIO,
0057 v16 < c->root_priol || v16 > c->root_priou))
0058 return false;
0059 }
0060 if (info->bitmask & EBT_STP_ROOTADDR) {
0061 if (NF_INVF(info, EBT_STP_ROOTADDR,
0062 !ether_addr_equal_masked(&stpc->root[2],
0063 c->root_addr,
0064 c->root_addrmsk)))
0065 return false;
0066 }
0067 if (info->bitmask & EBT_STP_ROOTCOST) {
0068 v32 = NR32(stpc->root_cost);
0069 if (NF_INVF(info, EBT_STP_ROOTCOST,
0070 v32 < c->root_costl || v32 > c->root_costu))
0071 return false;
0072 }
0073 if (info->bitmask & EBT_STP_SENDERPRIO) {
0074 v16 = NR16(stpc->sender);
0075 if (NF_INVF(info, EBT_STP_SENDERPRIO,
0076 v16 < c->sender_priol || v16 > c->sender_priou))
0077 return false;
0078 }
0079 if (info->bitmask & EBT_STP_SENDERADDR) {
0080 if (NF_INVF(info, EBT_STP_SENDERADDR,
0081 !ether_addr_equal_masked(&stpc->sender[2],
0082 c->sender_addr,
0083 c->sender_addrmsk)))
0084 return false;
0085 }
0086 if (info->bitmask & EBT_STP_PORT) {
0087 v16 = NR16(stpc->port);
0088 if (NF_INVF(info, EBT_STP_PORT,
0089 v16 < c->portl || v16 > c->portu))
0090 return false;
0091 }
0092 if (info->bitmask & EBT_STP_MSGAGE) {
0093 v16 = NR16(stpc->msg_age);
0094 if (NF_INVF(info, EBT_STP_MSGAGE,
0095 v16 < c->msg_agel || v16 > c->msg_ageu))
0096 return false;
0097 }
0098 if (info->bitmask & EBT_STP_MAXAGE) {
0099 v16 = NR16(stpc->max_age);
0100 if (NF_INVF(info, EBT_STP_MAXAGE,
0101 v16 < c->max_agel || v16 > c->max_ageu))
0102 return false;
0103 }
0104 if (info->bitmask & EBT_STP_HELLOTIME) {
0105 v16 = NR16(stpc->hello_time);
0106 if (NF_INVF(info, EBT_STP_HELLOTIME,
0107 v16 < c->hello_timel || v16 > c->hello_timeu))
0108 return false;
0109 }
0110 if (info->bitmask & EBT_STP_FWDD) {
0111 v16 = NR16(stpc->forward_delay);
0112 if (NF_INVF(info, EBT_STP_FWDD,
0113 v16 < c->forward_delayl || v16 > c->forward_delayu))
0114 return false;
0115 }
0116 return true;
0117 }
0118
0119 static bool
0120 ebt_stp_mt(const struct sk_buff *skb, struct xt_action_param *par)
0121 {
0122 const struct ebt_stp_info *info = par->matchinfo;
0123 const struct stp_header *sp;
0124 struct stp_header _stph;
0125 const u8 header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
0126
0127 sp = skb_header_pointer(skb, 0, sizeof(_stph), &_stph);
0128 if (sp == NULL)
0129 return false;
0130
0131
0132 if (memcmp(sp, header, sizeof(header)))
0133 return false;
0134
0135 if ((info->bitmask & EBT_STP_TYPE) &&
0136 NF_INVF(info, EBT_STP_TYPE, info->type != sp->type))
0137 return false;
0138
0139 if (sp->type == BPDU_TYPE_CONFIG &&
0140 info->bitmask & EBT_STP_CONFIG_MASK) {
0141 const struct stp_config_pdu *st;
0142 struct stp_config_pdu _stpc;
0143
0144 st = skb_header_pointer(skb, sizeof(_stph),
0145 sizeof(_stpc), &_stpc);
0146 if (st == NULL)
0147 return false;
0148 return ebt_filter_config(info, st);
0149 }
0150 return true;
0151 }
0152
0153 static int ebt_stp_mt_check(const struct xt_mtchk_param *par)
0154 {
0155 const struct ebt_stp_info *info = par->matchinfo;
0156 const struct ebt_entry *e = par->entryinfo;
0157
0158 if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
0159 !(info->bitmask & EBT_STP_MASK))
0160 return -EINVAL;
0161
0162 if (!par->nft_compat &&
0163 (!ether_addr_equal(e->destmac, eth_stp_addr) ||
0164 !(e->bitmask & EBT_DESTMAC) ||
0165 !is_broadcast_ether_addr(e->destmsk)))
0166 return -EINVAL;
0167
0168 return 0;
0169 }
0170
0171 static struct xt_match ebt_stp_mt_reg __read_mostly = {
0172 .name = "stp",
0173 .revision = 0,
0174 .family = NFPROTO_BRIDGE,
0175 .match = ebt_stp_mt,
0176 .checkentry = ebt_stp_mt_check,
0177 .matchsize = sizeof(struct ebt_stp_info),
0178 .me = THIS_MODULE,
0179 };
0180
0181 static int __init ebt_stp_init(void)
0182 {
0183 return xt_register_match(&ebt_stp_mt_reg);
0184 }
0185
0186 static void __exit ebt_stp_fini(void)
0187 {
0188 xt_unregister_match(&ebt_stp_mt_reg);
0189 }
0190
0191 module_init(ebt_stp_init);
0192 module_exit(ebt_stp_fini);
0193 MODULE_DESCRIPTION("Ebtables: Spanning Tree Protocol packet match");
0194 MODULE_LICENSE("GPL");