br_forward.c 3.4 KB

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