root/net/bridge/br_input.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. br_netif_receive_skb
  2. br_pass_frame_up
  3. br_handle_frame_finish
  4. __br_handle_local_finish
  5. br_handle_local_finish
  6. nf_hook_bridge_pre
  7. br_handle_frame

   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         struct net_bridge *br;
  78         u16 vid = 0;
  79 
  80         if (!p || p->state == BR_STATE_DISABLED)
  81                 goto drop;
  82 
  83         if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid))
  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, false);
  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 (p->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))) {
 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         default:
 148                 break;
 149         }
 150 
 151         if (dst) {
 152                 unsigned long now = jiffies;
 153 
 154                 if (dst->is_local)
 155                         return br_pass_frame_up(skb);
 156 
 157                 if (now != dst->used)
 158                         dst->used = now;
 159                 br_forward(dst->dst, skb, local_rcv, false);
 160         } else {
 161                 if (!mcast_hit)
 162                         br_flood(br, skb, pkt_type, local_rcv, false);
 163                 else
 164                         br_multicast_flood(mdst, skb, local_rcv, false);
 165         }
 166 
 167         if (local_rcv)
 168                 return br_pass_frame_up(skb);
 169 
 170 out:
 171         return 0;
 172 drop:
 173         kfree_skb(skb);
 174         goto out;
 175 }
 176 EXPORT_SYMBOL_GPL(br_handle_frame_finish);
 177 
 178 static void __br_handle_local_finish(struct sk_buff *skb)
 179 {
 180         struct net_bridge_port *p = br_port_get_rcu(skb->dev);
 181         u16 vid = 0;
 182 
 183         /* check if vlan is allowed, to avoid spoofing */
 184         if ((p->flags & BR_LEARNING) &&
 185             !br_opt_get(p->br, BROPT_NO_LL_LEARN) &&
 186             br_should_learn(p, skb, &vid))
 187                 br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, false);
 188 }
 189 
 190 /* note: already called with rcu_read_lock */
 191 static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
 192 {
 193         __br_handle_local_finish(skb);
 194 
 195         /* return 1 to signal the okfn() was called so it's ok to use the skb */
 196         return 1;
 197 }
 198 
 199 static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb)
 200 {
 201 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
 202         struct nf_hook_entries *e = NULL;
 203         struct nf_hook_state state;
 204         unsigned int verdict, i;
 205         struct net *net;
 206         int ret;
 207 
 208         net = dev_net(skb->dev);
 209 #ifdef HAVE_JUMP_LABEL
 210         if (!static_key_false(&nf_hooks_needed[NFPROTO_BRIDGE][NF_BR_PRE_ROUTING]))
 211                 goto frame_finish;
 212 #endif
 213 
 214         e = rcu_dereference(net->nf.hooks_bridge[NF_BR_PRE_ROUTING]);
 215         if (!e)
 216                 goto frame_finish;
 217 
 218         nf_hook_state_init(&state, NF_BR_PRE_ROUTING,
 219                            NFPROTO_BRIDGE, skb->dev, NULL, NULL,
 220                            net, br_handle_frame_finish);
 221 
 222         for (i = 0; i < e->num_hook_entries; i++) {
 223                 verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state);
 224                 switch (verdict & NF_VERDICT_MASK) {
 225                 case NF_ACCEPT:
 226                         if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) {
 227                                 *pskb = skb;
 228                                 return RX_HANDLER_PASS;
 229                         }
 230                         break;
 231                 case NF_DROP:
 232                         kfree_skb(skb);
 233                         return RX_HANDLER_CONSUMED;
 234                 case NF_QUEUE:
 235                         ret = nf_queue(skb, &state, i, verdict);
 236                         if (ret == 1)
 237                                 continue;
 238                         return RX_HANDLER_CONSUMED;
 239                 default: /* STOLEN */
 240                         return RX_HANDLER_CONSUMED;
 241                 }
 242         }
 243 frame_finish:
 244         net = dev_net(skb->dev);
 245         br_handle_frame_finish(net, NULL, skb);
 246 #else
 247         br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
 248 #endif
 249         return RX_HANDLER_CONSUMED;
 250 }
 251 
 252 /*
 253  * Return NULL if skb is handled
 254  * note: already called with rcu_read_lock
 255  */
 256 rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
 257 {
 258         struct net_bridge_port *p;
 259         struct sk_buff *skb = *pskb;
 260         const unsigned char *dest = eth_hdr(skb)->h_dest;
 261 
 262         if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
 263                 return RX_HANDLER_PASS;
 264 
 265         if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
 266                 goto drop;
 267 
 268         skb = skb_share_check(skb, GFP_ATOMIC);
 269         if (!skb)
 270                 return RX_HANDLER_CONSUMED;
 271 
 272         memset(skb->cb, 0, sizeof(struct br_input_skb_cb));
 273 
 274         p = br_port_get_rcu(skb->dev);
 275         if (p->flags & BR_VLAN_TUNNEL) {
 276                 if (br_handle_ingress_vlan_tunnel(skb, p,
 277                                                   nbp_vlan_group_rcu(p)))
 278                         goto drop;
 279         }
 280 
 281         if (unlikely(is_link_local_ether_addr(dest))) {
 282                 u16 fwd_mask = p->br->group_fwd_mask_required;
 283 
 284                 /*
 285                  * See IEEE 802.1D Table 7-10 Reserved addresses
 286                  *
 287                  * Assignment                           Value
 288                  * Bridge Group Address         01-80-C2-00-00-00
 289                  * (MAC Control) 802.3          01-80-C2-00-00-01
 290                  * (Link Aggregation) 802.3     01-80-C2-00-00-02
 291                  * 802.1X PAE address           01-80-C2-00-00-03
 292                  *
 293                  * 802.1AB LLDP                 01-80-C2-00-00-0E
 294                  *
 295                  * Others reserved for future standardization
 296                  */
 297                 fwd_mask |= p->group_fwd_mask;
 298                 switch (dest[5]) {
 299                 case 0x00:      /* Bridge Group Address */
 300                         /* If STP is turned off,
 301                            then must forward to keep loop detection */
 302                         if (p->br->stp_enabled == BR_NO_STP ||
 303                             fwd_mask & (1u << dest[5]))
 304                                 goto forward;
 305                         *pskb = skb;
 306                         __br_handle_local_finish(skb);
 307                         return RX_HANDLER_PASS;
 308 
 309                 case 0x01:      /* IEEE MAC (Pause) */
 310                         goto drop;
 311 
 312                 case 0x0E:      /* 802.1AB LLDP */
 313                         fwd_mask |= p->br->group_fwd_mask;
 314                         if (fwd_mask & (1u << dest[5]))
 315                                 goto forward;
 316                         *pskb = skb;
 317                         __br_handle_local_finish(skb);
 318                         return RX_HANDLER_PASS;
 319 
 320                 default:
 321                         /* Allow selective forwarding for most other protocols */
 322                         fwd_mask |= p->br->group_fwd_mask;
 323                         if (fwd_mask & (1u << dest[5]))
 324                                 goto forward;
 325                 }
 326 
 327                 /* The else clause should be hit when nf_hook():
 328                  *   - returns < 0 (drop/error)
 329                  *   - returns = 0 (stolen/nf_queue)
 330                  * Thus return 1 from the okfn() to signal the skb is ok to pass
 331                  */
 332                 if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
 333                             dev_net(skb->dev), NULL, skb, skb->dev, NULL,
 334                             br_handle_local_finish) == 1) {
 335                         return RX_HANDLER_PASS;
 336                 } else {
 337                         return RX_HANDLER_CONSUMED;
 338                 }
 339         }
 340 
 341 forward:
 342         switch (p->state) {
 343         case BR_STATE_FORWARDING:
 344         case BR_STATE_LEARNING:
 345                 if (ether_addr_equal(p->br->dev->dev_addr, dest))
 346                         skb->pkt_type = PACKET_HOST;
 347 
 348                 return nf_hook_bridge_pre(skb, pskb);
 349         default:
 350 drop:
 351                 kfree_skb(skb);
 352         }
 353         return RX_HANDLER_CONSUMED;
 354 }

/* [<][>][^][v][top][bottom][index][help] */