Commit | Line | Data |
---|---|---|
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 | ||
19 | struct stp_header { | |
402f9030 TH |
20 | u8 dsap; |
21 | u8 ssap; | |
22 | u8 ctrl; | |
23 | u8 pid; | |
24 | u8 vers; | |
25 | u8 type; | |
1da177e4 LT |
26 | }; |
27 | ||
28 | struct stp_config_pdu { | |
402f9030 TH |
29 | 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]; | |
1da177e4 LT |
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 | 43 | static bool ebt_filter_config(const struct ebt_stp_info *info, |
052a4bc4 | 44 | const struct stp_config_pdu *stpc) |
1da177e4 | 45 | { |
abfdf1c4 | 46 | const struct ebt_stp_config_info *c; |
402f9030 TH |
47 | u16 v16; |
48 | u32 v32; | |
1da177e4 LT |
49 | |
50 | c = &info->config; | |
51 | if ((info->bitmask & EBT_STP_FLAGS) && | |
c37a2dfa | 52 | NF_INVF(info, EBT_STP_FLAGS, c->flags != stpc->flags)) |
8cc784ee | 53 | return false; |
1da177e4 LT |
54 | if (info->bitmask & EBT_STP_ROOTPRIO) { |
55 | v16 = NR16(stpc->root); | |
c37a2dfa JP |
56 | if (NF_INVF(info, EBT_STP_ROOTPRIO, |
57 | v16 < c->root_priol || v16 > c->root_priou)) | |
8cc784ee | 58 | return false; |
1da177e4 LT |
59 | } |
60 | if (info->bitmask & EBT_STP_ROOTADDR) { | |
c37a2dfa JP |
61 | if (NF_INVF(info, EBT_STP_ROOTADDR, |
62 | !ether_addr_equal_masked(&stpc->root[2], | |
63 | c->root_addr, | |
64 | c->root_addrmsk))) | |
8cc784ee | 65 | return false; |
1da177e4 LT |
66 | } |
67 | if (info->bitmask & EBT_STP_ROOTCOST) { | |
68 | v32 = NR32(stpc->root_cost); | |
c37a2dfa JP |
69 | if (NF_INVF(info, EBT_STP_ROOTCOST, |
70 | v32 < c->root_costl || v32 > c->root_costu)) | |
8cc784ee | 71 | return false; |
1da177e4 LT |
72 | } |
73 | if (info->bitmask & EBT_STP_SENDERPRIO) { | |
74 | v16 = NR16(stpc->sender); | |
c37a2dfa JP |
75 | if (NF_INVF(info, EBT_STP_SENDERPRIO, |
76 | v16 < c->sender_priol || v16 > c->sender_priou)) | |
8cc784ee | 77 | return false; |
1da177e4 LT |
78 | } |
79 | if (info->bitmask & EBT_STP_SENDERADDR) { | |
c37a2dfa JP |
80 | if (NF_INVF(info, EBT_STP_SENDERADDR, |
81 | !ether_addr_equal_masked(&stpc->sender[2], | |
82 | c->sender_addr, | |
83 | c->sender_addrmsk))) | |
8cc784ee | 84 | return false; |
1da177e4 LT |
85 | } |
86 | if (info->bitmask & EBT_STP_PORT) { | |
87 | v16 = NR16(stpc->port); | |
c37a2dfa JP |
88 | if (NF_INVF(info, EBT_STP_PORT, |
89 | v16 < c->portl || v16 > c->portu)) | |
8cc784ee | 90 | return false; |
1da177e4 LT |
91 | } |
92 | if (info->bitmask & EBT_STP_MSGAGE) { | |
93 | v16 = NR16(stpc->msg_age); | |
c37a2dfa JP |
94 | if (NF_INVF(info, EBT_STP_MSGAGE, |
95 | v16 < c->msg_agel || v16 > c->msg_ageu)) | |
8cc784ee | 96 | return false; |
1da177e4 LT |
97 | } |
98 | if (info->bitmask & EBT_STP_MAXAGE) { | |
99 | v16 = NR16(stpc->max_age); | |
c37a2dfa JP |
100 | if (NF_INVF(info, EBT_STP_MAXAGE, |
101 | v16 < c->max_agel || v16 > c->max_ageu)) | |
8cc784ee | 102 | return false; |
1da177e4 LT |
103 | } |
104 | if (info->bitmask & EBT_STP_HELLOTIME) { | |
105 | v16 = NR16(stpc->hello_time); | |
c37a2dfa JP |
106 | if (NF_INVF(info, EBT_STP_HELLOTIME, |
107 | v16 < c->hello_timel || v16 > c->hello_timeu)) | |
8cc784ee | 108 | return false; |
1da177e4 LT |
109 | } |
110 | if (info->bitmask & EBT_STP_FWDD) { | |
111 | v16 = NR16(stpc->forward_delay); | |
c37a2dfa JP |
112 | if (NF_INVF(info, EBT_STP_FWDD, |
113 | v16 < c->forward_delayl || v16 > c->forward_delayu)) | |
8cc784ee | 114 | return false; |
1da177e4 | 115 | } |
8cc784ee | 116 | return true; |
1da177e4 LT |
117 | } |
118 | ||
2d06d4a5 | 119 | static bool |
62fc8051 | 120 | ebt_stp_mt(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4 | 121 | { |
f7108a20 | 122 | const struct ebt_stp_info *info = par->matchinfo; |
abfdf1c4 JE |
123 | const struct stp_header *sp; |
124 | struct stp_header _stph; | |
402f9030 | 125 | const u8 header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00}; |
1da177e4 LT |
126 | |
127 | sp = skb_header_pointer(skb, 0, sizeof(_stph), &_stph); | |
128 | if (sp == NULL) | |
8cc784ee | 129 | return false; |
1da177e4 LT |
130 | |
131 | /* The stp code only considers these */ | |
132 | if (memcmp(sp, header, sizeof(header))) | |
8cc784ee | 133 | return false; |
1da177e4 | 134 | |
c37a2dfa JP |
135 | if ((info->bitmask & EBT_STP_TYPE) && |
136 | NF_INVF(info, EBT_STP_TYPE, info->type != sp->type)) | |
8cc784ee | 137 | return false; |
1da177e4 LT |
138 | |
139 | if (sp->type == BPDU_TYPE_CONFIG && | |
140 | info->bitmask & EBT_STP_CONFIG_MASK) { | |
abfdf1c4 JE |
141 | const struct stp_config_pdu *st; |
142 | struct stp_config_pdu _stpc; | |
1da177e4 LT |
143 | |
144 | st = skb_header_pointer(skb, sizeof(_stph), | |
145 | sizeof(_stpc), &_stpc); | |
146 | if (st == NULL) | |
8cc784ee | 147 | return false; |
1da177e4 LT |
148 | return ebt_filter_config(info, st); |
149 | } | |
8cc784ee | 150 | return true; |
1da177e4 LT |
151 | } |
152 | ||
b0f38452 | 153 | static int ebt_stp_mt_check(const struct xt_mtchk_param *par) |
1da177e4 | 154 | { |
9b4fce7a | 155 | const struct ebt_stp_info *info = par->matchinfo; |
402f9030 TH |
156 | const u8 bridge_ula[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00}; |
157 | const u8 msk[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | |
9b4fce7a | 158 | const struct ebt_entry *e = par->entryinfo; |
1da177e4 LT |
159 | |
160 | if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK || | |
161 | !(info->bitmask & EBT_STP_MASK)) | |
bd414ee6 | 162 | return -EINVAL; |
1da177e4 | 163 | /* Make sure the match only receives stp frames */ |
55917a21 PNA |
164 | if (!par->nft_compat && |
165 | (!ether_addr_equal(e->destmac, bridge_ula) || | |
166 | !ether_addr_equal(e->destmsk, msk) || | |
167 | !(e->bitmask & EBT_DESTMAC))) | |
bd414ee6 | 168 | return -EINVAL; |
1da177e4 | 169 | |
bd414ee6 | 170 | return 0; |
1da177e4 LT |
171 | } |
172 | ||
043ef46c JE |
173 | static struct xt_match ebt_stp_mt_reg __read_mostly = { |
174 | .name = "stp", | |
001a18d3 JE |
175 | .revision = 0, |
176 | .family = NFPROTO_BRIDGE, | |
2d06d4a5 JE |
177 | .match = ebt_stp_mt, |
178 | .checkentry = ebt_stp_mt_check, | |
fc0e3df4 | 179 | .matchsize = sizeof(struct ebt_stp_info), |
1da177e4 LT |
180 | .me = THIS_MODULE, |
181 | }; | |
182 | ||
65b4b4e8 | 183 | static int __init ebt_stp_init(void) |
1da177e4 | 184 | { |
043ef46c | 185 | return xt_register_match(&ebt_stp_mt_reg); |
1da177e4 LT |
186 | } |
187 | ||
65b4b4e8 | 188 | static void __exit ebt_stp_fini(void) |
1da177e4 | 189 | { |
043ef46c | 190 | xt_unregister_match(&ebt_stp_mt_reg); |
1da177e4 LT |
191 | } |
192 | ||
65b4b4e8 AM |
193 | module_init(ebt_stp_init); |
194 | module_exit(ebt_stp_fini); | |
f776c4cd | 195 | MODULE_DESCRIPTION("Ebtables: Spanning Tree Protocol packet match"); |
1da177e4 | 196 | MODULE_LICENSE("GPL"); |