br_input.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Handle incoming frames
  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/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/netfilter_bridge.h>
  18. #include <linux/neighbour.h>
  19. #include <net/arp.h>
  20. #include <linux/export.h>
  21. #include <linux/rculist.h>
  22. #include "br_private.h"
  23. #include "br_private_tunnel.h"
  24. /* Hook for brouter */
  25. br_should_route_hook_t __rcu *br_should_route_hook __read_mostly;
  26. EXPORT_SYMBOL(br_should_route_hook);
  27. static int
  28. br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb)
  29. {
  30. br_drop_fake_rtable(skb);
  31. return netif_receive_skb(skb);
  32. }
  33. static int br_pass_frame_up(struct sk_buff *skb)
  34. {
  35. struct net_device *indev, *brdev = BR_INPUT_SKB_CB(skb)->brdev;
  36. struct net_bridge *br = netdev_priv(brdev);
  37. struct net_bridge_vlan_group *vg;
  38. struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
  39. u64_stats_update_begin(&brstats->syncp);
  40. brstats->rx_packets++;
  41. brstats->rx_bytes += skb->len;
  42. u64_stats_update_end(&brstats->syncp);
  43. vg = br_vlan_group_rcu(br);
  44. /* Bridge is just like any other port. Make sure the
  45. * packet is allowed except in promisc modue when someone
  46. * may be running packet capture.
  47. */
  48. if (!(brdev->flags & IFF_PROMISC) &&
  49. !br_allowed_egress(vg, skb)) {
  50. kfree_skb(skb);
  51. return NET_RX_DROP;
  52. }
  53. indev = skb->dev;
  54. skb->dev = brdev;
  55. skb = br_handle_vlan(br, NULL, vg, skb);
  56. if (!skb)
  57. return NET_RX_DROP;
  58. /* update the multicast stats if the packet is IGMP/MLD */
  59. br_multicast_count(br, NULL, skb, br_multicast_igmp_type(skb),
  60. BR_MCAST_DIR_TX);
  61. return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
  62. dev_net(indev), NULL, skb, indev, NULL,
  63. br_netif_receive_skb);
  64. }
  65. /* note: already called with rcu_read_lock */
  66. int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  67. {
  68. struct net_bridge_port *p = br_port_get_rcu(skb->dev);
  69. enum br_pkt_type pkt_type = BR_PKT_UNICAST;
  70. struct net_bridge_fdb_entry *dst = NULL;
  71. struct net_bridge_mdb_entry *mdst;
  72. bool local_rcv, mcast_hit = false;
  73. struct net_bridge *br;
  74. u16 vid = 0;
  75. if (!p || p->state == BR_STATE_DISABLED)
  76. goto drop;
  77. if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid))
  78. goto out;
  79. nbp_switchdev_frame_mark(p, skb);
  80. /* insert into forwarding database after filtering to avoid spoofing */
  81. br = p->br;
  82. if (p->flags & BR_LEARNING)
  83. br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, false);
  84. local_rcv = !!(br->dev->flags & IFF_PROMISC);
  85. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) {
  86. /* by definition the broadcast is also a multicast address */
  87. if (is_broadcast_ether_addr(eth_hdr(skb)->h_dest)) {
  88. pkt_type = BR_PKT_BROADCAST;
  89. local_rcv = true;
  90. } else {
  91. pkt_type = BR_PKT_MULTICAST;
  92. if (br_multicast_rcv(br, p, skb, vid))
  93. goto drop;
  94. }
  95. }
  96. if (p->state == BR_STATE_LEARNING)
  97. goto drop;
  98. BR_INPUT_SKB_CB(skb)->brdev = br->dev;
  99. BR_INPUT_SKB_CB(skb)->src_port_isolated = !!(p->flags & BR_ISOLATED);
  100. if (IS_ENABLED(CONFIG_INET) &&
  101. (skb->protocol == htons(ETH_P_ARP) ||
  102. skb->protocol == htons(ETH_P_RARP))) {
  103. br_do_proxy_suppress_arp(skb, br, vid, p);
  104. } else if (IS_ENABLED(CONFIG_IPV6) &&
  105. skb->protocol == htons(ETH_P_IPV6) &&
  106. br->neigh_suppress_enabled &&
  107. pskb_may_pull(skb, sizeof(struct ipv6hdr) +
  108. sizeof(struct nd_msg)) &&
  109. ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
  110. struct nd_msg *msg, _msg;
  111. msg = br_is_nd_neigh_msg(skb, &_msg);
  112. if (msg)
  113. br_do_suppress_nd(skb, br, vid, p, msg);
  114. }
  115. switch (pkt_type) {
  116. case BR_PKT_MULTICAST:
  117. mdst = br_mdb_get(br, skb, vid);
  118. if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
  119. br_multicast_querier_exists(br, eth_hdr(skb))) {
  120. if ((mdst && mdst->host_joined) ||
  121. br_multicast_is_router(br)) {
  122. local_rcv = true;
  123. br->dev->stats.multicast++;
  124. }
  125. mcast_hit = true;
  126. } else {
  127. local_rcv = true;
  128. br->dev->stats.multicast++;
  129. }
  130. break;
  131. case BR_PKT_UNICAST:
  132. dst = br_fdb_find_rcu(br, eth_hdr(skb)->h_dest, vid);
  133. default:
  134. break;
  135. }
  136. if (dst) {
  137. unsigned long now = jiffies;
  138. if (dst->is_local)
  139. return br_pass_frame_up(skb);
  140. if (now != dst->used)
  141. dst->used = now;
  142. br_forward(dst->dst, skb, local_rcv, false);
  143. } else {
  144. if (!mcast_hit)
  145. br_flood(br, skb, pkt_type, local_rcv, false);
  146. else
  147. br_multicast_flood(mdst, skb, local_rcv, false);
  148. }
  149. if (local_rcv)
  150. return br_pass_frame_up(skb);
  151. out:
  152. return 0;
  153. drop:
  154. kfree_skb(skb);
  155. goto out;
  156. }
  157. EXPORT_SYMBOL_GPL(br_handle_frame_finish);
  158. static void __br_handle_local_finish(struct sk_buff *skb)
  159. {
  160. struct net_bridge_port *p = br_port_get_rcu(skb->dev);
  161. u16 vid = 0;
  162. /* check if vlan is allowed, to avoid spoofing */
  163. if (p->flags & BR_LEARNING && br_should_learn(p, skb, &vid))
  164. br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, false);
  165. }
  166. /* note: already called with rcu_read_lock */
  167. static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  168. {
  169. __br_handle_local_finish(skb);
  170. /* return 1 to signal the okfn() was called so it's ok to use the skb */
  171. return 1;
  172. }
  173. /*
  174. * Return NULL if skb is handled
  175. * note: already called with rcu_read_lock
  176. */
  177. rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
  178. {
  179. struct net_bridge_port *p;
  180. struct sk_buff *skb = *pskb;
  181. const unsigned char *dest = eth_hdr(skb)->h_dest;
  182. br_should_route_hook_t *rhook;
  183. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  184. return RX_HANDLER_PASS;
  185. if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
  186. goto drop;
  187. skb = skb_share_check(skb, GFP_ATOMIC);
  188. if (!skb)
  189. return RX_HANDLER_CONSUMED;
  190. p = br_port_get_rcu(skb->dev);
  191. if (p->flags & BR_VLAN_TUNNEL) {
  192. if (br_handle_ingress_vlan_tunnel(skb, p,
  193. nbp_vlan_group_rcu(p)))
  194. goto drop;
  195. }
  196. if (unlikely(is_link_local_ether_addr(dest))) {
  197. u16 fwd_mask = p->br->group_fwd_mask_required;
  198. /*
  199. * See IEEE 802.1D Table 7-10 Reserved addresses
  200. *
  201. * Assignment Value
  202. * Bridge Group Address 01-80-C2-00-00-00
  203. * (MAC Control) 802.3 01-80-C2-00-00-01
  204. * (Link Aggregation) 802.3 01-80-C2-00-00-02
  205. * 802.1X PAE address 01-80-C2-00-00-03
  206. *
  207. * 802.1AB LLDP 01-80-C2-00-00-0E
  208. *
  209. * Others reserved for future standardization
  210. */
  211. fwd_mask |= p->group_fwd_mask;
  212. switch (dest[5]) {
  213. case 0x00: /* Bridge Group Address */
  214. /* If STP is turned off,
  215. then must forward to keep loop detection */
  216. if (p->br->stp_enabled == BR_NO_STP ||
  217. fwd_mask & (1u << dest[5]))
  218. goto forward;
  219. *pskb = skb;
  220. __br_handle_local_finish(skb);
  221. return RX_HANDLER_PASS;
  222. case 0x01: /* IEEE MAC (Pause) */
  223. goto drop;
  224. case 0x0E: /* 802.1AB LLDP */
  225. fwd_mask |= p->br->group_fwd_mask;
  226. if (fwd_mask & (1u << dest[5]))
  227. goto forward;
  228. *pskb = skb;
  229. __br_handle_local_finish(skb);
  230. return RX_HANDLER_PASS;
  231. default:
  232. /* Allow selective forwarding for most other protocols */
  233. fwd_mask |= p->br->group_fwd_mask;
  234. if (fwd_mask & (1u << dest[5]))
  235. goto forward;
  236. }
  237. /* The else clause should be hit when nf_hook():
  238. * - returns < 0 (drop/error)
  239. * - returns = 0 (stolen/nf_queue)
  240. * Thus return 1 from the okfn() to signal the skb is ok to pass
  241. */
  242. if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
  243. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  244. br_handle_local_finish) == 1) {
  245. return RX_HANDLER_PASS;
  246. } else {
  247. return RX_HANDLER_CONSUMED;
  248. }
  249. }
  250. forward:
  251. switch (p->state) {
  252. case BR_STATE_FORWARDING:
  253. rhook = rcu_dereference(br_should_route_hook);
  254. if (rhook) {
  255. if ((*rhook)(skb)) {
  256. *pskb = skb;
  257. return RX_HANDLER_PASS;
  258. }
  259. dest = eth_hdr(skb)->h_dest;
  260. }
  261. /* fall through */
  262. case BR_STATE_LEARNING:
  263. if (ether_addr_equal(p->br->dev->dev_addr, dest))
  264. skb->pkt_type = PACKET_HOST;
  265. NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING,
  266. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  267. br_handle_frame_finish);
  268. break;
  269. default:
  270. drop:
  271. kfree_skb(skb);
  272. }
  273. return RX_HANDLER_CONSUMED;
  274. }