br_input.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. /* Hook for brouter */
  24. br_should_route_hook_t __rcu *br_should_route_hook __read_mostly;
  25. EXPORT_SYMBOL(br_should_route_hook);
  26. static int
  27. br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb)
  28. {
  29. br_drop_fake_rtable(skb);
  30. return netif_receive_skb(skb);
  31. }
  32. static int br_pass_frame_up(struct sk_buff *skb)
  33. {
  34. struct net_device *indev, *brdev = BR_INPUT_SKB_CB(skb)->brdev;
  35. struct net_bridge *br = netdev_priv(brdev);
  36. struct net_bridge_vlan_group *vg;
  37. struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
  38. u64_stats_update_begin(&brstats->syncp);
  39. brstats->rx_packets++;
  40. brstats->rx_bytes += skb->len;
  41. u64_stats_update_end(&brstats->syncp);
  42. vg = br_vlan_group_rcu(br);
  43. /* Bridge is just like any other port. Make sure the
  44. * packet is allowed except in promisc modue when someone
  45. * may be running packet capture.
  46. */
  47. if (!(brdev->flags & IFF_PROMISC) &&
  48. !br_allowed_egress(vg, skb)) {
  49. kfree_skb(skb);
  50. return NET_RX_DROP;
  51. }
  52. indev = skb->dev;
  53. skb->dev = brdev;
  54. skb = br_handle_vlan(br, vg, skb);
  55. if (!skb)
  56. return NET_RX_DROP;
  57. /* update the multicast stats if the packet is IGMP/MLD */
  58. br_multicast_count(br, NULL, skb, br_multicast_igmp_type(skb),
  59. BR_MCAST_DIR_TX);
  60. return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
  61. dev_net(indev), NULL, skb, indev, NULL,
  62. br_netif_receive_skb);
  63. }
  64. static void br_do_proxy_arp(struct sk_buff *skb, struct net_bridge *br,
  65. u16 vid, struct net_bridge_port *p)
  66. {
  67. struct net_device *dev = br->dev;
  68. struct neighbour *n;
  69. struct arphdr *parp;
  70. u8 *arpptr, *sha;
  71. __be32 sip, tip;
  72. BR_INPUT_SKB_CB(skb)->proxyarp_replied = false;
  73. if ((dev->flags & IFF_NOARP) ||
  74. !pskb_may_pull(skb, arp_hdr_len(dev)))
  75. return;
  76. parp = arp_hdr(skb);
  77. if (parp->ar_pro != htons(ETH_P_IP) ||
  78. parp->ar_op != htons(ARPOP_REQUEST) ||
  79. parp->ar_hln != dev->addr_len ||
  80. parp->ar_pln != 4)
  81. return;
  82. arpptr = (u8 *)parp + sizeof(struct arphdr);
  83. sha = arpptr;
  84. arpptr += dev->addr_len; /* sha */
  85. memcpy(&sip, arpptr, sizeof(sip));
  86. arpptr += sizeof(sip);
  87. arpptr += dev->addr_len; /* tha */
  88. memcpy(&tip, arpptr, sizeof(tip));
  89. if (ipv4_is_loopback(tip) ||
  90. ipv4_is_multicast(tip))
  91. return;
  92. n = neigh_lookup(&arp_tbl, &tip, dev);
  93. if (n) {
  94. struct net_bridge_fdb_entry *f;
  95. if (!(n->nud_state & NUD_VALID)) {
  96. neigh_release(n);
  97. return;
  98. }
  99. f = __br_fdb_get(br, n->ha, vid);
  100. if (f && ((p->flags & BR_PROXYARP) ||
  101. (f->dst && (f->dst->flags & BR_PROXYARP_WIFI)))) {
  102. arp_send(ARPOP_REPLY, ETH_P_ARP, sip, skb->dev, tip,
  103. sha, n->ha, sha);
  104. BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
  105. }
  106. neigh_release(n);
  107. }
  108. }
  109. /* note: already called with rcu_read_lock */
  110. int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  111. {
  112. struct net_bridge_port *p = br_port_get_rcu(skb->dev);
  113. const unsigned char *dest = eth_hdr(skb)->h_dest;
  114. enum br_pkt_type pkt_type = BR_PKT_UNICAST;
  115. struct net_bridge_fdb_entry *dst = NULL;
  116. struct net_bridge_mdb_entry *mdst;
  117. bool local_rcv, mcast_hit = false;
  118. struct net_bridge *br;
  119. u16 vid = 0;
  120. if (!p || p->state == BR_STATE_DISABLED)
  121. goto drop;
  122. if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid))
  123. goto out;
  124. nbp_switchdev_frame_mark(p, skb);
  125. /* insert into forwarding database after filtering to avoid spoofing */
  126. br = p->br;
  127. if (p->flags & BR_LEARNING)
  128. br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, false);
  129. local_rcv = !!(br->dev->flags & IFF_PROMISC);
  130. if (is_multicast_ether_addr(dest)) {
  131. /* by definition the broadcast is also a multicast address */
  132. if (is_broadcast_ether_addr(dest)) {
  133. pkt_type = BR_PKT_BROADCAST;
  134. local_rcv = true;
  135. } else {
  136. pkt_type = BR_PKT_MULTICAST;
  137. if (br_multicast_rcv(br, p, skb, vid))
  138. goto drop;
  139. }
  140. }
  141. if (p->state == BR_STATE_LEARNING)
  142. goto drop;
  143. BR_INPUT_SKB_CB(skb)->brdev = br->dev;
  144. if (IS_ENABLED(CONFIG_INET) && skb->protocol == htons(ETH_P_ARP))
  145. br_do_proxy_arp(skb, br, vid, p);
  146. switch (pkt_type) {
  147. case BR_PKT_MULTICAST:
  148. mdst = br_mdb_get(br, skb, vid);
  149. if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
  150. br_multicast_querier_exists(br, eth_hdr(skb))) {
  151. if ((mdst && mdst->mglist) ||
  152. br_multicast_is_router(br)) {
  153. local_rcv = true;
  154. br->dev->stats.multicast++;
  155. }
  156. mcast_hit = true;
  157. } else {
  158. local_rcv = true;
  159. br->dev->stats.multicast++;
  160. }
  161. break;
  162. case BR_PKT_UNICAST:
  163. dst = __br_fdb_get(br, dest, vid);
  164. default:
  165. break;
  166. }
  167. if (dst) {
  168. if (dst->is_local)
  169. return br_pass_frame_up(skb);
  170. dst->used = jiffies;
  171. br_forward(dst->dst, skb, local_rcv, false);
  172. } else {
  173. if (!mcast_hit)
  174. br_flood(br, skb, pkt_type, local_rcv, false);
  175. else
  176. br_multicast_flood(mdst, skb, local_rcv, false);
  177. }
  178. if (local_rcv)
  179. return br_pass_frame_up(skb);
  180. out:
  181. return 0;
  182. drop:
  183. kfree_skb(skb);
  184. goto out;
  185. }
  186. EXPORT_SYMBOL_GPL(br_handle_frame_finish);
  187. static void __br_handle_local_finish(struct sk_buff *skb)
  188. {
  189. struct net_bridge_port *p = br_port_get_rcu(skb->dev);
  190. u16 vid = 0;
  191. /* check if vlan is allowed, to avoid spoofing */
  192. if (p->flags & BR_LEARNING && br_should_learn(p, skb, &vid))
  193. br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, false);
  194. }
  195. /* note: already called with rcu_read_lock */
  196. static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  197. {
  198. struct net_bridge_port *p = br_port_get_rcu(skb->dev);
  199. __br_handle_local_finish(skb);
  200. BR_INPUT_SKB_CB(skb)->brdev = p->br->dev;
  201. br_pass_frame_up(skb);
  202. return 0;
  203. }
  204. /*
  205. * Return NULL if skb is handled
  206. * note: already called with rcu_read_lock
  207. */
  208. rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
  209. {
  210. struct net_bridge_port *p;
  211. struct sk_buff *skb = *pskb;
  212. const unsigned char *dest = eth_hdr(skb)->h_dest;
  213. br_should_route_hook_t *rhook;
  214. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  215. return RX_HANDLER_PASS;
  216. if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
  217. goto drop;
  218. skb = skb_share_check(skb, GFP_ATOMIC);
  219. if (!skb)
  220. return RX_HANDLER_CONSUMED;
  221. p = br_port_get_rcu(skb->dev);
  222. if (unlikely(is_link_local_ether_addr(dest))) {
  223. u16 fwd_mask = p->br->group_fwd_mask_required;
  224. /*
  225. * See IEEE 802.1D Table 7-10 Reserved addresses
  226. *
  227. * Assignment Value
  228. * Bridge Group Address 01-80-C2-00-00-00
  229. * (MAC Control) 802.3 01-80-C2-00-00-01
  230. * (Link Aggregation) 802.3 01-80-C2-00-00-02
  231. * 802.1X PAE address 01-80-C2-00-00-03
  232. *
  233. * 802.1AB LLDP 01-80-C2-00-00-0E
  234. *
  235. * Others reserved for future standardization
  236. */
  237. switch (dest[5]) {
  238. case 0x00: /* Bridge Group Address */
  239. /* If STP is turned off,
  240. then must forward to keep loop detection */
  241. if (p->br->stp_enabled == BR_NO_STP ||
  242. fwd_mask & (1u << dest[5]))
  243. goto forward;
  244. *pskb = skb;
  245. __br_handle_local_finish(skb);
  246. return RX_HANDLER_PASS;
  247. case 0x01: /* IEEE MAC (Pause) */
  248. goto drop;
  249. case 0x0E: /* 802.1AB LLDP */
  250. fwd_mask |= p->br->group_fwd_mask;
  251. if (fwd_mask & (1u << dest[5]))
  252. goto forward;
  253. *pskb = skb;
  254. __br_handle_local_finish(skb);
  255. return RX_HANDLER_PASS;
  256. default:
  257. /* Allow selective forwarding for most other protocols */
  258. fwd_mask |= p->br->group_fwd_mask;
  259. if (fwd_mask & (1u << dest[5]))
  260. goto forward;
  261. }
  262. /* Deliver packet to local host only */
  263. NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, dev_net(skb->dev),
  264. NULL, skb, skb->dev, NULL, br_handle_local_finish);
  265. return RX_HANDLER_CONSUMED;
  266. }
  267. forward:
  268. switch (p->state) {
  269. case BR_STATE_FORWARDING:
  270. rhook = rcu_dereference(br_should_route_hook);
  271. if (rhook) {
  272. if ((*rhook)(skb)) {
  273. *pskb = skb;
  274. return RX_HANDLER_PASS;
  275. }
  276. dest = eth_hdr(skb)->h_dest;
  277. }
  278. /* fall through */
  279. case BR_STATE_LEARNING:
  280. if (ether_addr_equal(p->br->dev->dev_addr, dest))
  281. skb->pkt_type = PACKET_HOST;
  282. NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING,
  283. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  284. br_handle_frame_finish);
  285. break;
  286. default:
  287. drop:
  288. kfree_skb(skb);
  289. }
  290. return RX_HANDLER_CONSUMED;
  291. }