br_input.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Handle incoming frames
  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/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. __br_handle_local_finish(skb);
  199. /* return 1 to signal the okfn() was called so it's ok to use the skb */
  200. return 1;
  201. }
  202. /*
  203. * Return NULL if skb is handled
  204. * note: already called with rcu_read_lock
  205. */
  206. rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
  207. {
  208. struct net_bridge_port *p;
  209. struct sk_buff *skb = *pskb;
  210. const unsigned char *dest = eth_hdr(skb)->h_dest;
  211. br_should_route_hook_t *rhook;
  212. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  213. return RX_HANDLER_PASS;
  214. if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
  215. goto drop;
  216. skb = skb_share_check(skb, GFP_ATOMIC);
  217. if (!skb)
  218. return RX_HANDLER_CONSUMED;
  219. p = br_port_get_rcu(skb->dev);
  220. if (unlikely(is_link_local_ether_addr(dest))) {
  221. u16 fwd_mask = p->br->group_fwd_mask_required;
  222. /*
  223. * See IEEE 802.1D Table 7-10 Reserved addresses
  224. *
  225. * Assignment Value
  226. * Bridge Group Address 01-80-C2-00-00-00
  227. * (MAC Control) 802.3 01-80-C2-00-00-01
  228. * (Link Aggregation) 802.3 01-80-C2-00-00-02
  229. * 802.1X PAE address 01-80-C2-00-00-03
  230. *
  231. * 802.1AB LLDP 01-80-C2-00-00-0E
  232. *
  233. * Others reserved for future standardization
  234. */
  235. switch (dest[5]) {
  236. case 0x00: /* Bridge Group Address */
  237. /* If STP is turned off,
  238. then must forward to keep loop detection */
  239. if (p->br->stp_enabled == BR_NO_STP ||
  240. fwd_mask & (1u << dest[5]))
  241. goto forward;
  242. *pskb = skb;
  243. __br_handle_local_finish(skb);
  244. return RX_HANDLER_PASS;
  245. case 0x01: /* IEEE MAC (Pause) */
  246. goto drop;
  247. case 0x0E: /* 802.1AB LLDP */
  248. fwd_mask |= p->br->group_fwd_mask;
  249. if (fwd_mask & (1u << dest[5]))
  250. goto forward;
  251. *pskb = skb;
  252. __br_handle_local_finish(skb);
  253. return RX_HANDLER_PASS;
  254. default:
  255. /* Allow selective forwarding for most other protocols */
  256. fwd_mask |= p->br->group_fwd_mask;
  257. if (fwd_mask & (1u << dest[5]))
  258. goto forward;
  259. }
  260. /* The else clause should be hit when nf_hook():
  261. * - returns < 0 (drop/error)
  262. * - returns = 0 (stolen/nf_queue)
  263. * Thus return 1 from the okfn() to signal the skb is ok to pass
  264. */
  265. if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
  266. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  267. br_handle_local_finish) == 1) {
  268. return RX_HANDLER_PASS;
  269. } else {
  270. return RX_HANDLER_CONSUMED;
  271. }
  272. }
  273. forward:
  274. switch (p->state) {
  275. case BR_STATE_FORWARDING:
  276. rhook = rcu_dereference(br_should_route_hook);
  277. if (rhook) {
  278. if ((*rhook)(skb)) {
  279. *pskb = skb;
  280. return RX_HANDLER_PASS;
  281. }
  282. dest = eth_hdr(skb)->h_dest;
  283. }
  284. /* fall through */
  285. case BR_STATE_LEARNING:
  286. if (ether_addr_equal(p->br->dev->dev_addr, dest))
  287. skb->pkt_type = PACKET_HOST;
  288. NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING,
  289. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  290. br_handle_frame_finish);
  291. break;
  292. default:
  293. drop:
  294. kfree_skb(skb);
  295. }
  296. return RX_HANDLER_CONSUMED;
  297. }