br_input.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/netfilter_bridge.h>
  18. #include <linux/export.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. /* Hook for brouter */
  23. br_should_route_hook_t __rcu *br_should_route_hook __read_mostly;
  24. EXPORT_SYMBOL(br_should_route_hook);
  25. static int br_pass_frame_up(struct sk_buff *skb)
  26. {
  27. struct net_device *indev, *brdev = BR_INPUT_SKB_CB(skb)->brdev;
  28. struct net_bridge *br = netdev_priv(brdev);
  29. struct br_cpu_netstats *brstats = this_cpu_ptr(br->stats);
  30. u64_stats_update_begin(&brstats->syncp);
  31. brstats->rx_packets++;
  32. brstats->rx_bytes += skb->len;
  33. u64_stats_update_end(&brstats->syncp);
  34. indev = skb->dev;
  35. skb->dev = brdev;
  36. return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, skb, indev, NULL,
  37. netif_receive_skb);
  38. }
  39. /* note: already called with rcu_read_lock */
  40. int br_handle_frame_finish(struct sk_buff *skb)
  41. {
  42. const unsigned char *dest = eth_hdr(skb)->h_dest;
  43. struct net_bridge_port *p = br_port_get_rcu(skb->dev);
  44. struct net_bridge *br;
  45. struct net_bridge_fdb_entry *dst;
  46. struct net_bridge_mdb_entry *mdst;
  47. struct sk_buff *skb2;
  48. if (!p || p->state == BR_STATE_DISABLED)
  49. goto drop;
  50. /* insert into forwarding database after filtering to avoid spoofing */
  51. br = p->br;
  52. br_fdb_update(br, p, eth_hdr(skb)->h_source);
  53. if (!is_broadcast_ether_addr(dest) && is_multicast_ether_addr(dest) &&
  54. br_multicast_rcv(br, p, skb))
  55. goto drop;
  56. if (p->state == BR_STATE_LEARNING)
  57. goto drop;
  58. BR_INPUT_SKB_CB(skb)->brdev = br->dev;
  59. /* The packet skb2 goes to the local host (NULL to skip). */
  60. skb2 = NULL;
  61. if (br->dev->flags & IFF_PROMISC)
  62. skb2 = skb;
  63. dst = NULL;
  64. if (is_broadcast_ether_addr(dest))
  65. skb2 = skb;
  66. else if (is_multicast_ether_addr(dest)) {
  67. mdst = br_mdb_get(br, skb);
  68. if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) {
  69. if ((mdst && mdst->mglist) ||
  70. br_multicast_is_router(br))
  71. skb2 = skb;
  72. br_multicast_forward(mdst, skb, skb2);
  73. skb = NULL;
  74. if (!skb2)
  75. goto out;
  76. } else
  77. skb2 = skb;
  78. br->dev->stats.multicast++;
  79. } else if ((dst = __br_fdb_get(br, dest)) && dst->is_local) {
  80. skb2 = skb;
  81. /* Do not forward the packet since it's local. */
  82. skb = NULL;
  83. }
  84. if (skb) {
  85. if (dst) {
  86. dst->used = jiffies;
  87. br_forward(dst->dst, skb, skb2);
  88. } else
  89. br_flood_forward(br, skb, skb2);
  90. }
  91. if (skb2)
  92. return br_pass_frame_up(skb2);
  93. out:
  94. return 0;
  95. drop:
  96. kfree_skb(skb);
  97. goto out;
  98. }
  99. /* note: already called with rcu_read_lock */
  100. static int br_handle_local_finish(struct sk_buff *skb)
  101. {
  102. struct net_bridge_port *p = br_port_get_rcu(skb->dev);
  103. br_fdb_update(p->br, p, eth_hdr(skb)->h_source);
  104. return 0; /* process further */
  105. }
  106. /* Does address match the link local multicast address.
  107. * 01:80:c2:00:00:0X
  108. */
  109. static inline int is_link_local(const unsigned char *dest)
  110. {
  111. __be16 *a = (__be16 *)dest;
  112. static const __be16 *b = (const __be16 *)br_group_address;
  113. static const __be16 m = cpu_to_be16(0xfff0);
  114. return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | ((a[2] ^ b[2]) & m)) == 0;
  115. }
  116. /*
  117. * Return NULL if skb is handled
  118. * note: already called with rcu_read_lock
  119. */
  120. rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
  121. {
  122. struct net_bridge_port *p;
  123. struct sk_buff *skb = *pskb;
  124. const unsigned char *dest = eth_hdr(skb)->h_dest;
  125. br_should_route_hook_t *rhook;
  126. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  127. return RX_HANDLER_PASS;
  128. if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
  129. goto drop;
  130. skb = skb_share_check(skb, GFP_ATOMIC);
  131. if (!skb)
  132. return RX_HANDLER_CONSUMED;
  133. p = br_port_get_rcu(skb->dev);
  134. if (unlikely(is_link_local(dest))) {
  135. /*
  136. * See IEEE 802.1D Table 7-10 Reserved addresses
  137. *
  138. * Assignment Value
  139. * Bridge Group Address 01-80-C2-00-00-00
  140. * (MAC Control) 802.3 01-80-C2-00-00-01
  141. * (Link Aggregation) 802.3 01-80-C2-00-00-02
  142. * 802.1X PAE address 01-80-C2-00-00-03
  143. *
  144. * 802.1AB LLDP 01-80-C2-00-00-0E
  145. *
  146. * Others reserved for future standardization
  147. */
  148. switch (dest[5]) {
  149. case 0x00: /* Bridge Group Address */
  150. /* If STP is turned off,
  151. then must forward to keep loop detection */
  152. if (p->br->stp_enabled == BR_NO_STP)
  153. goto forward;
  154. break;
  155. case 0x01: /* IEEE MAC (Pause) */
  156. goto drop;
  157. default:
  158. /* Allow selective forwarding for most other protocols */
  159. if (p->br->group_fwd_mask & (1u << dest[5]))
  160. goto forward;
  161. }
  162. /* Deliver packet to local host only */
  163. if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, skb, skb->dev,
  164. NULL, br_handle_local_finish)) {
  165. return RX_HANDLER_CONSUMED; /* consumed by filter */
  166. } else {
  167. *pskb = skb;
  168. return RX_HANDLER_PASS; /* continue processing */
  169. }
  170. }
  171. forward:
  172. switch (p->state) {
  173. case BR_STATE_FORWARDING:
  174. rhook = rcu_dereference(br_should_route_hook);
  175. if (rhook) {
  176. if ((*rhook)(skb)) {
  177. *pskb = skb;
  178. return RX_HANDLER_PASS;
  179. }
  180. dest = eth_hdr(skb)->h_dest;
  181. }
  182. /* fall through */
  183. case BR_STATE_LEARNING:
  184. if (!compare_ether_addr(p->br->dev->dev_addr, dest))
  185. skb->pkt_type = PACKET_HOST;
  186. NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
  187. br_handle_frame_finish);
  188. break;
  189. default:
  190. drop:
  191. kfree_skb(skb);
  192. }
  193. return RX_HANDLER_CONSUMED;
  194. }