br_forward.c 3.4 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/if_vlan.h>
  19. #include <linux/netfilter_bridge.h>
  20. #include "br_private.h"
  21. /* Don't forward packets to originating port or forwarding diasabled */
  22. static inline int should_deliver(const struct net_bridge_port *p,
  23. const struct sk_buff *skb)
  24. {
  25. return (skb->dev != p->dev && p->state == BR_STATE_FORWARDING);
  26. }
  27. static inline unsigned packet_length(const struct sk_buff *skb)
  28. {
  29. return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
  30. }
  31. int br_dev_queue_push_xmit(struct sk_buff *skb)
  32. {
  33. /* drop mtu oversized packets except gso */
  34. if (packet_length(skb) > skb->dev->mtu && !skb_shinfo(skb)->gso_size)
  35. kfree_skb(skb);
  36. else {
  37. #ifdef CONFIG_BRIDGE_NETFILTER
  38. /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */
  39. nf_bridge_maybe_copy_header(skb);
  40. #endif
  41. skb_push(skb, ETH_HLEN);
  42. dev_queue_xmit(skb);
  43. }
  44. return 0;
  45. }
  46. int br_forward_finish(struct sk_buff *skb)
  47. {
  48. return NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
  49. br_dev_queue_push_xmit);
  50. }
  51. static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
  52. {
  53. skb->dev = to->dev;
  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. }