br_forward.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. !br_skb_isolated(p, skb);
  32. }
  33. int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
  34. {
  35. skb_push(skb, ETH_HLEN);
  36. if (!is_skb_forwardable(skb->dev, skb))
  37. goto drop;
  38. br_drop_fake_rtable(skb);
  39. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  40. (skb->protocol == htons(ETH_P_8021Q) ||
  41. skb->protocol == htons(ETH_P_8021AD))) {
  42. int depth;
  43. if (!__vlan_get_protocol(skb, skb->protocol, &depth))
  44. goto drop;
  45. skb_set_network_header(skb, depth);
  46. }
  47. dev_queue_xmit(skb);
  48. return 0;
  49. drop:
  50. kfree_skb(skb);
  51. return 0;
  52. }
  53. EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit);
  54. int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  55. {
  56. skb->tstamp = 0;
  57. return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING,
  58. net, sk, skb, NULL, skb->dev,
  59. br_dev_queue_push_xmit);
  60. }
  61. EXPORT_SYMBOL_GPL(br_forward_finish);
  62. static void __br_forward(const struct net_bridge_port *to,
  63. struct sk_buff *skb, bool local_orig)
  64. {
  65. struct net_bridge_vlan_group *vg;
  66. struct net_device *indev;
  67. struct net *net;
  68. int br_hook;
  69. vg = nbp_vlan_group_rcu(to);
  70. skb = br_handle_vlan(to->br, to, vg, skb);
  71. if (!skb)
  72. return;
  73. indev = skb->dev;
  74. skb->dev = to->dev;
  75. if (!local_orig) {
  76. if (skb_warn_if_lro(skb)) {
  77. kfree_skb(skb);
  78. return;
  79. }
  80. br_hook = NF_BR_FORWARD;
  81. skb_forward_csum(skb);
  82. net = dev_net(indev);
  83. } else {
  84. if (unlikely(netpoll_tx_running(to->br->dev))) {
  85. skb_push(skb, ETH_HLEN);
  86. if (!is_skb_forwardable(skb->dev, skb))
  87. kfree_skb(skb);
  88. else
  89. br_netpoll_send_skb(to, skb);
  90. return;
  91. }
  92. br_hook = NF_BR_LOCAL_OUT;
  93. net = dev_net(skb->dev);
  94. indev = NULL;
  95. }
  96. NF_HOOK(NFPROTO_BRIDGE, br_hook,
  97. net, NULL, skb, indev, skb->dev,
  98. br_forward_finish);
  99. }
  100. static int deliver_clone(const struct net_bridge_port *prev,
  101. struct sk_buff *skb, bool local_orig)
  102. {
  103. struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  104. skb = skb_clone(skb, GFP_ATOMIC);
  105. if (!skb) {
  106. dev->stats.tx_dropped++;
  107. return -ENOMEM;
  108. }
  109. __br_forward(prev, skb, local_orig);
  110. return 0;
  111. }
  112. /**
  113. * br_forward - forward a packet to a specific port
  114. * @to: destination port
  115. * @skb: packet being forwarded
  116. * @local_rcv: packet will be received locally after forwarding
  117. * @local_orig: packet is locally originated
  118. *
  119. * Should be called with rcu_read_lock.
  120. */
  121. void br_forward(const struct net_bridge_port *to,
  122. struct sk_buff *skb, bool local_rcv, bool local_orig)
  123. {
  124. if (unlikely(!to))
  125. goto out;
  126. /* redirect to backup link if the destination port is down */
  127. if (rcu_access_pointer(to->backup_port) && !netif_carrier_ok(to->dev)) {
  128. struct net_bridge_port *backup_port;
  129. backup_port = rcu_dereference(to->backup_port);
  130. if (unlikely(!backup_port))
  131. goto out;
  132. to = backup_port;
  133. }
  134. if (should_deliver(to, skb)) {
  135. if (local_rcv)
  136. deliver_clone(to, skb, local_orig);
  137. else
  138. __br_forward(to, skb, local_orig);
  139. return;
  140. }
  141. out:
  142. if (!local_rcv)
  143. kfree_skb(skb);
  144. }
  145. EXPORT_SYMBOL_GPL(br_forward);
  146. static struct net_bridge_port *maybe_deliver(
  147. struct net_bridge_port *prev, struct net_bridge_port *p,
  148. struct sk_buff *skb, bool local_orig)
  149. {
  150. int err;
  151. if (!should_deliver(p, skb))
  152. return prev;
  153. if (!prev)
  154. goto out;
  155. err = deliver_clone(prev, skb, local_orig);
  156. if (err)
  157. return ERR_PTR(err);
  158. out:
  159. return p;
  160. }
  161. /* called under rcu_read_lock */
  162. void br_flood(struct net_bridge *br, struct sk_buff *skb,
  163. enum br_pkt_type pkt_type, bool local_rcv, bool local_orig)
  164. {
  165. u8 igmp_type = br_multicast_igmp_type(skb);
  166. struct net_bridge_port *prev = NULL;
  167. struct net_bridge_port *p;
  168. list_for_each_entry_rcu(p, &br->port_list, list) {
  169. /* Do not flood unicast traffic to ports that turn it off, nor
  170. * other traffic if flood off, except for traffic we originate
  171. */
  172. switch (pkt_type) {
  173. case BR_PKT_UNICAST:
  174. if (!(p->flags & BR_FLOOD))
  175. continue;
  176. break;
  177. case BR_PKT_MULTICAST:
  178. if (!(p->flags & BR_MCAST_FLOOD) && skb->dev != br->dev)
  179. continue;
  180. break;
  181. case BR_PKT_BROADCAST:
  182. if (!(p->flags & BR_BCAST_FLOOD) && skb->dev != br->dev)
  183. continue;
  184. break;
  185. }
  186. /* Do not flood to ports that enable proxy ARP */
  187. if (p->flags & BR_PROXYARP)
  188. continue;
  189. if ((p->flags & (BR_PROXYARP_WIFI | BR_NEIGH_SUPPRESS)) &&
  190. BR_INPUT_SKB_CB(skb)->proxyarp_replied)
  191. continue;
  192. prev = maybe_deliver(prev, p, skb, local_orig);
  193. if (IS_ERR(prev))
  194. goto out;
  195. if (prev == p)
  196. br_multicast_count(p->br, p, skb, igmp_type,
  197. BR_MCAST_DIR_TX);
  198. }
  199. if (!prev)
  200. goto out;
  201. if (local_rcv)
  202. deliver_clone(prev, skb, local_orig);
  203. else
  204. __br_forward(prev, skb, local_orig);
  205. return;
  206. out:
  207. if (!local_rcv)
  208. kfree_skb(skb);
  209. }
  210. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  211. static void maybe_deliver_addr(struct net_bridge_port *p, struct sk_buff *skb,
  212. const unsigned char *addr, bool local_orig)
  213. {
  214. struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  215. const unsigned char *src = eth_hdr(skb)->h_source;
  216. if (!should_deliver(p, skb))
  217. return;
  218. /* Even with hairpin, no soliloquies - prevent breaking IPv6 DAD */
  219. if (skb->dev == p->dev && ether_addr_equal(src, addr))
  220. return;
  221. skb = skb_copy(skb, GFP_ATOMIC);
  222. if (!skb) {
  223. dev->stats.tx_dropped++;
  224. return;
  225. }
  226. if (!is_broadcast_ether_addr(addr))
  227. memcpy(eth_hdr(skb)->h_dest, addr, ETH_ALEN);
  228. __br_forward(p, skb, local_orig);
  229. }
  230. /* called with rcu_read_lock */
  231. void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
  232. struct sk_buff *skb,
  233. bool local_rcv, bool local_orig)
  234. {
  235. struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
  236. u8 igmp_type = br_multicast_igmp_type(skb);
  237. struct net_bridge *br = netdev_priv(dev);
  238. struct net_bridge_port *prev = NULL;
  239. struct net_bridge_port_group *p;
  240. struct hlist_node *rp;
  241. rp = rcu_dereference(hlist_first_rcu(&br->router_list));
  242. p = mdst ? rcu_dereference(mdst->ports) : NULL;
  243. while (p || rp) {
  244. struct net_bridge_port *port, *lport, *rport;
  245. lport = p ? p->port : NULL;
  246. rport = hlist_entry_safe(rp, struct net_bridge_port, rlist);
  247. if ((unsigned long)lport > (unsigned long)rport) {
  248. port = lport;
  249. if (port->flags & BR_MULTICAST_TO_UNICAST) {
  250. maybe_deliver_addr(lport, skb, p->eth_addr,
  251. local_orig);
  252. goto delivered;
  253. }
  254. } else {
  255. port = rport;
  256. }
  257. prev = maybe_deliver(prev, port, skb, local_orig);
  258. delivered:
  259. if (IS_ERR(prev))
  260. goto out;
  261. if (prev == port)
  262. br_multicast_count(port->br, port, skb, igmp_type,
  263. BR_MCAST_DIR_TX);
  264. if ((unsigned long)lport >= (unsigned long)port)
  265. p = rcu_dereference(p->next);
  266. if ((unsigned long)rport >= (unsigned long)port)
  267. rp = rcu_dereference(hlist_next_rcu(rp));
  268. }
  269. if (!prev)
  270. goto out;
  271. if (local_rcv)
  272. deliver_clone(prev, skb, local_orig);
  273. else
  274. __br_forward(prev, skb, local_orig);
  275. return;
  276. out:
  277. if (!local_rcv)
  278. kfree_skb(skb);
  279. }
  280. #endif