]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/bridge/netfilter/ebt_stp.c
netfilter: add dummy members to Ebtables code to ease transition to Xtables
[net-next-2.6.git] / net / bridge / netfilter / ebt_stp.c
CommitLineData
1da177e4
LT
1/*
2 * ebt_stp
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
6 * Stephen Hemminger <shemminger@osdl.org>
7 *
8 * July, 2003
9 */
82bf7e97 10#include <linux/etherdevice.h>
1da177e4 11#include <linux/module.h>
18219d3f
JE
12#include <linux/netfilter/x_tables.h>
13#include <linux/netfilter_bridge/ebtables.h>
14#include <linux/netfilter_bridge/ebt_stp.h>
1da177e4
LT
15
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
8cc784ee 43static bool ebt_filter_config(const struct ebt_stp_info *info,
abfdf1c4 44 const struct stp_config_pdu *stpc)
1da177e4 45{
abfdf1c4 46 const struct ebt_stp_config_info *c;
1da177e4
LT
47 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))
8cc784ee 54 return false;
1da177e4
LT
55 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))
8cc784ee 59 return false;
1da177e4
LT
60 }
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]) &
9d6f229f 65 c->root_addrmsk[i];
1da177e4 66 if (FWINV(verdict != 0, EBT_STP_ROOTADDR))
8cc784ee 67 return false;
1da177e4
LT
68 }
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))
8cc784ee 73 return false;
1da177e4
LT
74 }
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))
8cc784ee 79 return false;
1da177e4
LT
80 }
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]) &
9d6f229f 85 c->sender_addrmsk[i];
1da177e4 86 if (FWINV(verdict != 0, EBT_STP_SENDERADDR))
8cc784ee 87 return false;
1da177e4
LT
88 }
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))
8cc784ee 93 return false;
1da177e4
LT
94 }
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))
8cc784ee 99 return false;
1da177e4
LT
100 }
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))
8cc784ee 105 return false;
1da177e4
LT
106 }
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))
8cc784ee 111 return false;
1da177e4
LT
112 }
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))
8cc784ee 117 return false;
1da177e4 118 }
8cc784ee 119 return true;
1da177e4
LT
120}
121
8cc784ee
JE
122static bool ebt_filter_stp(const struct sk_buff *skb,
123 const struct net_device *in,
1da177e4
LT
124 const struct net_device *out, const void *data, unsigned int datalen)
125{
abfdf1c4
JE
126 const struct ebt_stp_info *info = data;
127 const struct stp_header *sp;
128 struct stp_header _stph;
129 const uint8_t header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
1da177e4
LT
130
131 sp = skb_header_pointer(skb, 0, sizeof(_stph), &_stph);
132 if (sp == NULL)
8cc784ee 133 return false;
1da177e4
LT
134
135 /* The stp code only considers these */
136 if (memcmp(sp, header, sizeof(header)))
8cc784ee 137 return false;
1da177e4
LT
138
139 if (info->bitmask & EBT_STP_TYPE
140 && FWINV(info->type != sp->type, EBT_STP_TYPE))
8cc784ee 141 return false;
1da177e4
LT
142
143 if (sp->type == BPDU_TYPE_CONFIG &&
144 info->bitmask & EBT_STP_CONFIG_MASK) {
abfdf1c4
JE
145 const struct stp_config_pdu *st;
146 struct stp_config_pdu _stpc;
1da177e4
LT
147
148 st = skb_header_pointer(skb, sizeof(_stph),
149 sizeof(_stpc), &_stpc);
150 if (st == NULL)
8cc784ee 151 return false;
1da177e4
LT
152 return ebt_filter_config(info, st);
153 }
8cc784ee 154 return true;
1da177e4
LT
155}
156
19eda879 157static bool ebt_stp_check(const char *tablename, unsigned int hookmask,
1da177e4
LT
158 const struct ebt_entry *e, void *data, unsigned int datalen)
159{
abfdf1c4 160 const struct ebt_stp_info *info = data;
abfdf1c4
JE
161 const uint8_t bridge_ula[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00};
162 const uint8_t msk[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1da177e4
LT
163
164 if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
165 !(info->bitmask & EBT_STP_MASK))
19eda879 166 return false;
1da177e4 167 /* Make sure the match only receives stp frames */
d3f4a687
KK
168 if (compare_ether_addr(e->destmac, bridge_ula) ||
169 compare_ether_addr(e->destmsk, msk) || !(e->bitmask & EBT_DESTMAC))
19eda879 170 return false;
1da177e4 171
19eda879 172 return true;
1da177e4
LT
173}
174
30083c95 175static struct ebt_match filter_stp __read_mostly = {
1da177e4 176 .name = EBT_STP_MATCH,
001a18d3
JE
177 .revision = 0,
178 .family = NFPROTO_BRIDGE,
1da177e4
LT
179 .match = ebt_filter_stp,
180 .check = ebt_stp_check,
18219d3f 181 .matchsize = XT_ALIGN(sizeof(struct ebt_stp_info)),
1da177e4
LT
182 .me = THIS_MODULE,
183};
184
65b4b4e8 185static int __init ebt_stp_init(void)
1da177e4
LT
186{
187 return ebt_register_match(&filter_stp);
188}
189
65b4b4e8 190static void __exit ebt_stp_fini(void)
1da177e4
LT
191{
192 ebt_unregister_match(&filter_stp);
193}
194
65b4b4e8
AM
195module_init(ebt_stp_init);
196module_exit(ebt_stp_fini);
f776c4cd 197MODULE_DESCRIPTION("Ebtables: Spanning Tree Protocol packet match");
1da177e4 198MODULE_LICENSE("GPL");