br_forward.c 6.3 KB

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