blob: 3140eb912d7eb41bab790932aedbcb953539b76a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ebt_stp
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
6 * Stephen Hemminger <shemminger@osdl.org>
7 *
8 * July, 2003
9 */
David S. Miller82bf7e92006-01-11 15:38:28 -080010#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
Jan Engelhardt18219d32008-10-08 11:35:13 +020012#include <linux/netfilter/x_tables.h>
13#include <linux/netfilter_bridge/ebtables.h>
14#include <linux/netfilter_bridge/ebt_stp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#define BPDU_TYPE_CONFIG 0
17#define BPDU_TYPE_TCN 0x80
18
19struct stp_header {
Tobin C Harding402f9032016-05-10 11:26:57 +100020 u8 dsap;
21 u8 ssap;
22 u8 ctrl;
23 u8 pid;
24 u8 vers;
25 u8 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
28struct stp_config_pdu {
Tobin C Harding402f9032016-05-10 11:26:57 +100029 u8 flags;
30 u8 root[8];
31 u8 root_cost[4];
32 u8 sender[8];
33 u8 port[2];
34 u8 msg_age[2];
35 u8 max_age[2];
36 u8 hello_time[2];
37 u8 forward_delay[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
40#define NR16(p) (p[0] << 8 | p[1])
41#define NR32(p) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])
42
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020043static bool ebt_filter_config(const struct ebt_stp_info *info,
Ian Morris052a4bc2015-10-26 09:10:40 +000044 const struct stp_config_pdu *stpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080046 const struct ebt_stp_config_info *c;
Tobin C Harding402f9032016-05-10 11:26:57 +100047 u16 v16;
48 u32 v32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 c = &info->config;
51 if ((info->bitmask & EBT_STP_FLAGS) &&
Joe Perchesc37a2df2016-06-24 13:25:22 -070052 NF_INVF(info, EBT_STP_FLAGS, c->flags != stpc->flags))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020053 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (info->bitmask & EBT_STP_ROOTPRIO) {
55 v16 = NR16(stpc->root);
Joe Perchesc37a2df2016-06-24 13:25:22 -070056 if (NF_INVF(info, EBT_STP_ROOTPRIO,
57 v16 < c->root_priol || v16 > c->root_priou))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020058 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60 if (info->bitmask & EBT_STP_ROOTADDR) {
Joe Perchesc37a2df2016-06-24 13:25:22 -070061 if (NF_INVF(info, EBT_STP_ROOTADDR,
62 !ether_addr_equal_masked(&stpc->root[2],
63 c->root_addr,
64 c->root_addrmsk)))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020065 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67 if (info->bitmask & EBT_STP_ROOTCOST) {
68 v32 = NR32(stpc->root_cost);
Joe Perchesc37a2df2016-06-24 13:25:22 -070069 if (NF_INVF(info, EBT_STP_ROOTCOST,
70 v32 < c->root_costl || v32 > c->root_costu))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020071 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73 if (info->bitmask & EBT_STP_SENDERPRIO) {
74 v16 = NR16(stpc->sender);
Joe Perchesc37a2df2016-06-24 13:25:22 -070075 if (NF_INVF(info, EBT_STP_SENDERPRIO,
76 v16 < c->sender_priol || v16 > c->sender_priou))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020077 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79 if (info->bitmask & EBT_STP_SENDERADDR) {
Joe Perchesc37a2df2016-06-24 13:25:22 -070080 if (NF_INVF(info, EBT_STP_SENDERADDR,
81 !ether_addr_equal_masked(&stpc->sender[2],
82 c->sender_addr,
83 c->sender_addrmsk)))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020084 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 if (info->bitmask & EBT_STP_PORT) {
87 v16 = NR16(stpc->port);
Joe Perchesc37a2df2016-06-24 13:25:22 -070088 if (NF_INVF(info, EBT_STP_PORT,
89 v16 < c->portl || v16 > c->portu))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020090 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92 if (info->bitmask & EBT_STP_MSGAGE) {
93 v16 = NR16(stpc->msg_age);
Joe Perchesc37a2df2016-06-24 13:25:22 -070094 if (NF_INVF(info, EBT_STP_MSGAGE,
95 v16 < c->msg_agel || v16 > c->msg_ageu))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020096 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98 if (info->bitmask & EBT_STP_MAXAGE) {
99 v16 = NR16(stpc->max_age);
Joe Perchesc37a2df2016-06-24 13:25:22 -0700100 if (NF_INVF(info, EBT_STP_MAXAGE,
101 v16 < c->max_agel || v16 > c->max_ageu))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200102 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104 if (info->bitmask & EBT_STP_HELLOTIME) {
105 v16 = NR16(stpc->hello_time);
Joe Perchesc37a2df2016-06-24 13:25:22 -0700106 if (NF_INVF(info, EBT_STP_HELLOTIME,
107 v16 < c->hello_timel || v16 > c->hello_timeu))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200108 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110 if (info->bitmask & EBT_STP_FWDD) {
111 v16 = NR16(stpc->forward_delay);
Joe Perchesc37a2df2016-06-24 13:25:22 -0700112 if (NF_INVF(info, EBT_STP_FWDD,
113 v16 < c->forward_delayl || v16 > c->forward_delayu))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200114 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200116 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200119static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200120ebt_stp_mt(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200122 const struct ebt_stp_info *info = par->matchinfo;
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800123 const struct stp_header *sp;
124 struct stp_header _stph;
Tobin C Harding402f9032016-05-10 11:26:57 +1000125 const u8 header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 sp = skb_header_pointer(skb, 0, sizeof(_stph), &_stph);
128 if (sp == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200129 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /* The stp code only considers these */
132 if (memcmp(sp, header, sizeof(header)))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200133 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Joe Perchesc37a2df2016-06-24 13:25:22 -0700135 if ((info->bitmask & EBT_STP_TYPE) &&
136 NF_INVF(info, EBT_STP_TYPE, info->type != sp->type))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200137 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 if (sp->type == BPDU_TYPE_CONFIG &&
140 info->bitmask & EBT_STP_CONFIG_MASK) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800141 const struct stp_config_pdu *st;
142 struct stp_config_pdu _stpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 st = skb_header_pointer(skb, sizeof(_stph),
145 sizeof(_stpc), &_stpc);
146 if (st == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200147 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return ebt_filter_config(info, st);
149 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200150 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100153static int ebt_stp_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200155 const struct ebt_stp_info *info = par->matchinfo;
Tobin C Harding402f9032016-05-10 11:26:57 +1000156 const u8 bridge_ula[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00};
157 const u8 msk[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200158 const struct ebt_entry *e = par->entryinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
161 !(info->bitmask & EBT_STP_MASK))
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100162 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* Make sure the match only receives stp frames */
Pablo Neira Ayuso55917a22015-05-14 14:57:23 +0200164 if (!par->nft_compat &&
165 (!ether_addr_equal(e->destmac, bridge_ula) ||
166 !ether_addr_equal(e->destmsk, msk) ||
167 !(e->bitmask & EBT_DESTMAC)))
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100168 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Jan Engelhardt043ef462008-10-08 11:35:15 +0200173static struct xt_match ebt_stp_mt_reg __read_mostly = {
174 .name = "stp",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200175 .revision = 0,
176 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200177 .match = ebt_stp_mt,
178 .checkentry = ebt_stp_mt_check,
Florian Westphalfc0e3df2010-02-15 18:16:26 +0100179 .matchsize = sizeof(struct ebt_stp_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .me = THIS_MODULE,
181};
182
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800183static int __init ebt_stp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200185 return xt_register_match(&ebt_stp_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800188static void __exit ebt_stp_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200190 xt_unregister_match(&ebt_stp_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800193module_init(ebt_stp_init);
194module_exit(ebt_stp_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800195MODULE_DESCRIPTION("Ebtables: Spanning Tree Protocol packet match");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196MODULE_LICENSE("GPL");