br_forward.c 7.1 KB

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