1 // SPDX-License-Identifier: GPL-2.0-only
6 * Bart De Schuymer <bdschuym@pandora.be>
7 * Stephen Hemminger <shemminger@osdl.org>
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>
17 #define BPDU_TYPE_CONFIG 0
18 #define BPDU_TYPE_TCN 0x80
29 struct stp_config_pdu {
41 #define NR16(p) (p[0] << 8 | p[1])
42 #define NR32(p) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])
44 static bool ebt_filter_config(const struct ebt_stp_info *info,
45 const struct stp_config_pdu *stpc)
47 const struct ebt_stp_config_info *c;
52 if ((info->bitmask & EBT_STP_FLAGS) &&
53 NF_INVF(info, EBT_STP_FLAGS, c->flags != stpc->flags))
55 if (info->bitmask & EBT_STP_ROOTPRIO) {
56 v16 = NR16(stpc->root);
57 if (NF_INVF(info, EBT_STP_ROOTPRIO,
58 v16 < c->root_priol || v16 > c->root_priou))
61 if (info->bitmask & EBT_STP_ROOTADDR) {
62 if (NF_INVF(info, EBT_STP_ROOTADDR,
63 !ether_addr_equal_masked(&stpc->root[2],
68 if (info->bitmask & EBT_STP_ROOTCOST) {
69 v32 = NR32(stpc->root_cost);
70 if (NF_INVF(info, EBT_STP_ROOTCOST,
71 v32 < c->root_costl || v32 > c->root_costu))
74 if (info->bitmask & EBT_STP_SENDERPRIO) {
75 v16 = NR16(stpc->sender);
76 if (NF_INVF(info, EBT_STP_SENDERPRIO,
77 v16 < c->sender_priol || v16 > c->sender_priou))
80 if (info->bitmask & EBT_STP_SENDERADDR) {
81 if (NF_INVF(info, EBT_STP_SENDERADDR,
82 !ether_addr_equal_masked(&stpc->sender[2],
87 if (info->bitmask & EBT_STP_PORT) {
88 v16 = NR16(stpc->port);
89 if (NF_INVF(info, EBT_STP_PORT,
90 v16 < c->portl || v16 > c->portu))
93 if (info->bitmask & EBT_STP_MSGAGE) {
94 v16 = NR16(stpc->msg_age);
95 if (NF_INVF(info, EBT_STP_MSGAGE,
96 v16 < c->msg_agel || v16 > c->msg_ageu))
99 if (info->bitmask & EBT_STP_MAXAGE) {
100 v16 = NR16(stpc->max_age);
101 if (NF_INVF(info, EBT_STP_MAXAGE,
102 v16 < c->max_agel || v16 > c->max_ageu))
105 if (info->bitmask & EBT_STP_HELLOTIME) {
106 v16 = NR16(stpc->hello_time);
107 if (NF_INVF(info, EBT_STP_HELLOTIME,
108 v16 < c->hello_timel || v16 > c->hello_timeu))
111 if (info->bitmask & EBT_STP_FWDD) {
112 v16 = NR16(stpc->forward_delay);
113 if (NF_INVF(info, EBT_STP_FWDD,
114 v16 < c->forward_delayl || v16 > c->forward_delayu))
121 ebt_stp_mt(const struct sk_buff *skb, struct xt_action_param *par)
123 const struct ebt_stp_info *info = par->matchinfo;
124 const struct stp_header *sp;
125 struct stp_header _stph;
126 const u8 header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
128 sp = skb_header_pointer(skb, 0, sizeof(_stph), &_stph);
132 /* The stp code only considers these */
133 if (memcmp(sp, header, sizeof(header)))
136 if ((info->bitmask & EBT_STP_TYPE) &&
137 NF_INVF(info, EBT_STP_TYPE, info->type != sp->type))
140 if (sp->type == BPDU_TYPE_CONFIG &&
141 info->bitmask & EBT_STP_CONFIG_MASK) {
142 const struct stp_config_pdu *st;
143 struct stp_config_pdu _stpc;
145 st = skb_header_pointer(skb, sizeof(_stph),
146 sizeof(_stpc), &_stpc);
149 return ebt_filter_config(info, st);
154 static int ebt_stp_mt_check(const struct xt_mtchk_param *par)
156 const struct ebt_stp_info *info = par->matchinfo;
157 const struct ebt_entry *e = par->entryinfo;
159 if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
160 !(info->bitmask & EBT_STP_MASK))
162 /* Make sure the match only receives stp frames */
163 if (!par->nft_compat &&
164 (!ether_addr_equal(e->destmac, eth_stp_addr) ||
165 !(e->bitmask & EBT_DESTMAC) ||
166 !is_broadcast_ether_addr(e->destmsk)))
172 static struct xt_match ebt_stp_mt_reg __read_mostly = {
175 .family = NFPROTO_BRIDGE,
177 .checkentry = ebt_stp_mt_check,
178 .matchsize = sizeof(struct ebt_stp_info),
182 static int __init ebt_stp_init(void)
184 return xt_register_match(&ebt_stp_mt_reg);
187 static void __exit ebt_stp_fini(void)
189 xt_unregister_match(&ebt_stp_mt_reg);
192 module_init(ebt_stp_init);
193 module_exit(ebt_stp_fini);
194 MODULE_DESCRIPTION("Ebtables: Spanning Tree Protocol packet match");
195 MODULE_LICENSE("GPL");