br_input.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
  58. dev_net(indev), NULL, skb, indev, NULL,
  59. br_netif_receive_skb);
  60. }
  61. static void br_do_proxy_arp(struct sk_buff *skb, struct net_bridge *br,
  62. u16 vid, struct net_bridge_port *p)
  63. {
  64. struct net_device *dev = br->dev;
  65. struct neighbour *n;
  66. struct arphdr *parp;
  67. u8 *arpptr, *sha;
  68. __be32 sip, tip;
  69. BR_INPUT_SKB_CB(skb)->proxyarp_replied = false;
  70. if (dev->flags & IFF_NOARP)
  71. return;
  72. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  73. dev->stats.tx_dropped++;
  74. return;
  75. }
  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. const unsigned char *dest = eth_hdr(skb)->h_dest;
  113. struct net_bridge_port *p = br_port_get_rcu(skb->dev);
  114. struct net_bridge *br;
  115. struct net_bridge_fdb_entry *dst;
  116. struct net_bridge_mdb_entry *mdst;
  117. struct sk_buff *skb2;
  118. bool unicast = true;
  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. /* insert into forwarding database after filtering to avoid spoofing */
  125. br = p->br;
  126. if (p->flags & BR_LEARNING)
  127. br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, false);
  128. if (!is_broadcast_ether_addr(dest) && is_multicast_ether_addr(dest) &&
  129. br_multicast_rcv(br, p, skb, vid))
  130. goto drop;
  131. if (p->state == BR_STATE_LEARNING)
  132. goto drop;
  133. BR_INPUT_SKB_CB(skb)->brdev = br->dev;
  134. /* The packet skb2 goes to the local host (NULL to skip). */
  135. skb2 = NULL;
  136. if (br->dev->flags & IFF_PROMISC)
  137. skb2 = skb;
  138. dst = NULL;
  139. if (IS_ENABLED(CONFIG_INET) && skb->protocol == htons(ETH_P_ARP))
  140. br_do_proxy_arp(skb, br, vid, p);
  141. if (is_broadcast_ether_addr(dest)) {
  142. skb2 = skb;
  143. unicast = false;
  144. } else if (is_multicast_ether_addr(dest)) {
  145. mdst = br_mdb_get(br, skb, vid);
  146. if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
  147. br_multicast_querier_exists(br, eth_hdr(skb))) {
  148. if ((mdst && mdst->mglist) ||
  149. br_multicast_is_router(br))
  150. skb2 = skb;
  151. br_multicast_forward(mdst, skb, skb2);
  152. skb = NULL;
  153. if (!skb2)
  154. goto out;
  155. } else
  156. skb2 = skb;
  157. unicast = false;
  158. br->dev->stats.multicast++;
  159. } else if ((dst = __br_fdb_get(br, dest, vid)) &&
  160. dst->is_local) {
  161. skb2 = skb;
  162. /* Do not forward the packet since it's local. */
  163. skb = NULL;
  164. }
  165. if (skb) {
  166. if (dst) {
  167. dst->used = jiffies;
  168. br_forward(dst->dst, skb, skb2);
  169. } else
  170. br_flood_forward(br, skb, skb2, unicast);
  171. }
  172. if (skb2)
  173. return br_pass_frame_up(skb2);
  174. out:
  175. return 0;
  176. drop:
  177. kfree_skb(skb);
  178. goto out;
  179. }
  180. EXPORT_SYMBOL_GPL(br_handle_frame_finish);
  181. /* note: already called with rcu_read_lock */
  182. static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  183. {
  184. struct net_bridge_port *p = br_port_get_rcu(skb->dev);
  185. u16 vid = 0;
  186. /* check if vlan is allowed, to avoid spoofing */
  187. if (p->flags & BR_LEARNING && br_should_learn(p, skb, &vid))
  188. br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, false);
  189. return 0; /* process further */
  190. }
  191. /*
  192. * Return NULL if skb is handled
  193. * note: already called with rcu_read_lock
  194. */
  195. rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
  196. {
  197. struct net_bridge_port *p;
  198. struct sk_buff *skb = *pskb;
  199. const unsigned char *dest = eth_hdr(skb)->h_dest;
  200. br_should_route_hook_t *rhook;
  201. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  202. return RX_HANDLER_PASS;
  203. if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
  204. goto drop;
  205. skb = skb_share_check(skb, GFP_ATOMIC);
  206. if (!skb)
  207. return RX_HANDLER_CONSUMED;
  208. p = br_port_get_rcu(skb->dev);
  209. if (unlikely(is_link_local_ether_addr(dest))) {
  210. u16 fwd_mask = p->br->group_fwd_mask_required;
  211. /*
  212. * See IEEE 802.1D Table 7-10 Reserved addresses
  213. *
  214. * Assignment Value
  215. * Bridge Group Address 01-80-C2-00-00-00
  216. * (MAC Control) 802.3 01-80-C2-00-00-01
  217. * (Link Aggregation) 802.3 01-80-C2-00-00-02
  218. * 802.1X PAE address 01-80-C2-00-00-03
  219. *
  220. * 802.1AB LLDP 01-80-C2-00-00-0E
  221. *
  222. * Others reserved for future standardization
  223. */
  224. switch (dest[5]) {
  225. case 0x00: /* Bridge Group Address */
  226. /* If STP is turned off,
  227. then must forward to keep loop detection */
  228. if (p->br->stp_enabled == BR_NO_STP ||
  229. fwd_mask & (1u << dest[5]))
  230. goto forward;
  231. break;
  232. case 0x01: /* IEEE MAC (Pause) */
  233. goto drop;
  234. default:
  235. /* Allow selective forwarding for most other protocols */
  236. fwd_mask |= p->br->group_fwd_mask;
  237. if (fwd_mask & (1u << dest[5]))
  238. goto forward;
  239. }
  240. /* Deliver packet to local host only */
  241. if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
  242. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  243. br_handle_local_finish)) {
  244. return RX_HANDLER_CONSUMED; /* consumed by filter */
  245. } else {
  246. *pskb = skb;
  247. return RX_HANDLER_PASS; /* continue processing */
  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. }