br_stp_bpdu.c 5.2 KB

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