br_input.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Handle incoming frames
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.org>
  7. *
  8. * $Id: br_input.c,v 1.10 2001/12/24 04:50:20 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/netdevice.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/netfilter_bridge.h>
  19. #include "br_private.h"
  20. /* Bridge group multicast address 802.1d (pg 51). */
  21. const u8 br_group_address[ETH_ALEN] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
  22. static void br_pass_frame_up(struct net_bridge *br, struct sk_buff *skb)
  23. {
  24. struct net_device *indev;
  25. br->statistics.rx_packets++;
  26. br->statistics.rx_bytes += skb->len;
  27. indev = skb->dev;
  28. skb->dev = br->dev;
  29. NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_IN, skb, indev, NULL,
  30. netif_receive_skb);
  31. }
  32. /* note: already called with rcu_read_lock (preempt_disabled) */
  33. int br_handle_frame_finish(struct sk_buff *skb)
  34. {
  35. const unsigned char *dest = eth_hdr(skb)->h_dest;
  36. struct net_bridge_port *p = rcu_dereference(skb->dev->br_port);
  37. struct net_bridge *br;
  38. struct net_bridge_fdb_entry *dst;
  39. struct sk_buff *skb2;
  40. if (!p || p->state == BR_STATE_DISABLED)
  41. goto drop;
  42. /* insert into forwarding database after filtering to avoid spoofing */
  43. br = p->br;
  44. br_fdb_update(br, p, eth_hdr(skb)->h_source);
  45. if (p->state == BR_STATE_LEARNING)
  46. goto drop;
  47. /* The packet skb2 goes to the local host (NULL to skip). */
  48. skb2 = NULL;
  49. if (br->dev->flags & IFF_PROMISC)
  50. skb2 = skb;
  51. dst = NULL;
  52. if (is_multicast_ether_addr(dest)) {
  53. br->statistics.multicast++;
  54. skb2 = skb;
  55. } else if ((dst = __br_fdb_get(br, dest)) && dst->is_local) {
  56. skb2 = skb;
  57. /* Do not forward the packet since it's local. */
  58. skb = NULL;
  59. }
  60. if (skb2 == skb)
  61. skb2 = skb_clone(skb, GFP_ATOMIC);
  62. if (skb2)
  63. br_pass_frame_up(br, skb2);
  64. if (skb) {
  65. if (dst)
  66. br_forward(dst->dst, skb);
  67. else
  68. br_flood_forward(br, skb);
  69. }
  70. out:
  71. return 0;
  72. drop:
  73. kfree_skb(skb);
  74. goto out;
  75. }
  76. /* note: already called with rcu_read_lock (preempt_disabled) */
  77. static int br_handle_local_finish(struct sk_buff *skb)
  78. {
  79. struct net_bridge_port *p = rcu_dereference(skb->dev->br_port);
  80. if (p)
  81. br_fdb_update(p->br, p, eth_hdr(skb)->h_source);
  82. return 0; /* process further */
  83. }
  84. /* Does address match the link local multicast address.
  85. * 01:80:c2:00:00:0X
  86. */
  87. static inline int is_link_local(const unsigned char *dest)
  88. {
  89. __be16 *a = (__be16 *)dest;
  90. static const __be16 *b = (const __be16 *)br_group_address;
  91. static const __be16 m = cpu_to_be16(0xfff0);
  92. return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | ((a[2] ^ b[2]) & m)) == 0;
  93. }
  94. /*
  95. * Called via br_handle_frame_hook.
  96. * Return NULL if skb is handled
  97. * note: already called with rcu_read_lock (preempt_disabled)
  98. */
  99. struct sk_buff *br_handle_frame(struct net_bridge_port *p, struct sk_buff *skb)
  100. {
  101. const unsigned char *dest = eth_hdr(skb)->h_dest;
  102. int (*rhook)(struct sk_buff *skb);
  103. if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
  104. goto drop;
  105. skb = skb_share_check(skb, GFP_ATOMIC);
  106. if (!skb)
  107. return NULL;
  108. if (unlikely(is_link_local(dest))) {
  109. /* Pause frames shouldn't be passed up by driver anyway */
  110. if (skb->protocol == htons(ETH_P_PAUSE))
  111. goto drop;
  112. /* Process STP BPDU's through normal netif_receive_skb() path */
  113. if (p->br->stp_enabled != BR_NO_STP) {
  114. if (NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_IN, skb, skb->dev,
  115. NULL, br_handle_local_finish))
  116. return NULL;
  117. else
  118. return skb;
  119. }
  120. }
  121. switch (p->state) {
  122. case BR_STATE_FORWARDING:
  123. rhook = rcu_dereference(br_should_route_hook);
  124. if (rhook != NULL) {
  125. if (rhook(skb))
  126. return skb;
  127. dest = eth_hdr(skb)->h_dest;
  128. }
  129. /* fall through */
  130. case BR_STATE_LEARNING:
  131. if (!compare_ether_addr(p->br->dev->dev_addr, dest))
  132. skb->pkt_type = PACKET_HOST;
  133. NF_HOOK(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
  134. br_handle_frame_finish);
  135. break;
  136. default:
  137. drop:
  138. kfree_skb(skb);
  139. }
  140. return NULL;
  141. }