br_forward.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Forwarding decision
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <[email protected]>
  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. skb_push(skb, ETH_HLEN);
  35. if (!is_skb_forwardable(skb->dev, skb))
  36. goto drop;
  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. skb_push(skb, ETH_HLEN);
  84. if (!is_skb_forwardable(skb->dev, skb))
  85. kfree_skb(skb);
  86. else
  87. br_netpoll_send_skb(to, skb);
  88. return;
  89. }
  90. br_hook = NF_BR_LOCAL_OUT;
  91. net = dev_net(skb->dev);
  92. indev = NULL;
  93. }
  94. NF_HOOK(NFPROTO_BRIDGE, br_hook,
  95. net, NULL, skb, indev, skb->dev,
  96. br_forward_finish);
  97. }
  98. static int deliver_clone(const struct net_bridge_port *prev,
  99. struct sk_buff *skb, bool local_orig)
  100. {
  101. struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  102. skb = skb_clone(skb, GFP_ATOMIC);
  103. if (!skb) {
  104. dev->stats.tx_dropped++;
  105. return -ENOMEM;
  106. }
  107. __br_forward(prev, skb, local_orig);
  108. return 0;
  109. }
  110. /**
  111. * br_forward - forward a packet to a specific port
  112. * @to: destination port
  113. * @skb: packet being forwarded
  114. * @local_rcv: packet will be received locally after forwarding
  115. * @local_orig: packet is locally originated
  116. *
  117. * Should be called with rcu_read_lock.
  118. */
  119. void br_forward(const struct net_bridge_port *to,
  120. struct sk_buff *skb, bool local_rcv, bool local_orig)
  121. {
  122. if (to && should_deliver(to, skb)) {
  123. if (local_rcv)
  124. deliver_clone(to, skb, local_orig);
  125. else
  126. __br_forward(to, skb, local_orig);
  127. return;
  128. }
  129. if (!local_rcv)
  130. kfree_skb(skb);
  131. }
  132. EXPORT_SYMBOL_GPL(br_forward);
  133. static struct net_bridge_port *maybe_deliver(
  134. struct net_bridge_port *prev, struct net_bridge_port *p,
  135. struct sk_buff *skb, bool local_orig)
  136. {
  137. int err;
  138. if (!should_deliver(p, skb))
  139. return prev;
  140. if (!prev)
  141. goto out;
  142. err = deliver_clone(prev, skb, local_orig);
  143. if (err)
  144. return ERR_PTR(err);
  145. out:
  146. return p;
  147. }
  148. /* called under rcu_read_lock */
  149. void br_flood(struct net_bridge *br, struct sk_buff *skb,
  150. enum br_pkt_type pkt_type, bool local_rcv, bool local_orig)
  151. {
  152. u8 igmp_type = br_multicast_igmp_type(skb);
  153. struct net_bridge_port *prev = NULL;
  154. struct net_bridge_port *p;
  155. list_for_each_entry_rcu(p, &br->port_list, list) {
  156. /* Do not flood unicast traffic to ports that turn it off */
  157. if (pkt_type == BR_PKT_UNICAST && !(p->flags & BR_FLOOD))
  158. continue;
  159. /* Do not flood if mc off, except for traffic we originate */
  160. if (pkt_type == BR_PKT_MULTICAST &&
  161. !(p->flags & BR_MCAST_FLOOD) && skb->dev != br->dev)
  162. continue;
  163. /* Do not flood to ports that enable proxy ARP */
  164. if (p->flags & BR_PROXYARP)
  165. continue;
  166. if ((p->flags & BR_PROXYARP_WIFI) &&
  167. BR_INPUT_SKB_CB(skb)->proxyarp_replied)
  168. continue;
  169. prev = maybe_deliver(prev, p, skb, local_orig);
  170. if (IS_ERR(prev))
  171. goto out;
  172. if (prev == p)
  173. br_multicast_count(p->br, p, skb, igmp_type,
  174. BR_MCAST_DIR_TX);
  175. }
  176. if (!prev)
  177. goto out;
  178. if (local_rcv)
  179. deliver_clone(prev, skb, local_orig);
  180. else
  181. __br_forward(prev, skb, local_orig);
  182. return;
  183. out:
  184. if (!local_rcv)
  185. kfree_skb(skb);
  186. }
  187. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  188. /* called with rcu_read_lock */
  189. void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
  190. struct sk_buff *skb,
  191. bool local_rcv, bool local_orig)
  192. {
  193. struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  194. u8 igmp_type = br_multicast_igmp_type(skb);
  195. struct net_bridge *br = netdev_priv(dev);
  196. struct net_bridge_port *prev = NULL;
  197. struct net_bridge_port_group *p;
  198. struct hlist_node *rp;
  199. rp = rcu_dereference(hlist_first_rcu(&br->router_list));
  200. p = mdst ? rcu_dereference(mdst->ports) : NULL;
  201. while (p || rp) {
  202. struct net_bridge_port *port, *lport, *rport;
  203. lport = p ? p->port : NULL;
  204. rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
  205. NULL;
  206. port = (unsigned long)lport > (unsigned long)rport ?
  207. lport : rport;
  208. prev = maybe_deliver(prev, port, skb, local_orig);
  209. if (IS_ERR(prev))
  210. goto out;
  211. if (prev == port)
  212. br_multicast_count(port->br, port, skb, igmp_type,
  213. BR_MCAST_DIR_TX);
  214. if ((unsigned long)lport >= (unsigned long)port)
  215. p = rcu_dereference(p->next);
  216. if ((unsigned long)rport >= (unsigned long)port)
  217. rp = rcu_dereference(hlist_next_rcu(rp));
  218. }
  219. if (!prev)
  220. goto out;
  221. if (local_rcv)
  222. deliver_clone(prev, skb, local_orig);
  223. else
  224. __br_forward(prev, skb, local_orig);
  225. return;
  226. out:
  227. if (!local_rcv)
  228. kfree_skb(skb);
  229. }
  230. #endif