br_forward.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Forwarding decision
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.org>
  7. *
  8. * $Id: br_forward.c,v 1.4 2001/08/14 22:05:57 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/skbuff.h>
  18. #include <linux/netfilter_bridge.h>
  19. #include "br_private.h"
  20. static inline int should_deliver(const struct net_bridge_port *p,
  21. const struct sk_buff *skb)
  22. {
  23. if (skb->dev == p->dev ||
  24. p->state != BR_STATE_FORWARDING)
  25. return 0;
  26. return 1;
  27. }
  28. int br_dev_queue_push_xmit(struct sk_buff *skb)
  29. {
  30. if (skb->len > skb->dev->mtu)
  31. kfree_skb(skb);
  32. else {
  33. #ifdef CONFIG_BRIDGE_NETFILTER
  34. /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */
  35. nf_bridge_maybe_copy_header(skb);
  36. #endif
  37. skb_push(skb, ETH_HLEN);
  38. dev_queue_xmit(skb);
  39. }
  40. return 0;
  41. }
  42. int br_forward_finish(struct sk_buff *skb)
  43. {
  44. NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
  45. br_dev_queue_push_xmit);
  46. return 0;
  47. }
  48. static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
  49. {
  50. skb->dev = to->dev;
  51. #ifdef CONFIG_NETFILTER_DEBUG
  52. skb->nf_debug = 0;
  53. #endif
  54. NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
  55. br_forward_finish);
  56. }
  57. static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
  58. {
  59. struct net_device *indev;
  60. indev = skb->dev;
  61. skb->dev = to->dev;
  62. skb->ip_summed = CHECKSUM_NONE;
  63. NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
  64. br_forward_finish);
  65. }
  66. /* called with rcu_read_lock */
  67. void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
  68. {
  69. if (should_deliver(to, skb)) {
  70. __br_deliver(to, skb);
  71. return;
  72. }
  73. kfree_skb(skb);
  74. }
  75. /* called with rcu_read_lock */
  76. void br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
  77. {
  78. if (should_deliver(to, skb)) {
  79. __br_forward(to, skb);
  80. return;
  81. }
  82. kfree_skb(skb);
  83. }
  84. /* called under bridge lock */
  85. static void br_flood(struct net_bridge *br, struct sk_buff *skb, int clone,
  86. void (*__packet_hook)(const struct net_bridge_port *p,
  87. struct sk_buff *skb))
  88. {
  89. struct net_bridge_port *p;
  90. struct net_bridge_port *prev;
  91. if (clone) {
  92. struct sk_buff *skb2;
  93. if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  94. br->statistics.tx_dropped++;
  95. return;
  96. }
  97. skb = skb2;
  98. }
  99. prev = NULL;
  100. list_for_each_entry_rcu(p, &br->port_list, list) {
  101. if (should_deliver(p, skb)) {
  102. if (prev != NULL) {
  103. struct sk_buff *skb2;
  104. if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  105. br->statistics.tx_dropped++;
  106. kfree_skb(skb);
  107. return;
  108. }
  109. __packet_hook(prev, skb2);
  110. }
  111. prev = p;
  112. }
  113. }
  114. if (prev != NULL) {
  115. __packet_hook(prev, skb);
  116. return;
  117. }
  118. kfree_skb(skb);
  119. }
  120. /* called with rcu_read_lock */
  121. void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, int clone)
  122. {
  123. br_flood(br, skb, clone, __br_deliver);
  124. }
  125. /* called under bridge lock */
  126. void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, int clone)
  127. {
  128. br_flood(br, skb, clone, __br_forward);
  129. }