br_forward.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 disabled */
  27. static inline int should_deliver(const struct net_bridge_port *p,
  28. const struct sk_buff *skb)
  29. {
  30. struct net_bridge_vlan_group *vg;
  31. vg = nbp_vlan_group_rcu(p);
  32. return ((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) &&
  33. br_allowed_egress(vg, skb) && p->state == BR_STATE_FORWARDING;
  34. }
  35. int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
  36. {
  37. skb_push(skb, ETH_HLEN);
  38. if (!is_skb_forwardable(skb->dev, skb))
  39. goto drop;
  40. br_drop_fake_rtable(skb);
  41. skb_sender_cpu_clear(skb);
  42. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  43. (skb->protocol == htons(ETH_P_8021Q) ||
  44. skb->protocol == htons(ETH_P_8021AD))) {
  45. int depth;
  46. if (!__vlan_get_protocol(skb, skb->protocol, &depth))
  47. goto drop;
  48. skb_set_network_header(skb, depth);
  49. }
  50. dev_queue_xmit(skb);
  51. return 0;
  52. drop:
  53. kfree_skb(skb);
  54. return 0;
  55. }
  56. EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit);
  57. int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  58. {
  59. return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING,
  60. net, sk, skb, NULL, skb->dev,
  61. br_dev_queue_push_xmit);
  62. }
  63. EXPORT_SYMBOL_GPL(br_forward_finish);
  64. static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
  65. {
  66. struct net_bridge_vlan_group *vg;
  67. vg = nbp_vlan_group_rcu(to);
  68. skb = br_handle_vlan(to->br, vg, skb);
  69. if (!skb)
  70. return;
  71. skb->dev = to->dev;
  72. if (unlikely(netpoll_tx_running(to->br->dev))) {
  73. skb_push(skb, ETH_HLEN);
  74. if (!is_skb_forwardable(skb->dev, skb))
  75. kfree_skb(skb);
  76. else
  77. br_netpoll_send_skb(to, skb);
  78. return;
  79. }
  80. NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT,
  81. dev_net(skb->dev), NULL, skb,NULL, skb->dev,
  82. br_forward_finish);
  83. }
  84. static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
  85. {
  86. struct net_bridge_vlan_group *vg;
  87. struct net_device *indev;
  88. if (skb_warn_if_lro(skb)) {
  89. kfree_skb(skb);
  90. return;
  91. }
  92. vg = nbp_vlan_group_rcu(to);
  93. skb = br_handle_vlan(to->br, vg, skb);
  94. if (!skb)
  95. return;
  96. indev = skb->dev;
  97. skb->dev = to->dev;
  98. skb_forward_csum(skb);
  99. NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD,
  100. dev_net(indev), NULL, skb, indev, skb->dev,
  101. br_forward_finish);
  102. }
  103. /* called with rcu_read_lock */
  104. void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
  105. {
  106. if (to && should_deliver(to, skb)) {
  107. __br_deliver(to, skb);
  108. return;
  109. }
  110. kfree_skb(skb);
  111. }
  112. EXPORT_SYMBOL_GPL(br_deliver);
  113. /* called with rcu_read_lock */
  114. void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0)
  115. {
  116. if (to && should_deliver(to, skb)) {
  117. if (skb0)
  118. deliver_clone(to, skb, __br_forward);
  119. else
  120. __br_forward(to, skb);
  121. return;
  122. }
  123. if (!skb0)
  124. kfree_skb(skb);
  125. }
  126. static int deliver_clone(const struct net_bridge_port *prev,
  127. struct sk_buff *skb,
  128. void (*__packet_hook)(const struct net_bridge_port *p,
  129. struct sk_buff *skb))
  130. {
  131. struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  132. skb = skb_clone(skb, GFP_ATOMIC);
  133. if (!skb) {
  134. dev->stats.tx_dropped++;
  135. return -ENOMEM;
  136. }
  137. __packet_hook(prev, skb);
  138. return 0;
  139. }
  140. static struct net_bridge_port *maybe_deliver(
  141. struct net_bridge_port *prev, struct net_bridge_port *p,
  142. struct sk_buff *skb,
  143. void (*__packet_hook)(const struct net_bridge_port *p,
  144. struct sk_buff *skb))
  145. {
  146. int err;
  147. if (!should_deliver(p, skb))
  148. return prev;
  149. if (!prev)
  150. goto out;
  151. err = deliver_clone(prev, skb, __packet_hook);
  152. if (err)
  153. return ERR_PTR(err);
  154. out:
  155. return p;
  156. }
  157. /* called under bridge lock */
  158. static void br_flood(struct net_bridge *br, struct sk_buff *skb,
  159. struct sk_buff *skb0,
  160. void (*__packet_hook)(const struct net_bridge_port *p,
  161. struct sk_buff *skb),
  162. bool unicast)
  163. {
  164. struct net_bridge_port *p;
  165. struct net_bridge_port *prev;
  166. prev = NULL;
  167. list_for_each_entry_rcu(p, &br->port_list, list) {
  168. /* Do not flood unicast traffic to ports that turn it off */
  169. if (unicast && !(p->flags & BR_FLOOD))
  170. continue;
  171. /* Do not flood to ports that enable proxy ARP */
  172. if (p->flags & BR_PROXYARP)
  173. continue;
  174. if ((p->flags & BR_PROXYARP_WIFI) &&
  175. BR_INPUT_SKB_CB(skb)->proxyarp_replied)
  176. continue;
  177. prev = maybe_deliver(prev, p, skb, __packet_hook);
  178. if (IS_ERR(prev))
  179. goto out;
  180. }
  181. if (!prev)
  182. goto out;
  183. if (skb0)
  184. deliver_clone(prev, skb, __packet_hook);
  185. else
  186. __packet_hook(prev, skb);
  187. return;
  188. out:
  189. if (!skb0)
  190. kfree_skb(skb);
  191. }
  192. /* called with rcu_read_lock */
  193. void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, bool unicast)
  194. {
  195. br_flood(br, skb, NULL, __br_deliver, unicast);
  196. }
  197. /* called under bridge lock */
  198. void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
  199. struct sk_buff *skb2, bool unicast)
  200. {
  201. br_flood(br, skb, skb2, __br_forward, unicast);
  202. }
  203. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  204. /* called with rcu_read_lock */
  205. static void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
  206. struct sk_buff *skb, struct sk_buff *skb0,
  207. void (*__packet_hook)(
  208. const struct net_bridge_port *p,
  209. struct sk_buff *skb))
  210. {
  211. struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  212. struct net_bridge *br = netdev_priv(dev);
  213. struct net_bridge_port *prev = NULL;
  214. struct net_bridge_port_group *p;
  215. struct hlist_node *rp;
  216. rp = rcu_dereference(hlist_first_rcu(&br->router_list));
  217. p = mdst ? rcu_dereference(mdst->ports) : NULL;
  218. while (p || rp) {
  219. struct net_bridge_port *port, *lport, *rport;
  220. lport = p ? p->port : NULL;
  221. rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
  222. NULL;
  223. port = (unsigned long)lport > (unsigned long)rport ?
  224. lport : rport;
  225. prev = maybe_deliver(prev, port, skb, __packet_hook);
  226. if (IS_ERR(prev))
  227. goto out;
  228. if ((unsigned long)lport >= (unsigned long)port)
  229. p = rcu_dereference(p->next);
  230. if ((unsigned long)rport >= (unsigned long)port)
  231. rp = rcu_dereference(hlist_next_rcu(rp));
  232. }
  233. if (!prev)
  234. goto out;
  235. if (skb0)
  236. deliver_clone(prev, skb, __packet_hook);
  237. else
  238. __packet_hook(prev, skb);
  239. return;
  240. out:
  241. if (!skb0)
  242. kfree_skb(skb);
  243. }
  244. /* called with rcu_read_lock */
  245. void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
  246. struct sk_buff *skb)
  247. {
  248. br_multicast_flood(mdst, skb, NULL, __br_deliver);
  249. }
  250. /* called with rcu_read_lock */
  251. void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
  252. struct sk_buff *skb, struct sk_buff *skb2)
  253. {
  254. br_multicast_flood(mdst, skb, skb2, __br_forward);
  255. }
  256. #endif