1 /*
2 * Spanning tree protocol; BPDU handling
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
8 * $Id: br_stp_bpdu.c,v 1.2 2000/02/21 15:51:34 davem Exp $
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16 #include <linux/kernel.h>
17 #include <linux/if_bridge.h>
18 #include "br_private.h"
19 #include "br_private_stp.h"
20
21 #define JIFFIES_TO_TICKS(j) (((j) << 8) / HZ)
22 #define TICKS_TO_JIFFIES(j) (((j) * HZ) >> 8)
23
24 static void br_send_bpdu(struct net_bridge_port *p, unsigned char *data, int length)
25 {
26 struct net_device *dev;
27 struct sk_buff *skb;
28 int size;
29
30 if (!p->br->stp_enabled)
31 return;
32
33 size = length + 2*ETH_ALEN + 2;
34 if (size < 60)
35 size = 60;
36
37 dev = p->dev;
38
39 if ((skb = dev_alloc_skb(size)) == NULL) {
40 printk(KERN_INFO "br: memory squeeze!\n");
41 return;
42 }
43
44 skb->dev = dev;
45 skb->mac.raw = skb_put(skb, size);
46 memcpy(skb->mac.raw, bridge_ula, ETH_ALEN);
47 memcpy(skb->mac.raw+ETH_ALEN, dev->dev_addr, ETH_ALEN);
48 skb->mac.raw[2*ETH_ALEN] = 0;
49 skb->mac.raw[2*ETH_ALEN+1] = length;
50 skb->nh.raw = skb->mac.raw + 2*ETH_ALEN + 2;
51 memcpy(skb->nh.raw, data, length);
52 memset(skb->nh.raw + length, 0xa5, size - length - 2*ETH_ALEN - 2);
53
54 dev_queue_xmit(skb);
55 }
56
57 static __inline__ void br_set_ticks(unsigned char *dest, int jiff)
58 {
59 __u16 ticks;
60
61 ticks = JIFFIES_TO_TICKS(jiff);
62 dest[0] = (ticks >> 8) & 0xFF;
63 dest[1] = ticks & 0xFF;
64 }
65
66 static __inline__ int br_get_ticks(unsigned char *dest)
67 {
68 return TICKS_TO_JIFFIES((dest[0] << 8) | dest[1]);
69 }
70
71 /* called under bridge lock */
72 void br_send_config_bpdu(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
73 {
74 unsigned char buf[38];
75
76 buf[0] = 0x42;
77 buf[1] = 0x42;
78 buf[2] = 0x03;
79 buf[3] = 0;
80 buf[4] = 0;
81 buf[5] = 0;
82 buf[6] = BPDU_TYPE_CONFIG;
83 buf[7] = (bpdu->topology_change ? 0x01 : 0) |
84 (bpdu->topology_change_ack ? 0x80 : 0);
85 buf[8] = bpdu->root.prio[0];
86 buf[9] = bpdu->root.prio[1];
87 buf[10] = bpdu->root.addr[0];
88 buf[11] = bpdu->root.addr[1];
89 buf[12] = bpdu->root.addr[2];
90 buf[13] = bpdu->root.addr[3];
91 buf[14] = bpdu->root.addr[4];
92 buf[15] = bpdu->root.addr[5];
93 buf[16] = (bpdu->root_path_cost >> 24) & 0xFF;
94 buf[17] = (bpdu->root_path_cost >> 16) & 0xFF;
95 buf[18] = (bpdu->root_path_cost >> 8) & 0xFF;
96 buf[19] = bpdu->root_path_cost & 0xFF;
97 buf[20] = bpdu->bridge_id.prio[0];
98 buf[21] = bpdu->bridge_id.prio[1];
99 buf[22] = bpdu->bridge_id.addr[0];
100 buf[23] = bpdu->bridge_id.addr[1];
101 buf[24] = bpdu->bridge_id.addr[2];
102 buf[25] = bpdu->bridge_id.addr[3];
103 buf[26] = bpdu->bridge_id.addr[4];
104 buf[27] = bpdu->bridge_id.addr[5];
105 buf[28] = (bpdu->port_id >> 8) & 0xFF;
106 buf[29] = bpdu->port_id & 0xFF;
107
108 br_set_ticks(buf+30, bpdu->message_age);
109 br_set_ticks(buf+32, bpdu->max_age);
110 br_set_ticks(buf+34, bpdu->hello_time);
111 br_set_ticks(buf+36, bpdu->forward_delay);
112
113 br_send_bpdu(p, buf, 38);
114 }
115
116 /* called under bridge lock */
117 void br_send_tcn_bpdu(struct net_bridge_port *p)
118 {
119 unsigned char buf[7];
120
121 buf[0] = 0x42;
122 buf[1] = 0x42;
123 buf[2] = 0x03;
124 buf[3] = 0;
125 buf[4] = 0;
126 buf[5] = 0;
127 buf[6] = BPDU_TYPE_TCN;
128 br_send_bpdu(p, buf, 7);
129 }
130
131 static unsigned char header[6] = {0x42, 0x42, 0x03, 0x00, 0x00, 0x00};
132
133 /* called under bridge lock */
134 void br_stp_handle_bpdu(struct sk_buff *skb)
135 {
136 unsigned char *buf;
137 struct net_bridge_port *p;
138
139 buf = skb->mac.raw + 14;
140 p = skb->dev->br_port;
141 if (!p->br->stp_enabled || memcmp(buf, header, 6)) {
142 kfree_skb(skb);
143 return;
144 }
145
146 if (buf[6] == BPDU_TYPE_CONFIG) {
147 struct br_config_bpdu bpdu;
148
149 bpdu.topology_change = (buf[7] & 0x01) ? 1 : 0;
150 bpdu.topology_change_ack = (buf[7] & 0x80) ? 1 : 0;
151 bpdu.root.prio[0] = buf[8];
152 bpdu.root.prio[1] = buf[9];
153 bpdu.root.addr[0] = buf[10];
154 bpdu.root.addr[1] = buf[11];
155 bpdu.root.addr[2] = buf[12];
156 bpdu.root.addr[3] = buf[13];
157 bpdu.root.addr[4] = buf[14];
158 bpdu.root.addr[5] = buf[15];
159 bpdu.root_path_cost =
160 (buf[16] << 24) |
161 (buf[17] << 16) |
162 (buf[18] << 8) |
163 buf[19];
164 bpdu.bridge_id.prio[0] = buf[20];
165 bpdu.bridge_id.prio[1] = buf[21];
166 bpdu.bridge_id.addr[0] = buf[22];
167 bpdu.bridge_id.addr[1] = buf[23];
168 bpdu.bridge_id.addr[2] = buf[24];
169 bpdu.bridge_id.addr[3] = buf[25];
170 bpdu.bridge_id.addr[4] = buf[26];
171 bpdu.bridge_id.addr[5] = buf[27];
172 bpdu.port_id = (buf[28] << 8) | buf[29];
173
174 bpdu.message_age = br_get_ticks(buf+30);
175 bpdu.max_age = br_get_ticks(buf+32);
176 bpdu.hello_time = br_get_ticks(buf+34);
177 bpdu.forward_delay = br_get_ticks(buf+36);
178
179 kfree_skb(skb);
180 br_received_config_bpdu(p, &bpdu);
181 return;
182 }
183
184 if (buf[6] == BPDU_TYPE_TCN) {
185 br_received_tcn_bpdu(p);
186 kfree_skb(skb);
187 return;
188 }
189
190 kfree_skb(skb);
191 }
192
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.