br_forward.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

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