br_forward.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_is_gso(skb))
  35. kfree_skb(skb);
  36. else {
  37. /* ip_refrag calls ip_fragment, doesn't copy the MAC header. */
  38. if (nf_bridge_maybe_copy_header(skb))
  39. kfree_skb(skb);
  40. else {
  41. skb_push(skb, ETH_HLEN);
  42. dev_queue_xmit(skb);
  43. }
  44. }
  45. return 0;
  46. }
  47. int br_forward_finish(struct sk_buff *skb)
  48. {
  49. return NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
  50. br_dev_queue_push_xmit);
  51. }
  52. static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
  53. {
  54. skb->dev = to->dev;
  55. NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
  56. br_forward_finish);
  57. }
  58. static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
  59. {
  60. struct net_device *indev;
  61. indev = skb->dev;
  62. skb->dev = to->dev;
  63. skb_forward_csum(skb);
  64. NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
  65. br_forward_finish);
  66. }
  67. /* called with rcu_read_lock */
  68. void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
  69. {
  70. if (should_deliver(to, skb)) {
  71. __br_deliver(to, skb);
  72. return;
  73. }
  74. kfree_skb(skb);
  75. }
  76. /* called with rcu_read_lock */
  77. void br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
  78. {
  79. if (should_deliver(to, skb)) {
  80. __br_forward(to, skb);
  81. return;
  82. }
  83. kfree_skb(skb);
  84. }
  85. /* called under bridge lock */
  86. static void br_flood(struct net_bridge *br, struct sk_buff *skb,
  87. void (*__packet_hook)(const struct net_bridge_port *p,
  88. struct sk_buff *skb))
  89. {
  90. struct net_bridge_port *p;
  91. struct net_bridge_port *prev;
  92. prev = NULL;
  93. list_for_each_entry_rcu(p, &br->port_list, list) {
  94. if (should_deliver(p, skb)) {
  95. if (prev != NULL) {
  96. struct sk_buff *skb2;
  97. if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  98. br->statistics.tx_dropped++;
  99. kfree_skb(skb);
  100. return;
  101. }
  102. __packet_hook(prev, skb2);
  103. }
  104. prev = p;
  105. }
  106. }
  107. if (prev != NULL) {
  108. __packet_hook(prev, skb);
  109. return;
  110. }
  111. kfree_skb(skb);
  112. }
  113. /* called with rcu_read_lock */
  114. void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb)
  115. {
  116. br_flood(br, skb, __br_deliver);
  117. }
  118. /* called under bridge lock */
  119. void br_flood_forward(struct net_bridge *br, struct sk_buff *skb)
  120. {
  121. br_flood(br, skb, __br_forward);
  122. }