blob: 7576d1d62a496bd87d319f3d8b6c499e35bfca29 [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 {
20 uint8_t dsap;
21 uint8_t ssap;
22 uint8_t ctrl;
23 uint8_t pid;
24 uint8_t vers;
25 uint8_t type;
26};
27
28struct stp_config_pdu {
29 uint8_t flags;
30 uint8_t root[8];
31 uint8_t root_cost[4];
32 uint8_t sender[8];
33 uint8_t port[2];
34 uint8_t msg_age[2];
35 uint8_t max_age[2];
36 uint8_t hello_time[2];
37 uint8_t forward_delay[2];
38};
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,
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080044 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 uint16_t v16;
48 uint32_t v32;
49 int verdict, i;
50
51 c = &info->config;
52 if ((info->bitmask & EBT_STP_FLAGS) &&
53 FWINV(c->flags != stpc->flags, EBT_STP_FLAGS))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020054 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (info->bitmask & EBT_STP_ROOTPRIO) {
56 v16 = NR16(stpc->root);
57 if (FWINV(v16 < c->root_priol ||
58 v16 > c->root_priou, EBT_STP_ROOTPRIO))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020059 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61 if (info->bitmask & EBT_STP_ROOTADDR) {
62 verdict = 0;
63 for (i = 0; i < 6; i++)
64 verdict |= (stpc->root[2+i] ^ c->root_addr[i]) &
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090065 c->root_addrmsk[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (FWINV(verdict != 0, EBT_STP_ROOTADDR))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020067 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
69 if (info->bitmask & EBT_STP_ROOTCOST) {
70 v32 = NR32(stpc->root_cost);
71 if (FWINV(v32 < c->root_costl ||
72 v32 > c->root_costu, EBT_STP_ROOTCOST))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020073 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 if (info->bitmask & EBT_STP_SENDERPRIO) {
76 v16 = NR16(stpc->sender);
77 if (FWINV(v16 < c->sender_priol ||
78 v16 > c->sender_priou, EBT_STP_SENDERPRIO))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020079 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81 if (info->bitmask & EBT_STP_SENDERADDR) {
82 verdict = 0;
83 for (i = 0; i < 6; i++)
84 verdict |= (stpc->sender[2+i] ^ c->sender_addr[i]) &
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090085 c->sender_addrmsk[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (FWINV(verdict != 0, EBT_STP_SENDERADDR))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020087 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89 if (info->bitmask & EBT_STP_PORT) {
90 v16 = NR16(stpc->port);
91 if (FWINV(v16 < c->portl ||
92 v16 > c->portu, EBT_STP_PORT))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020093 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95 if (info->bitmask & EBT_STP_MSGAGE) {
96 v16 = NR16(stpc->msg_age);
97 if (FWINV(v16 < c->msg_agel ||
98 v16 > c->msg_ageu, EBT_STP_MSGAGE))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020099 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 if (info->bitmask & EBT_STP_MAXAGE) {
102 v16 = NR16(stpc->max_age);
103 if (FWINV(v16 < c->max_agel ||
104 v16 > c->max_ageu, EBT_STP_MAXAGE))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200105 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107 if (info->bitmask & EBT_STP_HELLOTIME) {
108 v16 = NR16(stpc->hello_time);
109 if (FWINV(v16 < c->hello_timel ||
110 v16 > c->hello_timeu, EBT_STP_HELLOTIME))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200111 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113 if (info->bitmask & EBT_STP_FWDD) {
114 v16 = NR16(stpc->forward_delay);
115 if (FWINV(v16 < c->forward_delayl ||
116 v16 > c->forward_delayu, EBT_STP_FWDD))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200117 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200119 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200122static bool
123ebt_stp_mt(const struct sk_buff *skb, const struct net_device *in,
124 const struct net_device *out, const struct xt_match *match,
125 const void *data, int offset, unsigned int protoff, bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800127 const struct ebt_stp_info *info = data;
128 const struct stp_header *sp;
129 struct stp_header _stph;
130 const uint8_t header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 sp = skb_header_pointer(skb, 0, sizeof(_stph), &_stph);
133 if (sp == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200134 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 /* The stp code only considers these */
137 if (memcmp(sp, header, sizeof(header)))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200138 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 if (info->bitmask & EBT_STP_TYPE
141 && FWINV(info->type != sp->type, EBT_STP_TYPE))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200142 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (sp->type == BPDU_TYPE_CONFIG &&
145 info->bitmask & EBT_STP_CONFIG_MASK) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800146 const struct stp_config_pdu *st;
147 struct stp_config_pdu _stpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 st = skb_header_pointer(skb, sizeof(_stph),
150 sizeof(_stpc), &_stpc);
151 if (st == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200152 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return ebt_filter_config(info, st);
154 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200155 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200158static bool
159ebt_stp_mt_check(const char *table, const void *entry,
160 const struct xt_match *match, void *data,
161 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800163 const struct ebt_stp_info *info = data;
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800164 const uint8_t bridge_ula[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00};
165 const uint8_t msk[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200166 const struct ebt_entry *e = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
169 !(info->bitmask & EBT_STP_MASK))
Jan Engelhardt19eda872008-10-08 11:35:13 +0200170 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* Make sure the match only receives stp frames */
Kris Katterjohnd3f4a682006-01-09 16:01:43 -0800172 if (compare_ether_addr(e->destmac, bridge_ula) ||
173 compare_ether_addr(e->destmsk, msk) || !(e->bitmask & EBT_DESTMAC))
Jan Engelhardt19eda872008-10-08 11:35:13 +0200174 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jan Engelhardt19eda872008-10-08 11:35:13 +0200176 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Jan Engelhardt30083c92008-01-31 04:00:59 -0800179static struct ebt_match filter_stp __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .name = EBT_STP_MATCH,
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200181 .revision = 0,
182 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200183 .match = ebt_stp_mt,
184 .checkentry = ebt_stp_mt_check,
Jan Engelhardt18219d32008-10-08 11:35:13 +0200185 .matchsize = XT_ALIGN(sizeof(struct ebt_stp_info)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .me = THIS_MODULE,
187};
188
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800189static int __init ebt_stp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 return ebt_register_match(&filter_stp);
192}
193
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800194static void __exit ebt_stp_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 ebt_unregister_match(&filter_stp);
197}
198
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800199module_init(ebt_stp_init);
200module_exit(ebt_stp_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800201MODULE_DESCRIPTION("Ebtables: Spanning Tree Protocol packet match");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202MODULE_LICENSE("GPL");