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