br_forward.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Forwarding decision
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/slab.h>
  15. #include <linux/kernel.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/netpoll.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/if_vlan.h>
  20. #include <linux/netfilter_bridge.h>
  21. #include "br_private.h"
  22. static int deliver_clone(const struct net_bridge_port *prev,
  23. struct sk_buff *skb,
  24. void (*__packet_hook)(const struct net_bridge_port *p,
  25. struct sk_buff *skb));
  26. /* Don't forward packets to originating port or forwarding diasabled */
  27. static inline int should_deliver(const struct net_bridge_port *p,
  28. const struct sk_buff *skb)
  29. {
  30. return (((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) &&
  31. p->state == BR_STATE_FORWARDING);
  32. }
  33. static inline unsigned packet_length(const struct sk_buff *skb)
  34. {
  35. return skb->len - (skb->protocol == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
  36. }
  37. int br_dev_queue_push_xmit(struct sk_buff *skb)
  38. {
  39. /* ip_fragment doesn't copy the MAC header */
  40. if (nf_bridge_maybe_copy_header(skb) ||
  41. (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb))) {
  42. kfree_skb(skb);
  43. } else {
  44. skb_push(skb, ETH_HLEN);
  45. br_drop_fake_rtable(skb);
  46. dev_queue_xmit(skb);
  47. }
  48. return 0;
  49. }
  50. int br_forward_finish(struct sk_buff *skb)
  51. {
  52. return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
  53. br_dev_queue_push_xmit);
  54. }
  55. static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
  56. {
  57. skb->dev = to->dev;
  58. if (unlikely(netpoll_tx_running(to->dev))) {
  59. if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb))
  60. kfree_skb(skb);
  61. else {
  62. skb_push(skb, ETH_HLEN);
  63. br_netpoll_send_skb(to, skb);
  64. }
  65. return;
  66. }
  67. NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
  68. br_forward_finish);
  69. }
  70. static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
  71. {
  72. struct net_device *indev;
  73. if (skb_warn_if_lro(skb)) {
  74. kfree_skb(skb);
  75. return;
  76. }
  77. indev = skb->dev;
  78. skb->dev = to->dev;
  79. skb_forward_csum(skb);
  80. NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
  81. br_forward_finish);
  82. }
  83. /* called with rcu_read_lock */
  84. void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
  85. {
  86. if (to && should_deliver(to, skb)) {
  87. __br_deliver(to, skb);
  88. return;
  89. }
  90. kfree_skb(skb);
  91. }
  92. /* called with rcu_read_lock */
  93. void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0)
  94. {
  95. if (should_deliver(to, skb)) {
  96. if (skb0)
  97. deliver_clone(to, skb, __br_forward);
  98. else
  99. __br_forward(to, skb);
  100. return;
  101. }
  102. if (!skb0)
  103. kfree_skb(skb);
  104. }
  105. static int deliver_clone(const struct net_bridge_port *prev,
  106. struct sk_buff *skb,
  107. void (*__packet_hook)(const struct net_bridge_port *p,
  108. struct sk_buff *skb))
  109. {
  110. struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  111. skb = skb_clone(skb, GFP_ATOMIC);
  112. if (!skb) {
  113. dev->stats.tx_dropped++;
  114. return -ENOMEM;
  115. }
  116. __packet_hook(prev, skb);
  117. return 0;
  118. }
  119. static struct net_bridge_port *maybe_deliver(
  120. struct net_bridge_port *prev, struct net_bridge_port *p,
  121. struct sk_buff *skb,
  122. void (*__packet_hook)(const struct net_bridge_port *p,
  123. struct sk_buff *skb))
  124. {
  125. int err;
  126. if (!should_deliver(p, skb))
  127. return prev;
  128. if (!prev)
  129. goto out;
  130. err = deliver_clone(prev, skb, __packet_hook);
  131. if (err)
  132. return ERR_PTR(err);
  133. out:
  134. return p;
  135. }
  136. /* called under bridge lock */
  137. static void br_flood(struct net_bridge *br, struct sk_buff *skb,
  138. struct sk_buff *skb0,
  139. void (*__packet_hook)(const struct net_bridge_port *p,
  140. struct sk_buff *skb))
  141. {
  142. struct net_bridge_port *p;
  143. struct net_bridge_port *prev;
  144. prev = NULL;
  145. list_for_each_entry_rcu(p, &br->port_list, list) {
  146. prev = maybe_deliver(prev, p, skb, __packet_hook);
  147. if (IS_ERR(prev))
  148. goto out;
  149. }
  150. if (!prev)
  151. goto out;
  152. if (skb0)
  153. deliver_clone(prev, skb, __packet_hook);
  154. else
  155. __packet_hook(prev, skb);
  156. return;
  157. out:
  158. if (!skb0)
  159. kfree_skb(skb);
  160. }
  161. /* called with rcu_read_lock */
  162. void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb)
  163. {
  164. br_flood(br, skb, NULL, __br_deliver);
  165. }
  166. /* called under bridge lock */
  167. void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
  168. struct sk_buff *skb2)
  169. {
  170. br_flood(br, skb, skb2, __br_forward);
  171. }
  172. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  173. /* called with rcu_read_lock */
  174. static void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
  175. struct sk_buff *skb, struct sk_buff *skb0,
  176. void (*__packet_hook)(
  177. const struct net_bridge_port *p,
  178. struct sk_buff *skb))
  179. {
  180. struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  181. struct net_bridge *br = netdev_priv(dev);
  182. struct net_bridge_port *prev = NULL;
  183. struct net_bridge_port_group *p;
  184. struct hlist_node *rp;
  185. rp = rcu_dereference(hlist_first_rcu(&br->router_list));
  186. p = mdst ? rcu_dereference(mdst->ports) : NULL;
  187. while (p || rp) {
  188. struct net_bridge_port *port, *lport, *rport;
  189. lport = p ? p->port : NULL;
  190. rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
  191. NULL;
  192. port = (unsigned long)lport > (unsigned long)rport ?
  193. lport : rport;
  194. prev = maybe_deliver(prev, port, skb, __packet_hook);
  195. if (IS_ERR(prev))
  196. goto out;
  197. if ((unsigned long)lport >= (unsigned long)port)
  198. p = rcu_dereference(p->next);
  199. if ((unsigned long)rport >= (unsigned long)port)
  200. rp = rcu_dereference(hlist_next_rcu(rp));
  201. }
  202. if (!prev)
  203. goto out;
  204. if (skb0)
  205. deliver_clone(prev, skb, __packet_hook);
  206. else
  207. __packet_hook(prev, skb);
  208. return;
  209. out:
  210. if (!skb0)
  211. kfree_skb(skb);
  212. }
  213. /* called with rcu_read_lock */
  214. void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
  215. struct sk_buff *skb)
  216. {
  217. br_multicast_flood(mdst, skb, NULL, __br_deliver);
  218. }
  219. /* called with rcu_read_lock */
  220. void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
  221. struct sk_buff *skb, struct sk_buff *skb2)
  222. {
  223. br_multicast_flood(mdst, skb, skb2, __br_forward);
  224. }
  225. #endif