br_forward.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:3k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  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/inetdevice.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/if_bridge.h>
  20. #include <linux/netfilter_bridge.h>
  21. #include "br_private.h"
  22. static inline int should_deliver(struct net_bridge_port *p, 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 int __dev_queue_push_xmit(struct sk_buff *skb)
  30. {
  31. skb_push(skb, ETH_HLEN);
  32. dev_queue_xmit(skb);
  33. return 0;
  34. }
  35. static int __br_forward_finish(struct sk_buff *skb)
  36. {
  37. NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
  38. __dev_queue_push_xmit);
  39. return 0;
  40. }
  41. static void __br_deliver(struct net_bridge_port *to, struct sk_buff *skb)
  42. {
  43. struct net_device *indev;
  44. indev = skb->dev;
  45. skb->dev = to->dev;
  46. NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, indev, skb->dev,
  47. __br_forward_finish);
  48. }
  49. static void __br_forward(struct net_bridge_port *to, struct sk_buff *skb)
  50. {
  51. struct net_device *indev;
  52. indev = skb->dev;
  53. skb->dev = to->dev;
  54. NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
  55. __br_forward_finish);
  56. }
  57. /* called under bridge lock */
  58. void br_deliver(struct net_bridge_port *to, struct sk_buff *skb)
  59. {
  60. if (should_deliver(to, skb)) {
  61. __br_deliver(to, skb);
  62. return;
  63. }
  64. kfree_skb(skb);
  65. }
  66. /* called under bridge lock */
  67. void br_forward(struct net_bridge_port *to, struct sk_buff *skb)
  68. {
  69. if (should_deliver(to, skb)) {
  70. __br_forward(to, skb);
  71. return;
  72. }
  73. kfree_skb(skb);
  74. }
  75. /* called under bridge lock */
  76. static void br_flood(struct net_bridge *br, struct sk_buff *skb, int clone,
  77. void (*__packet_hook)(struct net_bridge_port *p, struct sk_buff *skb))
  78. {
  79. struct net_bridge_port *p;
  80. struct net_bridge_port *prev;
  81. if (clone) {
  82. struct sk_buff *skb2;
  83. if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  84. br->statistics.tx_dropped++;
  85. return;
  86. }
  87. skb = skb2;
  88. }
  89. prev = NULL;
  90. p = br->port_list;
  91. while (p != NULL) {
  92. if (should_deliver(p, skb)) {
  93. if (prev != NULL) {
  94. struct sk_buff *skb2;
  95. if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
  96. br->statistics.tx_dropped++;
  97. kfree_skb(skb);
  98. return;
  99. }
  100. __packet_hook(prev, skb2);
  101. }
  102. prev = p;
  103. }
  104. p = p->next;
  105. }
  106. if (prev != NULL) {
  107. __packet_hook(prev, skb);
  108. return;
  109. }
  110. kfree_skb(skb);
  111. }
  112. /* called under bridge lock */
  113. void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, int clone)
  114. {
  115. br_flood(br, skb, clone, __br_deliver);
  116. }
  117. /* called under bridge lock */
  118. void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, int clone)
  119. {
  120. br_flood(br, skb, clone, __br_forward);
  121. }