ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / bridge / netfilter / ebt_stp.c
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  */
10
11 #include <linux/netfilter_bridge/ebtables.h>
12 #include <linux/netfilter_bridge/ebt_stp.h>
13 #include <linux/module.h>
14
15 #define BPDU_TYPE_CONFIG 0
16 #define BPDU_TYPE_TCN 0x80
17
18 struct stp_header {
19         uint8_t dsap;
20         uint8_t ssap;
21         uint8_t ctrl;
22         uint8_t pid;
23         uint8_t vers;
24         uint8_t type;
25 };
26
27 struct stp_config_pdu {
28         uint8_t flags;
29         uint8_t root[8];
30         uint8_t root_cost[4];
31         uint8_t sender[8];
32         uint8_t port[2];
33         uint8_t msg_age[2];
34         uint8_t max_age[2];
35         uint8_t hello_time[2];
36         uint8_t forward_delay[2];
37 };
38
39 #define NR16(p) (p[0] << 8 | p[1])
40 #define NR32(p) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])
41
42 static int ebt_filter_config(struct ebt_stp_info *info,
43    struct stp_config_pdu *stpc)
44 {
45         struct ebt_stp_config_info *c;
46         uint16_t v16;
47         uint32_t v32;
48         int verdict, i;
49
50         c = &info->config;
51         if ((info->bitmask & EBT_STP_FLAGS) &&
52             FWINV(c->flags != stpc->flags, EBT_STP_FLAGS))
53                 return EBT_NOMATCH;
54         if (info->bitmask & EBT_STP_ROOTPRIO) {
55                 v16 = NR16(stpc->root);
56                 if (FWINV(v16 < c->root_priol ||
57                     v16 > c->root_priou, EBT_STP_ROOTPRIO))
58                         return EBT_NOMATCH;
59         }
60         if (info->bitmask & EBT_STP_ROOTADDR) {
61                 verdict = 0;
62                 for (i = 0; i < 6; i++)
63                         verdict |= (stpc->root[2+i] ^ c->root_addr[i]) &
64                                    c->root_addrmsk[i];
65                 if (FWINV(verdict != 0, EBT_STP_ROOTADDR))
66                         return EBT_NOMATCH;
67         }
68         if (info->bitmask & EBT_STP_ROOTCOST) {
69                 v32 = NR32(stpc->root_cost);
70                 if (FWINV(v32 < c->root_costl ||
71                     v32 > c->root_costu, EBT_STP_ROOTCOST))
72                         return EBT_NOMATCH;
73         }
74         if (info->bitmask & EBT_STP_SENDERPRIO) {
75                 v16 = NR16(stpc->sender);
76                 if (FWINV(v16 < c->sender_priol ||
77                     v16 > c->sender_priou, EBT_STP_SENDERPRIO))
78                         return EBT_NOMATCH;
79         }
80         if (info->bitmask & EBT_STP_SENDERADDR) {
81                 verdict = 0;
82                 for (i = 0; i < 6; i++)
83                         verdict |= (stpc->sender[2+i] ^ c->sender_addr[i]) &
84                                    c->sender_addrmsk[i];
85                 if (FWINV(verdict != 0, EBT_STP_SENDERADDR))
86                         return EBT_NOMATCH;
87         }
88         if (info->bitmask & EBT_STP_PORT) {
89                 v16 = NR16(stpc->port);
90                 if (FWINV(v16 < c->portl ||
91                     v16 > c->portu, EBT_STP_PORT))
92                         return EBT_NOMATCH;
93         }
94         if (info->bitmask & EBT_STP_MSGAGE) {
95                 v16 = NR16(stpc->msg_age);
96                 if (FWINV(v16 < c->msg_agel ||
97                     v16 > c->msg_ageu, EBT_STP_MSGAGE))
98                         return EBT_NOMATCH;
99         }
100         if (info->bitmask & EBT_STP_MAXAGE) {
101                 v16 = NR16(stpc->max_age);
102                 if (FWINV(v16 < c->max_agel ||
103                     v16 > c->max_ageu, EBT_STP_MAXAGE))
104                         return EBT_NOMATCH;
105         }
106         if (info->bitmask & EBT_STP_HELLOTIME) {
107                 v16 = NR16(stpc->hello_time);
108                 if (FWINV(v16 < c->hello_timel ||
109                     v16 > c->hello_timeu, EBT_STP_HELLOTIME))
110                         return EBT_NOMATCH;
111         }
112         if (info->bitmask & EBT_STP_FWDD) {
113                 v16 = NR16(stpc->forward_delay);
114                 if (FWINV(v16 < c->forward_delayl ||
115                     v16 > c->forward_delayu, EBT_STP_FWDD))
116                         return EBT_NOMATCH;
117         }
118         return EBT_MATCH;
119 }
120
121 static int ebt_filter_stp(const struct sk_buff *skb, const struct net_device *in,
122    const struct net_device *out, const void *data, unsigned int datalen)
123 {
124         struct ebt_stp_info *info = (struct ebt_stp_info *)data;
125         struct stp_header stph;
126         uint8_t header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
127         if (skb_copy_bits(skb, 0, &stph, sizeof(stph)))
128                 return EBT_NOMATCH;
129
130         /* The stp code only considers these */
131         if (memcmp(&stph, header, sizeof(header)))
132                 return EBT_NOMATCH;
133
134         if (info->bitmask & EBT_STP_TYPE
135             && FWINV(info->type != stph.type, EBT_STP_TYPE))
136                 return EBT_NOMATCH;
137
138         if (stph.type == BPDU_TYPE_CONFIG &&
139             info->bitmask & EBT_STP_CONFIG_MASK) {
140                 struct stp_config_pdu stpc;
141
142                 if (skb_copy_bits(skb, sizeof(stph), &stpc, sizeof(stpc)))
143                     return EBT_NOMATCH;
144                 return ebt_filter_config(info, &stpc);
145         }
146         return EBT_MATCH;
147 }
148
149 static int ebt_stp_check(const char *tablename, unsigned int hookmask,
150    const struct ebt_entry *e, void *data, unsigned int datalen)
151 {
152         struct ebt_stp_info *info = (struct ebt_stp_info *)data;
153         int len = EBT_ALIGN(sizeof(struct ebt_stp_info));
154         uint8_t bridge_ula[6] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
155         uint8_t msk[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
156
157         if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
158             !(info->bitmask & EBT_STP_MASK))
159                 return -EINVAL;
160         if (datalen != len)
161                 return -EINVAL;
162         /* Make sure the match only receives stp frames */
163         if (memcmp(e->destmac, bridge_ula, ETH_ALEN) ||
164             memcmp(e->destmsk, msk, ETH_ALEN) || !(e->bitmask & EBT_DESTMAC))
165                 return -EINVAL;
166
167         return 0;
168 }
169
170 static struct ebt_match filter_stp =
171 {
172         .name           = EBT_STP_MATCH,
173         .match          = ebt_filter_stp,
174         .check          = ebt_stp_check,
175         .me             = THIS_MODULE,
176 };
177
178 static int __init init(void)
179 {
180         return ebt_register_match(&filter_stp);
181 }
182
183 static void __exit fini(void)
184 {
185         ebt_unregister_match(&filter_stp);
186 }
187
188 module_init(init);
189 module_exit(fini);
190 MODULE_LICENSE("GPL");