0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/netfilter_bridge.h>
0012 #include <linux/etherdevice.h>
0013 #include <linux/llc.h>
0014 #include <linux/slab.h>
0015 #include <linux/pkt_sched.h>
0016 #include <net/net_namespace.h>
0017 #include <net/llc.h>
0018 #include <net/llc_pdu.h>
0019 #include <net/stp.h>
0020 #include <asm/unaligned.h>
0021
0022 #include "br_private.h"
0023 #include "br_private_stp.h"
0024
0025 #define STP_HZ 256
0026
0027 #define LLC_RESERVE sizeof(struct llc_pdu_un)
0028
0029 static int br_send_bpdu_finish(struct net *net, struct sock *sk,
0030 struct sk_buff *skb)
0031 {
0032 return dev_queue_xmit(skb);
0033 }
0034
0035 static void br_send_bpdu(struct net_bridge_port *p,
0036 const unsigned char *data, int length)
0037 {
0038 struct sk_buff *skb;
0039
0040 skb = dev_alloc_skb(length+LLC_RESERVE);
0041 if (!skb)
0042 return;
0043
0044 skb->dev = p->dev;
0045 skb->protocol = htons(ETH_P_802_2);
0046 skb->priority = TC_PRIO_CONTROL;
0047
0048 skb_reserve(skb, LLC_RESERVE);
0049 __skb_put_data(skb, data, length);
0050
0051 llc_pdu_header_init(skb, LLC_PDU_TYPE_U, LLC_SAP_BSPAN,
0052 LLC_SAP_BSPAN, LLC_PDU_CMD);
0053 llc_pdu_init_as_ui_cmd(skb);
0054
0055 llc_mac_hdr_init(skb, p->dev->dev_addr, p->br->group_addr);
0056
0057 skb_reset_mac_header(skb);
0058
0059 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
0060 dev_net(p->dev), NULL, skb, NULL, skb->dev,
0061 br_send_bpdu_finish);
0062 }
0063
0064 static inline void br_set_ticks(unsigned char *dest, int j)
0065 {
0066 unsigned long ticks = (STP_HZ * j)/ HZ;
0067
0068 put_unaligned_be16(ticks, dest);
0069 }
0070
0071 static inline int br_get_ticks(const unsigned char *src)
0072 {
0073 unsigned long ticks = get_unaligned_be16(src);
0074
0075 return DIV_ROUND_UP(ticks * HZ, STP_HZ);
0076 }
0077
0078
0079 void br_send_config_bpdu(struct net_bridge_port *p, struct br_config_bpdu *bpdu)
0080 {
0081 unsigned char buf[35];
0082
0083 if (p->br->stp_enabled != BR_KERNEL_STP)
0084 return;
0085
0086 buf[0] = 0;
0087 buf[1] = 0;
0088 buf[2] = 0;
0089 buf[3] = BPDU_TYPE_CONFIG;
0090 buf[4] = (bpdu->topology_change ? 0x01 : 0) |
0091 (bpdu->topology_change_ack ? 0x80 : 0);
0092 buf[5] = bpdu->root.prio[0];
0093 buf[6] = bpdu->root.prio[1];
0094 buf[7] = bpdu->root.addr[0];
0095 buf[8] = bpdu->root.addr[1];
0096 buf[9] = bpdu->root.addr[2];
0097 buf[10] = bpdu->root.addr[3];
0098 buf[11] = bpdu->root.addr[4];
0099 buf[12] = bpdu->root.addr[5];
0100 buf[13] = (bpdu->root_path_cost >> 24) & 0xFF;
0101 buf[14] = (bpdu->root_path_cost >> 16) & 0xFF;
0102 buf[15] = (bpdu->root_path_cost >> 8) & 0xFF;
0103 buf[16] = bpdu->root_path_cost & 0xFF;
0104 buf[17] = bpdu->bridge_id.prio[0];
0105 buf[18] = bpdu->bridge_id.prio[1];
0106 buf[19] = bpdu->bridge_id.addr[0];
0107 buf[20] = bpdu->bridge_id.addr[1];
0108 buf[21] = bpdu->bridge_id.addr[2];
0109 buf[22] = bpdu->bridge_id.addr[3];
0110 buf[23] = bpdu->bridge_id.addr[4];
0111 buf[24] = bpdu->bridge_id.addr[5];
0112 buf[25] = (bpdu->port_id >> 8) & 0xFF;
0113 buf[26] = bpdu->port_id & 0xFF;
0114
0115 br_set_ticks(buf+27, bpdu->message_age);
0116 br_set_ticks(buf+29, bpdu->max_age);
0117 br_set_ticks(buf+31, bpdu->hello_time);
0118 br_set_ticks(buf+33, bpdu->forward_delay);
0119
0120 br_send_bpdu(p, buf, 35);
0121
0122 p->stp_xstats.tx_bpdu++;
0123 }
0124
0125
0126 void br_send_tcn_bpdu(struct net_bridge_port *p)
0127 {
0128 unsigned char buf[4];
0129
0130 if (p->br->stp_enabled != BR_KERNEL_STP)
0131 return;
0132
0133 buf[0] = 0;
0134 buf[1] = 0;
0135 buf[2] = 0;
0136 buf[3] = BPDU_TYPE_TCN;
0137 br_send_bpdu(p, buf, 4);
0138
0139 p->stp_xstats.tx_tcn++;
0140 }
0141
0142
0143
0144
0145
0146
0147 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
0148 struct net_device *dev)
0149 {
0150 struct net_bridge_port *p;
0151 struct net_bridge *br;
0152 const unsigned char *buf;
0153
0154 if (!pskb_may_pull(skb, 4))
0155 goto err;
0156
0157
0158 buf = skb->data;
0159 if (buf[0] != 0 || buf[1] != 0 || buf[2] != 0)
0160 goto err;
0161
0162 p = br_port_get_check_rcu(dev);
0163 if (!p)
0164 goto err;
0165
0166 br = p->br;
0167 spin_lock(&br->lock);
0168
0169 if (br->stp_enabled != BR_KERNEL_STP)
0170 goto out;
0171
0172 if (!(br->dev->flags & IFF_UP))
0173 goto out;
0174
0175 if (p->state == BR_STATE_DISABLED)
0176 goto out;
0177
0178 if (!ether_addr_equal(eth_hdr(skb)->h_dest, br->group_addr))
0179 goto out;
0180
0181 if (p->flags & BR_BPDU_GUARD) {
0182 br_notice(br, "BPDU received on blocked port %u(%s)\n",
0183 (unsigned int) p->port_no, p->dev->name);
0184 br_stp_disable_port(p);
0185 goto out;
0186 }
0187
0188 buf = skb_pull(skb, 3);
0189
0190 if (buf[0] == BPDU_TYPE_CONFIG) {
0191 struct br_config_bpdu bpdu;
0192
0193 if (!pskb_may_pull(skb, 32))
0194 goto out;
0195
0196 buf = skb->data;
0197 bpdu.topology_change = (buf[1] & 0x01) ? 1 : 0;
0198 bpdu.topology_change_ack = (buf[1] & 0x80) ? 1 : 0;
0199
0200 bpdu.root.prio[0] = buf[2];
0201 bpdu.root.prio[1] = buf[3];
0202 bpdu.root.addr[0] = buf[4];
0203 bpdu.root.addr[1] = buf[5];
0204 bpdu.root.addr[2] = buf[6];
0205 bpdu.root.addr[3] = buf[7];
0206 bpdu.root.addr[4] = buf[8];
0207 bpdu.root.addr[5] = buf[9];
0208 bpdu.root_path_cost =
0209 (buf[10] << 24) |
0210 (buf[11] << 16) |
0211 (buf[12] << 8) |
0212 buf[13];
0213 bpdu.bridge_id.prio[0] = buf[14];
0214 bpdu.bridge_id.prio[1] = buf[15];
0215 bpdu.bridge_id.addr[0] = buf[16];
0216 bpdu.bridge_id.addr[1] = buf[17];
0217 bpdu.bridge_id.addr[2] = buf[18];
0218 bpdu.bridge_id.addr[3] = buf[19];
0219 bpdu.bridge_id.addr[4] = buf[20];
0220 bpdu.bridge_id.addr[5] = buf[21];
0221 bpdu.port_id = (buf[22] << 8) | buf[23];
0222
0223 bpdu.message_age = br_get_ticks(buf+24);
0224 bpdu.max_age = br_get_ticks(buf+26);
0225 bpdu.hello_time = br_get_ticks(buf+28);
0226 bpdu.forward_delay = br_get_ticks(buf+30);
0227
0228 if (bpdu.message_age > bpdu.max_age) {
0229 if (net_ratelimit())
0230 br_notice(p->br,
0231 "port %u config from %pM"
0232 " (message_age %ul > max_age %ul)\n",
0233 p->port_no,
0234 eth_hdr(skb)->h_source,
0235 bpdu.message_age, bpdu.max_age);
0236 goto out;
0237 }
0238
0239 br_received_config_bpdu(p, &bpdu);
0240 } else if (buf[0] == BPDU_TYPE_TCN) {
0241 br_received_tcn_bpdu(p);
0242 }
0243 out:
0244 spin_unlock(&br->lock);
0245 err:
0246 kfree_skb(skb);
0247 }