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