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 <net/dsa.h> 21 #include <linux/export.h> 22 #include <linux/rculist.h> 23 #include "br_private.h" 24 #include "br_private_tunnel.h" 25 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 33 static int br_pass_frame_up(struct sk_buff *skb) 34 { 35 struct net_device *indev, *brdev = BR_INPUT_SKB_CB(skb)->brdev; 36 struct net_bridge *br = netdev_priv(brdev); 37 struct net_bridge_vlan_group *vg; 38 39 dev_sw_netstats_rx_add(brdev, skb->len); 40 41 vg = br_vlan_group_rcu(br); 42 /* Bridge is just like any other port. Make sure the 43 * packet is allowed except in promisc mode when someone 44 * may be running packet capture. 45 */ 46 if (!(brdev->flags & IFF_PROMISC) && 47 !br_allowed_egress(vg, skb)) { 48 kfree_skb(skb); 49 return NET_RX_DROP; 50 } 51 52 indev = skb->dev; 53 skb->dev = brdev; 54 skb = br_handle_vlan(br, NULL, 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 61 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, 62 dev_net(indev), NULL, skb, indev, NULL, 63 br_netif_receive_skb); 64 } 65 66 /* note: already called with rcu_read_lock */ 67 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 68 { 69 struct net_bridge_port *p = br_port_get_rcu(skb->dev); 70 enum br_pkt_type pkt_type = BR_PKT_UNICAST; 71 struct net_bridge_fdb_entry *dst = NULL; 72 struct net_bridge_mdb_entry *mdst; 73 bool local_rcv, mcast_hit = false; 74 struct net_bridge *br; 75 u16 vid = 0; 76 u8 state; 77 78 if (!p || p->state == BR_STATE_DISABLED) 79 goto drop; 80 81 state = p->state; 82 if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid, 83 &state)) 84 goto out; 85 86 nbp_switchdev_frame_mark(p, skb); 87 88 /* insert into forwarding database after filtering to avoid spoofing */ 89 br = p->br; 90 if (p->flags & BR_LEARNING) 91 br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, 0); 92 93 local_rcv = !!(br->dev->flags & IFF_PROMISC); 94 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) { 95 /* by definition the broadcast is also a multicast address */ 96 if (is_broadcast_ether_addr(eth_hdr(skb)->h_dest)) { 97 pkt_type = BR_PKT_BROADCAST; 98 local_rcv = true; 99 } else { 100 pkt_type = BR_PKT_MULTICAST; 101 if (br_multicast_rcv(br, p, skb, vid)) 102 goto drop; 103 } 104 } 105 106 if (state == BR_STATE_LEARNING) 107 goto drop; 108 109 BR_INPUT_SKB_CB(skb)->brdev = br->dev; 110 BR_INPUT_SKB_CB(skb)->src_port_isolated = !!(p->flags & BR_ISOLATED); 111 112 if (IS_ENABLED(CONFIG_INET) && 113 (skb->protocol == htons(ETH_P_ARP) || 114 skb->protocol == htons(ETH_P_RARP))) { 115 br_do_proxy_suppress_arp(skb, br, vid, p); 116 } else if (IS_ENABLED(CONFIG_IPV6) && 117 skb->protocol == htons(ETH_P_IPV6) && 118 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) && 119 pskb_may_pull(skb, sizeof(struct ipv6hdr) + 120 sizeof(struct nd_msg)) && 121 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) { 122 struct nd_msg *msg, _msg; 123 124 msg = br_is_nd_neigh_msg(skb, &_msg); 125 if (msg) 126 br_do_suppress_nd(skb, br, vid, p, msg); 127 } 128 129 switch (pkt_type) { 130 case BR_PKT_MULTICAST: 131 mdst = br_mdb_get(br, skb, vid); 132 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) && 133 br_multicast_querier_exists(br, eth_hdr(skb), mdst)) { 134 if ((mdst && mdst->host_joined) || 135 br_multicast_is_router(br)) { 136 local_rcv = true; 137 br->dev->stats.multicast++; 138 } 139 mcast_hit = true; 140 } else { 141 local_rcv = true; 142 br->dev->stats.multicast++; 143 } 144 break; 145 case BR_PKT_UNICAST: 146 dst = br_fdb_find_rcu(br, eth_hdr(skb)->h_dest, vid); 147 break; 148 default: 149 break; 150 } 151 152 if (dst) { 153 unsigned long now = jiffies; 154 155 if (test_bit(BR_FDB_LOCAL, &dst->flags)) 156 return br_pass_frame_up(skb); 157 158 if (now != dst->used) 159 dst->used = now; 160 br_forward(dst->dst, skb, local_rcv, false); 161 } else { 162 if (!mcast_hit) 163 br_flood(br, skb, pkt_type, local_rcv, false); 164 else 165 br_multicast_flood(mdst, skb, local_rcv, false); 166 } 167 168 if (local_rcv) 169 return br_pass_frame_up(skb); 170 171 out: 172 return 0; 173 drop: 174 kfree_skb(skb); 175 goto out; 176 } 177 EXPORT_SYMBOL_GPL(br_handle_frame_finish); 178 179 static void __br_handle_local_finish(struct sk_buff *skb) 180 { 181 struct net_bridge_port *p = br_port_get_rcu(skb->dev); 182 u16 vid = 0; 183 184 /* check if vlan is allowed, to avoid spoofing */ 185 if ((p->flags & BR_LEARNING) && 186 nbp_state_should_learn(p) && 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, 0); 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, 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 /* Return 0 if the frame was not processed otherwise 1 255 * note: already called with rcu_read_lock 256 */ 257 static int br_process_frame_type(struct net_bridge_port *p, 258 struct sk_buff *skb) 259 { 260 struct br_frame_type *tmp; 261 262 hlist_for_each_entry_rcu(tmp, &p->br->frame_type_list, list) 263 if (unlikely(tmp->type == skb->protocol)) 264 return tmp->frame_handler(p, skb); 265 266 return 0; 267 } 268 269 /* 270 * Return NULL if skb is handled 271 * note: already called with rcu_read_lock 272 */ 273 static rx_handler_result_t br_handle_frame(struct sk_buff **pskb) 274 { 275 struct net_bridge_port *p; 276 struct sk_buff *skb = *pskb; 277 const unsigned char *dest = eth_hdr(skb)->h_dest; 278 279 if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) 280 return RX_HANDLER_PASS; 281 282 if (!is_valid_ether_addr(eth_hdr(skb)->h_source)) 283 goto drop; 284 285 skb = skb_share_check(skb, GFP_ATOMIC); 286 if (!skb) 287 return RX_HANDLER_CONSUMED; 288 289 memset(skb->cb, 0, sizeof(struct br_input_skb_cb)); 290 291 p = br_port_get_rcu(skb->dev); 292 if (p->flags & BR_VLAN_TUNNEL) { 293 if (br_handle_ingress_vlan_tunnel(skb, p, 294 nbp_vlan_group_rcu(p))) 295 goto drop; 296 } 297 298 if (unlikely(is_link_local_ether_addr(dest))) { 299 u16 fwd_mask = p->br->group_fwd_mask_required; 300 301 /* 302 * See IEEE 802.1D Table 7-10 Reserved addresses 303 * 304 * Assignment Value 305 * Bridge Group Address 01-80-C2-00-00-00 306 * (MAC Control) 802.3 01-80-C2-00-00-01 307 * (Link Aggregation) 802.3 01-80-C2-00-00-02 308 * 802.1X PAE address 01-80-C2-00-00-03 309 * 310 * 802.1AB LLDP 01-80-C2-00-00-0E 311 * 312 * Others reserved for future standardization 313 */ 314 fwd_mask |= p->group_fwd_mask; 315 switch (dest[5]) { 316 case 0x00: /* Bridge Group Address */ 317 /* If STP is turned off, 318 then must forward to keep loop detection */ 319 if (p->br->stp_enabled == BR_NO_STP || 320 fwd_mask & (1u << dest[5])) 321 goto forward; 322 *pskb = skb; 323 __br_handle_local_finish(skb); 324 return RX_HANDLER_PASS; 325 326 case 0x01: /* IEEE MAC (Pause) */ 327 goto drop; 328 329 case 0x0E: /* 802.1AB LLDP */ 330 fwd_mask |= p->br->group_fwd_mask; 331 if (fwd_mask & (1u << dest[5])) 332 goto forward; 333 *pskb = skb; 334 __br_handle_local_finish(skb); 335 return RX_HANDLER_PASS; 336 337 default: 338 /* Allow selective forwarding for most other protocols */ 339 fwd_mask |= p->br->group_fwd_mask; 340 if (fwd_mask & (1u << dest[5])) 341 goto forward; 342 } 343 344 /* The else clause should be hit when nf_hook(): 345 * - returns < 0 (drop/error) 346 * - returns = 0 (stolen/nf_queue) 347 * Thus return 1 from the okfn() to signal the skb is ok to pass 348 */ 349 if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, 350 dev_net(skb->dev), NULL, skb, skb->dev, NULL, 351 br_handle_local_finish) == 1) { 352 return RX_HANDLER_PASS; 353 } else { 354 return RX_HANDLER_CONSUMED; 355 } 356 } 357 358 if (unlikely(br_process_frame_type(p, skb))) 359 return RX_HANDLER_PASS; 360 361 forward: 362 switch (p->state) { 363 case BR_STATE_FORWARDING: 364 case BR_STATE_LEARNING: 365 if (ether_addr_equal(p->br->dev->dev_addr, dest)) 366 skb->pkt_type = PACKET_HOST; 367 368 return nf_hook_bridge_pre(skb, pskb); 369 default: 370 drop: 371 kfree_skb(skb); 372 } 373 return RX_HANDLER_CONSUMED; 374 } 375 376 /* This function has no purpose other than to appease the br_port_get_rcu/rtnl 377 * helpers which identify bridged ports according to the rx_handler installed 378 * on them (so there _needs_ to be a bridge rx_handler even if we don't need it 379 * to do anything useful). This bridge won't support traffic to/from the stack, 380 * but only hardware bridging. So return RX_HANDLER_PASS so we don't steal 381 * frames from the ETH_P_XDSA packet_type handler. 382 */ 383 static rx_handler_result_t br_handle_frame_dummy(struct sk_buff **pskb) 384 { 385 return RX_HANDLER_PASS; 386 } 387 388 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev) 389 { 390 if (netdev_uses_dsa(dev)) 391 return br_handle_frame_dummy; 392 393 return br_handle_frame; 394 } 395 396 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft) 397 { 398 hlist_add_head_rcu(&ft->list, &br->frame_type_list); 399 } 400 401 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft) 402 { 403 struct br_frame_type *tmp; 404 405 hlist_for_each_entry(tmp, &br->frame_type_list, list) 406 if (ft == tmp) { 407 hlist_del_rcu(&ft->list); 408 return; 409 } 410 } 411