1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Handle incoming frames 4 * Linux ethernet bridge 5 * 6 * Authors: 7 * Lennert Buytenhek <buytenh@gnu.org> 8 */ 9 10 #include <linux/slab.h> 11 #include <linux/kernel.h> 12 #include <linux/netdevice.h> 13 #include <linux/etherdevice.h> 14 #include <linux/netfilter_bridge.h> 15 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE 16 #include <net/netfilter/nf_queue.h> 17 #endif 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 25 static int 26 br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb) 27 { 28 br_drop_fake_rtable(skb); 29 return netif_receive_skb(skb); 30 } 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 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 44 vg = br_vlan_group_rcu(br); 45 /* Bridge is just like any other port. Make sure the 46 * packet is allowed except in promisc modue when someone 47 * may be running packet capture. 48 */ 49 if (!(brdev->flags & IFF_PROMISC) && 50 !br_allowed_egress(vg, skb)) { 51 kfree_skb(skb); 52 return NET_RX_DROP; 53 } 54 55 indev = skb->dev; 56 skb->dev = brdev; 57 skb = br_handle_vlan(br, NULL, vg, skb); 58 if (!skb) 59 return NET_RX_DROP; 60 /* update the multicast stats if the packet is IGMP/MLD */ 61 br_multicast_count(br, NULL, skb, br_multicast_igmp_type(skb), 62 BR_MCAST_DIR_TX); 63 64 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, 65 dev_net(indev), NULL, skb, indev, NULL, 66 br_netif_receive_skb); 67 } 68 69 /* note: already called with rcu_read_lock */ 70 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 71 { 72 struct net_bridge_port *p = br_port_get_rcu(skb->dev); 73 enum br_pkt_type pkt_type = BR_PKT_UNICAST; 74 struct net_bridge_fdb_entry *dst = NULL; 75 struct net_bridge_mdb_entry *mdst; 76 bool local_rcv, mcast_hit = false; 77 const unsigned char *dest; 78 struct net_bridge *br; 79 u16 vid = 0; 80 81 if (!p || p->state == BR_STATE_DISABLED) 82 goto drop; 83 84 if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid)) 85 goto out; 86 87 nbp_switchdev_frame_mark(p, skb); 88 89 /* insert into forwarding database after filtering to avoid spoofing */ 90 br = p->br; 91 if (p->flags & BR_LEARNING) 92 br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, false); 93 94 local_rcv = !!(br->dev->flags & IFF_PROMISC); 95 dest = eth_hdr(skb)->h_dest; 96 if (is_multicast_ether_addr(dest)) { 97 /* by definition the broadcast is also a multicast address */ 98 if (is_broadcast_ether_addr(dest)) { 99 pkt_type = BR_PKT_BROADCAST; 100 local_rcv = true; 101 } else { 102 pkt_type = BR_PKT_MULTICAST; 103 if (br_multicast_rcv(br, p, skb, vid)) 104 goto drop; 105 } 106 } 107 108 if (p->state == BR_STATE_LEARNING) 109 goto drop; 110 111 BR_INPUT_SKB_CB(skb)->brdev = br->dev; 112 BR_INPUT_SKB_CB(skb)->src_port_isolated = !!(p->flags & BR_ISOLATED); 113 114 if (IS_ENABLED(CONFIG_INET) && 115 (skb->protocol == htons(ETH_P_ARP) || 116 skb->protocol == htons(ETH_P_RARP))) { 117 br_do_proxy_suppress_arp(skb, br, vid, p); 118 } else if (IS_ENABLED(CONFIG_IPV6) && 119 skb->protocol == htons(ETH_P_IPV6) && 120 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) && 121 pskb_may_pull(skb, sizeof(struct ipv6hdr) + 122 sizeof(struct nd_msg)) && 123 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) { 124 struct nd_msg *msg, _msg; 125 126 msg = br_is_nd_neigh_msg(skb, &_msg); 127 if (msg) 128 br_do_suppress_nd(skb, br, vid, p, msg); 129 } 130 131 switch (pkt_type) { 132 case BR_PKT_MULTICAST: 133 mdst = br_mdb_get(br, skb, vid); 134 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) && 135 br_multicast_querier_exists(br, eth_hdr(skb))) { 136 if ((mdst && mdst->host_joined) || 137 br_multicast_is_router(br)) { 138 local_rcv = true; 139 br->dev->stats.multicast++; 140 } 141 mcast_hit = true; 142 } else { 143 local_rcv = true; 144 br->dev->stats.multicast++; 145 } 146 break; 147 case BR_PKT_UNICAST: 148 dst = br_fdb_find_rcu(br, dest, vid); 149 default: 150 break; 151 } 152 153 if (dst) { 154 unsigned long now = jiffies; 155 156 if (dst->is_local) 157 return br_pass_frame_up(skb); 158 159 if (now != dst->used) 160 dst->used = now; 161 br_forward(dst->dst, skb, local_rcv, false); 162 } else { 163 if (!mcast_hit) 164 br_flood(br, skb, pkt_type, local_rcv, false); 165 else 166 br_multicast_flood(mdst, skb, local_rcv, false); 167 } 168 169 if (local_rcv) 170 return br_pass_frame_up(skb); 171 172 out: 173 return 0; 174 drop: 175 kfree_skb(skb); 176 goto out; 177 } 178 EXPORT_SYMBOL_GPL(br_handle_frame_finish); 179 180 static void __br_handle_local_finish(struct sk_buff *skb) 181 { 182 struct net_bridge_port *p = br_port_get_rcu(skb->dev); 183 u16 vid = 0; 184 185 /* check if vlan is allowed, to avoid spoofing */ 186 if ((p->flags & BR_LEARNING) && 187 !br_opt_get(p->br, BROPT_NO_LL_LEARN) && 188 br_should_learn(p, skb, &vid)) 189 br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, false); 190 } 191 192 /* note: already called with rcu_read_lock */ 193 static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 194 { 195 __br_handle_local_finish(skb); 196 197 /* return 1 to signal the okfn() was called so it's ok to use the skb */ 198 return 1; 199 } 200 201 static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb) 202 { 203 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE 204 struct nf_hook_entries *e = NULL; 205 struct nf_hook_state state; 206 unsigned int verdict, i; 207 struct net *net; 208 int ret; 209 210 net = dev_net(skb->dev); 211 #ifdef HAVE_JUMP_LABEL 212 if (!static_key_false(&nf_hooks_needed[NFPROTO_BRIDGE][NF_BR_PRE_ROUTING])) 213 goto frame_finish; 214 #endif 215 216 e = rcu_dereference(net->nf.hooks_bridge[NF_BR_PRE_ROUTING]); 217 if (!e) 218 goto frame_finish; 219 220 nf_hook_state_init(&state, NF_BR_PRE_ROUTING, 221 NFPROTO_BRIDGE, skb->dev, NULL, NULL, 222 net, br_handle_frame_finish); 223 224 for (i = 0; i < e->num_hook_entries; i++) { 225 verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state); 226 switch (verdict & NF_VERDICT_MASK) { 227 case NF_ACCEPT: 228 if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) { 229 *pskb = skb; 230 return RX_HANDLER_PASS; 231 } 232 break; 233 case NF_DROP: 234 kfree_skb(skb); 235 return RX_HANDLER_CONSUMED; 236 case NF_QUEUE: 237 ret = nf_queue(skb, &state, e, i, verdict); 238 if (ret == 1) 239 continue; 240 return RX_HANDLER_CONSUMED; 241 default: /* STOLEN */ 242 return RX_HANDLER_CONSUMED; 243 } 244 } 245 frame_finish: 246 net = dev_net(skb->dev); 247 br_handle_frame_finish(net, NULL, skb); 248 #else 249 br_handle_frame_finish(dev_net(skb->dev), NULL, skb); 250 #endif 251 return RX_HANDLER_CONSUMED; 252 } 253 254 /* 255 * Return NULL if skb is handled 256 * note: already called with rcu_read_lock 257 */ 258 rx_handler_result_t br_handle_frame(struct sk_buff **pskb) 259 { 260 struct net_bridge_port *p; 261 struct sk_buff *skb = *pskb; 262 const unsigned char *dest = eth_hdr(skb)->h_dest; 263 264 if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) 265 return RX_HANDLER_PASS; 266 267 if (!is_valid_ether_addr(eth_hdr(skb)->h_source)) 268 goto drop; 269 270 skb = skb_share_check(skb, GFP_ATOMIC); 271 if (!skb) 272 return RX_HANDLER_CONSUMED; 273 274 memset(skb->cb, 0, sizeof(struct br_input_skb_cb)); 275 276 p = br_port_get_rcu(skb->dev); 277 if (p->flags & BR_VLAN_TUNNEL) { 278 if (br_handle_ingress_vlan_tunnel(skb, p, 279 nbp_vlan_group_rcu(p))) 280 goto drop; 281 } 282 283 if (unlikely(is_link_local_ether_addr(dest))) { 284 u16 fwd_mask = p->br->group_fwd_mask_required; 285 286 /* 287 * See IEEE 802.1D Table 7-10 Reserved addresses 288 * 289 * Assignment Value 290 * Bridge Group Address 01-80-C2-00-00-00 291 * (MAC Control) 802.3 01-80-C2-00-00-01 292 * (Link Aggregation) 802.3 01-80-C2-00-00-02 293 * 802.1X PAE address 01-80-C2-00-00-03 294 * 295 * 802.1AB LLDP 01-80-C2-00-00-0E 296 * 297 * Others reserved for future standardization 298 */ 299 fwd_mask |= p->group_fwd_mask; 300 switch (dest[5]) { 301 case 0x00: /* Bridge Group Address */ 302 /* If STP is turned off, 303 then must forward to keep loop detection */ 304 if (p->br->stp_enabled == BR_NO_STP || 305 fwd_mask & (1u << dest[5])) 306 goto forward; 307 *pskb = skb; 308 __br_handle_local_finish(skb); 309 return RX_HANDLER_PASS; 310 311 case 0x01: /* IEEE MAC (Pause) */ 312 goto drop; 313 314 case 0x0E: /* 802.1AB LLDP */ 315 fwd_mask |= p->br->group_fwd_mask; 316 if (fwd_mask & (1u << dest[5])) 317 goto forward; 318 *pskb = skb; 319 __br_handle_local_finish(skb); 320 return RX_HANDLER_PASS; 321 322 default: 323 /* Allow selective forwarding for most other protocols */ 324 fwd_mask |= p->br->group_fwd_mask; 325 if (fwd_mask & (1u << dest[5])) 326 goto forward; 327 } 328 329 /* The else clause should be hit when nf_hook(): 330 * - returns < 0 (drop/error) 331 * - returns = 0 (stolen/nf_queue) 332 * Thus return 1 from the okfn() to signal the skb is ok to pass 333 */ 334 if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, 335 dev_net(skb->dev), NULL, skb, skb->dev, NULL, 336 br_handle_local_finish) == 1) { 337 return RX_HANDLER_PASS; 338 } else { 339 return RX_HANDLER_CONSUMED; 340 } 341 } 342 343 forward: 344 switch (p->state) { 345 case BR_STATE_FORWARDING: 346 case BR_STATE_LEARNING: 347 if (ether_addr_equal(p->br->dev->dev_addr, dest)) 348 skb->pkt_type = PACKET_HOST; 349 350 return nf_hook_bridge_pre(skb, pskb); 351 default: 352 drop: 353 kfree_skb(skb); 354 } 355 return RX_HANDLER_CONSUMED; 356 } 357