br_forward.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
  52. br_forward_finish);
  53. }
  54. static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
  55. {
  56. struct net_device *indev;
  57. indev = skb->dev;
  58. skb->dev = to->dev;
  59. skb->ip_summed = CHECKSUM_NONE;
  60. NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
  61. br_forward_finish);
  62. }
  63. /* called with rcu_read_lock */
  64. void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
  65. {
  66. if (should_deliver(to, skb)) {
  67. __br_deliver(to, skb);
  68. return;
  69. }
  70. kfree_skb(skb);
  71. }
  72. /* called with rcu_read_lock */
  73. void br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
  74. {
  75. if (should_deliver(to, skb)) {
  76. __br_forward(to, skb);
  77. return;
  78. }
  79. kfree_skb(skb);
  80. }
  81. /* called under bridge lock */
  82. static void br_flood(struct net_bridge *br, struct sk_buff *skb, int clone,
  83. void (*__packet_hook)(const struct net_bridge_port *p,
  84. struct sk_buff *skb))
  85. {
  86. struct net_bridge_port *p;
  87. struct net_bridge_port *prev;
  88. if (clone) {
  89. struct sk_buff *skb2;
  90. if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  91. br->statistics.tx_dropped++;
  92. return;
  93. }
  94. skb = skb2;
  95. }
  96. prev = NULL;
  97. list_for_each_entry_rcu(p, &br->port_list, list) {
  98. if (should_deliver(p, skb)) {
  99. if (prev != NULL) {
  100. struct sk_buff *skb2;
  101. if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  102. br->statistics.tx_dropped++;
  103. kfree_skb(skb);
  104. return;
  105. }
  106. __packet_hook(prev, skb2);
  107. }
  108. prev = p;
  109. }
  110. }
  111. if (prev != NULL) {
  112. __packet_hook(prev, skb);
  113. return;
  114. }
  115. kfree_skb(skb);
  116. }
  117. /* called with rcu_read_lock */
  118. void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, int clone)
  119. {
  120. br_flood(br, skb, clone, __br_deliver);
  121. }
  122. /* called under bridge lock */
  123. void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, int clone)
  124. {
  125. br_flood(br, skb, clone, __br_forward);
  126. }