1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/kernel.h>
17#include <linux/if_ether.h>
18#include <linux/if_bridge.h>
19#include <linux/netfilter_bridge.h>
20#include "br_private.h"
21#include "br_private_stp.h"
22
23#define JIFFIES_TO_TICKS(j) (((j) << 8) / HZ)
24#define TICKS_TO_JIFFIES(j) (((j) * HZ) >> 8)
25
26static void br_send_bpdu(struct net_bridge_port *p, unsigned char *data, int length)
27{
28 struct net_device *dev;
29 struct sk_buff *skb;
30 int size;
31
32 if (!p->br->stp_enabled)
33 return;
34
35 size = length + 2*ETH_ALEN + 2;
36 if (size < 60)
37 size = 60;
38
39 dev = p->dev;
40
41 if ((skb = dev_alloc_skb(size)) == NULL) {
42 printk(KERN_INFO "br: memory squeeze!\n");
43 return;
44 }
45
46 skb->dev = dev;
47 skb->protocol = htons(ETH_P_802_2);
48 skb->mac.raw = skb_put(skb, size);
49 memcpy(skb->mac.raw, bridge_ula, ETH_ALEN);
50 memcpy(skb->mac.raw+ETH_ALEN, dev->dev_addr, ETH_ALEN);
51 skb->mac.raw[2*ETH_ALEN] = 0;
52 skb->mac.raw[2*ETH_ALEN+1] = length;
53 skb->nh.raw = skb->mac.raw + 2*ETH_ALEN + 2;
54 memcpy(skb->nh.raw, data, length);
55 memset(skb->nh.raw + length, 0xa5, size - length - 2*ETH_ALEN - 2);
56
57 NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
58 dev_queue_xmit);
59}
60
61static __inline__ void br_set_ticks(unsigned char *dest, int jiff)
62{
63 __u16 ticks;
64
65 ticks = JIFFIES_TO_TICKS(jiff);
66 dest[0] = (ticks >> 8) & 0xFF;
67 dest[1] = ticks & 0xFF;
68}
69
70static __inline__ int br_get_ticks(unsigned char *dest)
71{
72 return TICKS_TO_JIFFIES((dest[0] << 8) | dest[1]);
73}
74
75
76void br_send_config_bpdu(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
77{
78 unsigned char buf[38];
79
80 buf[0] = 0x42;
81 buf[1] = 0x42;
82 buf[2] = 0x03;
83 buf[3] = 0;
84 buf[4] = 0;
85 buf[5] = 0;
86 buf[6] = BPDU_TYPE_CONFIG;
87 buf[7] = (bpdu->topology_change ? 0x01 : 0) |
88 (bpdu->topology_change_ack ? 0x80 : 0);
89 buf[8] = bpdu->root.prio[0];
90 buf[9] = bpdu->root.prio[1];
91 buf[10] = bpdu->root.addr[0];
92 buf[11] = bpdu->root.addr[1];
93 buf[12] = bpdu->root.addr[2];
94 buf[13] = bpdu->root.addr[3];
95 buf[14] = bpdu->root.addr[4];
96 buf[15] = bpdu->root.addr[5];
97 buf[16] = (bpdu->root_path_cost >> 24) & 0xFF;
98 buf[17] = (bpdu->root_path_cost >> 16) & 0xFF;
99 buf[18] = (bpdu->root_path_cost >> 8) & 0xFF;
100 buf[19] = bpdu->root_path_cost & 0xFF;
101 buf[20] = bpdu->bridge_id.prio[0];
102 buf[21] = bpdu->bridge_id.prio[1];
103 buf[22] = bpdu->bridge_id.addr[0];
104 buf[23] = bpdu->bridge_id.addr[1];
105 buf[24] = bpdu->bridge_id.addr[2];
106 buf[25] = bpdu->bridge_id.addr[3];
107 buf[26] = bpdu->bridge_id.addr[4];
108 buf[27] = bpdu->bridge_id.addr[5];
109 buf[28] = (bpdu->port_id >> 8) & 0xFF;
110 buf[29] = bpdu->port_id & 0xFF;
111
112 br_set_ticks(buf+30, bpdu->message_age);
113 br_set_ticks(buf+32, bpdu->max_age);
114 br_set_ticks(buf+34, bpdu->hello_time);
115 br_set_ticks(buf+36, bpdu->forward_delay);
116
117 br_send_bpdu(p, buf, 38);
118}
119
120
121void br_send_tcn_bpdu(struct net_bridge_port *p)
122{
123 unsigned char buf[7];
124
125 buf[0] = 0x42;
126 buf[1] = 0x42;
127 buf[2] = 0x03;
128 buf[3] = 0;
129 buf[4] = 0;
130 buf[5] = 0;
131 buf[6] = BPDU_TYPE_TCN;
132 br_send_bpdu(p, buf, 7);
133}
134
135static unsigned char header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
136
137
138int br_stp_handle_bpdu(struct sk_buff *skb)
139{
140 unsigned char *buf;
141 struct net_bridge_port *p;
142
143 p = skb->dev->br_port;
144
145 if (!p->br->stp_enabled ||
146 !pskb_may_pull(skb, sizeof(header)+1) ||
147 memcmp(skb->data, header, sizeof(header)))
148 goto err;
149
150 buf = skb_pull(skb, sizeof(header));
151 if (buf[0] == BPDU_TYPE_CONFIG) {
152 struct br_config_bpdu bpdu;
153
154 if (!pskb_may_pull(skb, 32))
155 goto err;
156
157 buf = skb->data;
158 bpdu.topology_change = (buf[1] & 0x01) ? 1 : 0;
159 bpdu.topology_change_ack = (buf[1] & 0x80) ? 1 : 0;
160
161 bpdu.root.prio[0] = buf[2];
162 bpdu.root.prio[1] = buf[3];
163 bpdu.root.addr[0] = buf[4];
164 bpdu.root.addr[1] = buf[5];
165 bpdu.root.addr[2] = buf[6];
166 bpdu.root.addr[3] = buf[7];
167 bpdu.root.addr[4] = buf[8];
168 bpdu.root.addr[5] = buf[9];
169 bpdu.root_path_cost =
170 (buf[10] << 24) |
171 (buf[11] << 16) |
172 (buf[12] << 8) |
173 buf[13];
174 bpdu.bridge_id.prio[0] = buf[14];
175 bpdu.bridge_id.prio[1] = buf[15];
176 bpdu.bridge_id.addr[0] = buf[16];
177 bpdu.bridge_id.addr[1] = buf[17];
178 bpdu.bridge_id.addr[2] = buf[18];
179 bpdu.bridge_id.addr[3] = buf[19];
180 bpdu.bridge_id.addr[4] = buf[20];
181 bpdu.bridge_id.addr[5] = buf[21];
182 bpdu.port_id = (buf[22] << 8) | buf[23];
183
184 bpdu.message_age = br_get_ticks(buf+24);
185 bpdu.max_age = br_get_ticks(buf+26);
186 bpdu.hello_time = br_get_ticks(buf+28);
187 bpdu.forward_delay = br_get_ticks(buf+30);
188
189 br_received_config_bpdu(p, &bpdu);
190 }
191
192 else if (buf[0] == BPDU_TYPE_TCN) {
193 br_received_tcn_bpdu(p);
194 }
195
196 err:
197 kfree_skb(skb);
198 return 0;
199}
200