1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Forwarding decision 4 * Linux ethernet bridge 5 * 6 * Authors: 7 * Lennert Buytenhek <buytenh@gnu.org> 8 */ 9 10 #include <linux/err.h> 11 #include <linux/slab.h> 12 #include <linux/kernel.h> 13 #include <linux/netdevice.h> 14 #include <linux/netpoll.h> 15 #include <linux/skbuff.h> 16 #include <linux/if_vlan.h> 17 #include <linux/netfilter_bridge.h> 18 #include "br_private.h" 19 20 /* Don't forward packets to originating port or forwarding disabled */ 21 static inline int should_deliver(const struct net_bridge_port *p, 22 const struct sk_buff *skb) 23 { 24 struct net_bridge_vlan_group *vg; 25 26 vg = nbp_vlan_group_rcu(p); 27 return ((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) && 28 (br_mst_is_enabled(p->br) || p->state == BR_STATE_FORWARDING) && 29 br_allowed_egress(vg, skb) && nbp_switchdev_allowed_egress(p, skb) && 30 !br_skb_isolated(p, skb); 31 } 32 33 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) 34 { 35 skb_push(skb, ETH_HLEN); 36 if (!is_skb_forwardable(skb->dev, skb)) 37 goto drop; 38 39 br_drop_fake_rtable(skb); 40 41 if (skb->ip_summed == CHECKSUM_PARTIAL && 42 eth_type_vlan(skb->protocol)) { 43 int depth; 44 45 if (!vlan_get_protocol_and_depth(skb, skb->protocol, &depth)) 46 goto drop; 47 48 skb_set_network_header(skb, depth); 49 } 50 51 br_switchdev_frame_set_offload_fwd_mark(skb); 52 53 dev_queue_xmit(skb); 54 55 return 0; 56 57 drop: 58 kfree_skb(skb); 59 return 0; 60 } 61 EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit); 62 63 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 64 { 65 skb_clear_tstamp(skb); 66 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, 67 net, sk, skb, NULL, skb->dev, 68 br_dev_queue_push_xmit); 69 70 } 71 EXPORT_SYMBOL_GPL(br_forward_finish); 72 73 static void __br_forward(const struct net_bridge_port *to, 74 struct sk_buff *skb, bool local_orig) 75 { 76 struct net_bridge_vlan_group *vg; 77 struct net_device *indev; 78 struct net *net; 79 int br_hook; 80 81 /* Mark the skb for forwarding offload early so that br_handle_vlan() 82 * can know whether to pop the VLAN header on egress or keep it. 83 */ 84 nbp_switchdev_frame_mark_tx_fwd_offload(to, skb); 85 86 vg = nbp_vlan_group_rcu(to); 87 skb = br_handle_vlan(to->br, to, vg, skb); 88 if (!skb) 89 return; 90 91 indev = skb->dev; 92 skb->dev = to->dev; 93 if (!local_orig) { 94 if (skb_warn_if_lro(skb)) { 95 kfree_skb(skb); 96 return; 97 } 98 br_hook = NF_BR_FORWARD; 99 skb_forward_csum(skb); 100 net = dev_net(indev); 101 } else { 102 if (unlikely(netpoll_tx_running(to->br->dev))) { 103 skb_push(skb, ETH_HLEN); 104 if (!is_skb_forwardable(skb->dev, skb)) 105 kfree_skb(skb); 106 else 107 br_netpoll_send_skb(to, skb); 108 return; 109 } 110 br_hook = NF_BR_LOCAL_OUT; 111 net = dev_net(skb->dev); 112 indev = NULL; 113 } 114 115 NF_HOOK(NFPROTO_BRIDGE, br_hook, 116 net, NULL, skb, indev, skb->dev, 117 br_forward_finish); 118 } 119 120 static int deliver_clone(const struct net_bridge_port *prev, 121 struct sk_buff *skb, bool local_orig) 122 { 123 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev; 124 125 skb = skb_clone(skb, GFP_ATOMIC); 126 if (!skb) { 127 DEV_STATS_INC(dev, tx_dropped); 128 return -ENOMEM; 129 } 130 131 __br_forward(prev, skb, local_orig); 132 return 0; 133 } 134 135 /** 136 * br_forward - forward a packet to a specific port 137 * @to: destination port 138 * @skb: packet being forwarded 139 * @local_rcv: packet will be received locally after forwarding 140 * @local_orig: packet is locally originated 141 * 142 * Should be called with rcu_read_lock. 143 */ 144 void br_forward(const struct net_bridge_port *to, 145 struct sk_buff *skb, bool local_rcv, bool local_orig) 146 { 147 if (unlikely(!to)) 148 goto out; 149 150 /* redirect to backup link if the destination port is down */ 151 if (rcu_access_pointer(to->backup_port) && !netif_carrier_ok(to->dev)) { 152 struct net_bridge_port *backup_port; 153 154 backup_port = rcu_dereference(to->backup_port); 155 if (unlikely(!backup_port)) 156 goto out; 157 BR_INPUT_SKB_CB(skb)->backup_nhid = READ_ONCE(to->backup_nhid); 158 to = backup_port; 159 } 160 161 if (should_deliver(to, skb)) { 162 if (local_rcv) 163 deliver_clone(to, skb, local_orig); 164 else 165 __br_forward(to, skb, local_orig); 166 return; 167 } 168 169 out: 170 if (!local_rcv) 171 kfree_skb(skb); 172 } 173 EXPORT_SYMBOL_GPL(br_forward); 174 175 static struct net_bridge_port *maybe_deliver( 176 struct net_bridge_port *prev, struct net_bridge_port *p, 177 struct sk_buff *skb, bool local_orig) 178 { 179 u8 igmp_type = br_multicast_igmp_type(skb); 180 int err; 181 182 if (!should_deliver(p, skb)) 183 return prev; 184 185 nbp_switchdev_frame_mark_tx_fwd_to_hwdom(p, skb); 186 187 if (!prev) 188 goto out; 189 190 err = deliver_clone(prev, skb, local_orig); 191 if (err) 192 return ERR_PTR(err); 193 out: 194 br_multicast_count(p->br, p, skb, igmp_type, BR_MCAST_DIR_TX); 195 196 return p; 197 } 198 199 /* called under rcu_read_lock */ 200 void br_flood(struct net_bridge *br, struct sk_buff *skb, 201 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig, 202 u16 vid) 203 { 204 struct net_bridge_port *prev = NULL; 205 struct net_bridge_port *p; 206 207 br_tc_skb_miss_set(skb, pkt_type != BR_PKT_BROADCAST); 208 209 list_for_each_entry_rcu(p, &br->port_list, list) { 210 /* Do not flood unicast traffic to ports that turn it off, nor 211 * other traffic if flood off, except for traffic we originate 212 */ 213 switch (pkt_type) { 214 case BR_PKT_UNICAST: 215 if (!(p->flags & BR_FLOOD)) 216 continue; 217 break; 218 case BR_PKT_MULTICAST: 219 if (!(p->flags & BR_MCAST_FLOOD) && skb->dev != br->dev) 220 continue; 221 break; 222 case BR_PKT_BROADCAST: 223 if (!(p->flags & BR_BCAST_FLOOD) && skb->dev != br->dev) 224 continue; 225 break; 226 } 227 228 /* Do not flood to ports that enable proxy ARP */ 229 if (p->flags & BR_PROXYARP) 230 continue; 231 if (BR_INPUT_SKB_CB(skb)->proxyarp_replied && 232 ((p->flags & BR_PROXYARP_WIFI) || 233 br_is_neigh_suppress_enabled(p, vid))) 234 continue; 235 236 prev = maybe_deliver(prev, p, skb, local_orig); 237 if (IS_ERR(prev)) 238 goto out; 239 } 240 241 if (!prev) 242 goto out; 243 244 if (local_rcv) 245 deliver_clone(prev, skb, local_orig); 246 else 247 __br_forward(prev, skb, local_orig); 248 return; 249 250 out: 251 if (!local_rcv) 252 kfree_skb(skb); 253 } 254 255 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 256 static void maybe_deliver_addr(struct net_bridge_port *p, struct sk_buff *skb, 257 const unsigned char *addr, bool local_orig) 258 { 259 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev; 260 const unsigned char *src = eth_hdr(skb)->h_source; 261 struct sk_buff *nskb; 262 263 if (!should_deliver(p, skb)) 264 return; 265 266 /* Even with hairpin, no soliloquies - prevent breaking IPv6 DAD */ 267 if (skb->dev == p->dev && ether_addr_equal(src, addr)) 268 return; 269 270 __skb_push(skb, ETH_HLEN); 271 nskb = pskb_copy(skb, GFP_ATOMIC); 272 __skb_pull(skb, ETH_HLEN); 273 if (!nskb) { 274 DEV_STATS_INC(dev, tx_dropped); 275 return; 276 } 277 278 skb = nskb; 279 __skb_pull(skb, ETH_HLEN); 280 if (!is_broadcast_ether_addr(addr)) 281 memcpy(eth_hdr(skb)->h_dest, addr, ETH_ALEN); 282 283 __br_forward(p, skb, local_orig); 284 } 285 286 /* called with rcu_read_lock */ 287 void br_multicast_flood(struct net_bridge_mdb_entry *mdst, 288 struct sk_buff *skb, 289 struct net_bridge_mcast *brmctx, 290 bool local_rcv, bool local_orig) 291 { 292 struct net_bridge_port *prev = NULL; 293 struct net_bridge_port_group *p; 294 bool allow_mode_include = true; 295 struct hlist_node *rp; 296 297 rp = br_multicast_get_first_rport_node(brmctx, skb); 298 299 if (mdst) { 300 p = rcu_dereference(mdst->ports); 301 if (br_multicast_should_handle_mode(brmctx, mdst->addr.proto) && 302 br_multicast_is_star_g(&mdst->addr)) 303 allow_mode_include = false; 304 } else { 305 p = NULL; 306 br_tc_skb_miss_set(skb, true); 307 } 308 309 while (p || rp) { 310 struct net_bridge_port *port, *lport, *rport; 311 312 lport = p ? p->key.port : NULL; 313 rport = br_multicast_rport_from_node_skb(rp, skb); 314 315 if ((unsigned long)lport > (unsigned long)rport) { 316 port = lport; 317 318 if (port->flags & BR_MULTICAST_TO_UNICAST) { 319 maybe_deliver_addr(lport, skb, p->eth_addr, 320 local_orig); 321 goto delivered; 322 } 323 if ((!allow_mode_include && 324 p->filter_mode == MCAST_INCLUDE) || 325 (p->flags & MDB_PG_FLAGS_BLOCKED)) 326 goto delivered; 327 } else { 328 port = rport; 329 } 330 331 prev = maybe_deliver(prev, port, skb, local_orig); 332 if (IS_ERR(prev)) 333 goto out; 334 delivered: 335 if ((unsigned long)lport >= (unsigned long)port) 336 p = rcu_dereference(p->next); 337 if ((unsigned long)rport >= (unsigned long)port) 338 rp = rcu_dereference(hlist_next_rcu(rp)); 339 } 340 341 if (!prev) 342 goto out; 343 344 if (local_rcv) 345 deliver_clone(prev, skb, local_orig); 346 else 347 __br_forward(prev, skb, local_orig); 348 return; 349 350 out: 351 if (!local_rcv) 352 kfree_skb(skb); 353 } 354 #endif 355