br_input.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. const unsigned char bridge_ula[6] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
  21. static int br_pass_frame_up_finish(struct sk_buff *skb)
  22. {
  23. netif_receive_skb(skb);
  24. return 0;
  25. }
  26. static void br_pass_frame_up(struct net_bridge *br, struct sk_buff *skb)
  27. {
  28. struct net_device *indev;
  29. br->statistics.rx_packets++;
  30. br->statistics.rx_bytes += skb->len;
  31. indev = skb->dev;
  32. skb->dev = br->dev;
  33. NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_IN, skb, indev, NULL,
  34. br_pass_frame_up_finish);
  35. }
  36. /* note: already called with rcu_read_lock (preempt_disabled) */
  37. int br_handle_frame_finish(struct sk_buff *skb)
  38. {
  39. const unsigned char *dest = eth_hdr(skb)->h_dest;
  40. struct net_bridge_port *p = rcu_dereference(skb->dev->br_port);
  41. struct net_bridge *br;
  42. struct net_bridge_fdb_entry *dst;
  43. int passedup = 0;
  44. if (!p || p->state == BR_STATE_DISABLED)
  45. goto drop;
  46. /* insert into forwarding database after filtering to avoid spoofing */
  47. br = p->br;
  48. br_fdb_update(br, p, eth_hdr(skb)->h_source);
  49. if (p->state == BR_STATE_LEARNING)
  50. goto drop;
  51. if (br->dev->flags & IFF_PROMISC) {
  52. struct sk_buff *skb2;
  53. skb2 = skb_clone(skb, GFP_ATOMIC);
  54. if (skb2 != NULL) {
  55. passedup = 1;
  56. br_pass_frame_up(br, skb2);
  57. }
  58. }
  59. if (is_multicast_ether_addr(dest)) {
  60. br_flood_forward(br, skb, !passedup);
  61. if (!passedup)
  62. br_pass_frame_up(br, skb);
  63. goto out;
  64. }
  65. dst = __br_fdb_get(br, dest);
  66. if (dst != NULL && dst->is_local) {
  67. if (!passedup)
  68. br_pass_frame_up(br, skb);
  69. else
  70. kfree_skb(skb);
  71. goto out;
  72. }
  73. if (dst != NULL) {
  74. br_forward(dst->dst, skb);
  75. goto out;
  76. }
  77. br_flood_forward(br, skb, 0);
  78. out:
  79. return 0;
  80. drop:
  81. kfree_skb(skb);
  82. goto out;
  83. }
  84. /*
  85. * Called via br_handle_frame_hook.
  86. * Return 0 if *pskb should be processed furthur
  87. * 1 if *pskb is handled
  88. * note: already called with rcu_read_lock (preempt_disabled)
  89. */
  90. int br_handle_frame(struct net_bridge_port *p, struct sk_buff **pskb)
  91. {
  92. struct sk_buff *skb = *pskb;
  93. const unsigned char *dest = eth_hdr(skb)->h_dest;
  94. if (p->state == BR_STATE_DISABLED)
  95. goto err;
  96. if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
  97. goto err;
  98. if (p->br->stp_enabled &&
  99. !memcmp(dest, bridge_ula, 5) &&
  100. !(dest[5] & 0xF0)) {
  101. if (!dest[5]) {
  102. NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_IN, skb, skb->dev,
  103. NULL, br_stp_handle_bpdu);
  104. return 1;
  105. }
  106. goto err;
  107. }
  108. if (p->state == BR_STATE_FORWARDING || p->state == BR_STATE_LEARNING) {
  109. if (br_should_route_hook) {
  110. if (br_should_route_hook(pskb))
  111. return 0;
  112. skb = *pskb;
  113. dest = eth_hdr(skb)->h_dest;
  114. }
  115. if (!compare_ether_addr(p->br->dev->dev_addr, dest))
  116. skb->pkt_type = PACKET_HOST;
  117. NF_HOOK(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
  118. br_handle_frame_finish);
  119. return 1;
  120. }
  121. err:
  122. kfree_skb(skb);
  123. return 1;
  124. }