cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

br_input.c (11463B)


      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
     26static int
     27br_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
     33static 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
     43	/* Reset the offload_fwd_mark because there could be a stacked
     44	 * bridge above, and it should not think this bridge it doing
     45	 * that bridge's work forwarding out its ports.
     46	 */
     47	br_switchdev_frame_unmark(skb);
     48
     49	/* Bridge is just like any other port.  Make sure the
     50	 * packet is allowed except in promisc mode when someone
     51	 * may be running packet capture.
     52	 */
     53	if (!(brdev->flags & IFF_PROMISC) &&
     54	    !br_allowed_egress(vg, skb)) {
     55		kfree_skb(skb);
     56		return NET_RX_DROP;
     57	}
     58
     59	indev = skb->dev;
     60	skb->dev = brdev;
     61	skb = br_handle_vlan(br, NULL, vg, skb);
     62	if (!skb)
     63		return NET_RX_DROP;
     64	/* update the multicast stats if the packet is IGMP/MLD */
     65	br_multicast_count(br, NULL, skb, br_multicast_igmp_type(skb),
     66			   BR_MCAST_DIR_TX);
     67
     68	return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
     69		       dev_net(indev), NULL, skb, indev, NULL,
     70		       br_netif_receive_skb);
     71}
     72
     73/* note: already called with rcu_read_lock */
     74int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
     75{
     76	struct net_bridge_port *p = br_port_get_rcu(skb->dev);
     77	enum br_pkt_type pkt_type = BR_PKT_UNICAST;
     78	struct net_bridge_fdb_entry *dst = NULL;
     79	struct net_bridge_mcast_port *pmctx;
     80	struct net_bridge_mdb_entry *mdst;
     81	bool local_rcv, mcast_hit = false;
     82	struct net_bridge_mcast *brmctx;
     83	struct net_bridge_vlan *vlan;
     84	struct net_bridge *br;
     85	u16 vid = 0;
     86	u8 state;
     87
     88	if (!p)
     89		goto drop;
     90
     91	br = p->br;
     92
     93	if (br_mst_is_enabled(br)) {
     94		state = BR_STATE_FORWARDING;
     95	} else {
     96		if (p->state == BR_STATE_DISABLED)
     97			goto drop;
     98
     99		state = p->state;
    100	}
    101
    102	brmctx = &p->br->multicast_ctx;
    103	pmctx = &p->multicast_ctx;
    104	if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid,
    105				&state, &vlan))
    106		goto out;
    107
    108	if (p->flags & BR_PORT_LOCKED) {
    109		struct net_bridge_fdb_entry *fdb_src =
    110			br_fdb_find_rcu(br, eth_hdr(skb)->h_source, vid);
    111
    112		if (!fdb_src || READ_ONCE(fdb_src->dst) != p ||
    113		    test_bit(BR_FDB_LOCAL, &fdb_src->flags))
    114			goto drop;
    115	}
    116
    117	nbp_switchdev_frame_mark(p, skb);
    118
    119	/* insert into forwarding database after filtering to avoid spoofing */
    120	if (p->flags & BR_LEARNING)
    121		br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, 0);
    122
    123	local_rcv = !!(br->dev->flags & IFF_PROMISC);
    124	if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) {
    125		/* by definition the broadcast is also a multicast address */
    126		if (is_broadcast_ether_addr(eth_hdr(skb)->h_dest)) {
    127			pkt_type = BR_PKT_BROADCAST;
    128			local_rcv = true;
    129		} else {
    130			pkt_type = BR_PKT_MULTICAST;
    131			if (br_multicast_rcv(&brmctx, &pmctx, vlan, skb, vid))
    132				goto drop;
    133		}
    134	}
    135
    136	if (state == BR_STATE_LEARNING)
    137		goto drop;
    138
    139	BR_INPUT_SKB_CB(skb)->brdev = br->dev;
    140	BR_INPUT_SKB_CB(skb)->src_port_isolated = !!(p->flags & BR_ISOLATED);
    141
    142	if (IS_ENABLED(CONFIG_INET) &&
    143	    (skb->protocol == htons(ETH_P_ARP) ||
    144	     skb->protocol == htons(ETH_P_RARP))) {
    145		br_do_proxy_suppress_arp(skb, br, vid, p);
    146	} else if (IS_ENABLED(CONFIG_IPV6) &&
    147		   skb->protocol == htons(ETH_P_IPV6) &&
    148		   br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
    149		   pskb_may_pull(skb, sizeof(struct ipv6hdr) +
    150				 sizeof(struct nd_msg)) &&
    151		   ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
    152			struct nd_msg *msg, _msg;
    153
    154			msg = br_is_nd_neigh_msg(skb, &_msg);
    155			if (msg)
    156				br_do_suppress_nd(skb, br, vid, p, msg);
    157	}
    158
    159	switch (pkt_type) {
    160	case BR_PKT_MULTICAST:
    161		mdst = br_mdb_get(brmctx, skb, vid);
    162		if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
    163		    br_multicast_querier_exists(brmctx, eth_hdr(skb), mdst)) {
    164			if ((mdst && mdst->host_joined) ||
    165			    br_multicast_is_router(brmctx, skb)) {
    166				local_rcv = true;
    167				br->dev->stats.multicast++;
    168			}
    169			mcast_hit = true;
    170		} else {
    171			local_rcv = true;
    172			br->dev->stats.multicast++;
    173		}
    174		break;
    175	case BR_PKT_UNICAST:
    176		dst = br_fdb_find_rcu(br, eth_hdr(skb)->h_dest, vid);
    177		break;
    178	default:
    179		break;
    180	}
    181
    182	if (dst) {
    183		unsigned long now = jiffies;
    184
    185		if (test_bit(BR_FDB_LOCAL, &dst->flags))
    186			return br_pass_frame_up(skb);
    187
    188		if (now != dst->used)
    189			dst->used = now;
    190		br_forward(dst->dst, skb, local_rcv, false);
    191	} else {
    192		if (!mcast_hit)
    193			br_flood(br, skb, pkt_type, local_rcv, false);
    194		else
    195			br_multicast_flood(mdst, skb, brmctx, local_rcv, false);
    196	}
    197
    198	if (local_rcv)
    199		return br_pass_frame_up(skb);
    200
    201out:
    202	return 0;
    203drop:
    204	kfree_skb(skb);
    205	goto out;
    206}
    207EXPORT_SYMBOL_GPL(br_handle_frame_finish);
    208
    209static void __br_handle_local_finish(struct sk_buff *skb)
    210{
    211	struct net_bridge_port *p = br_port_get_rcu(skb->dev);
    212	u16 vid = 0;
    213
    214	/* check if vlan is allowed, to avoid spoofing */
    215	if ((p->flags & BR_LEARNING) &&
    216	    nbp_state_should_learn(p) &&
    217	    !br_opt_get(p->br, BROPT_NO_LL_LEARN) &&
    218	    br_should_learn(p, skb, &vid))
    219		br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, 0);
    220}
    221
    222/* note: already called with rcu_read_lock */
    223static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
    224{
    225	__br_handle_local_finish(skb);
    226
    227	/* return 1 to signal the okfn() was called so it's ok to use the skb */
    228	return 1;
    229}
    230
    231static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb)
    232{
    233#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
    234	struct nf_hook_entries *e = NULL;
    235	struct nf_hook_state state;
    236	unsigned int verdict, i;
    237	struct net *net;
    238	int ret;
    239
    240	net = dev_net(skb->dev);
    241#ifdef HAVE_JUMP_LABEL
    242	if (!static_key_false(&nf_hooks_needed[NFPROTO_BRIDGE][NF_BR_PRE_ROUTING]))
    243		goto frame_finish;
    244#endif
    245
    246	e = rcu_dereference(net->nf.hooks_bridge[NF_BR_PRE_ROUTING]);
    247	if (!e)
    248		goto frame_finish;
    249
    250	nf_hook_state_init(&state, NF_BR_PRE_ROUTING,
    251			   NFPROTO_BRIDGE, skb->dev, NULL, NULL,
    252			   net, br_handle_frame_finish);
    253
    254	for (i = 0; i < e->num_hook_entries; i++) {
    255		verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state);
    256		switch (verdict & NF_VERDICT_MASK) {
    257		case NF_ACCEPT:
    258			if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) {
    259				*pskb = skb;
    260				return RX_HANDLER_PASS;
    261			}
    262			break;
    263		case NF_DROP:
    264			kfree_skb(skb);
    265			return RX_HANDLER_CONSUMED;
    266		case NF_QUEUE:
    267			ret = nf_queue(skb, &state, i, verdict);
    268			if (ret == 1)
    269				continue;
    270			return RX_HANDLER_CONSUMED;
    271		default: /* STOLEN */
    272			return RX_HANDLER_CONSUMED;
    273		}
    274	}
    275frame_finish:
    276	net = dev_net(skb->dev);
    277	br_handle_frame_finish(net, NULL, skb);
    278#else
    279	br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
    280#endif
    281	return RX_HANDLER_CONSUMED;
    282}
    283
    284/* Return 0 if the frame was not processed otherwise 1
    285 * note: already called with rcu_read_lock
    286 */
    287static int br_process_frame_type(struct net_bridge_port *p,
    288				 struct sk_buff *skb)
    289{
    290	struct br_frame_type *tmp;
    291
    292	hlist_for_each_entry_rcu(tmp, &p->br->frame_type_list, list)
    293		if (unlikely(tmp->type == skb->protocol))
    294			return tmp->frame_handler(p, skb);
    295
    296	return 0;
    297}
    298
    299/*
    300 * Return NULL if skb is handled
    301 * note: already called with rcu_read_lock
    302 */
    303static rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
    304{
    305	struct net_bridge_port *p;
    306	struct sk_buff *skb = *pskb;
    307	const unsigned char *dest = eth_hdr(skb)->h_dest;
    308
    309	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
    310		return RX_HANDLER_PASS;
    311
    312	if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
    313		goto drop;
    314
    315	skb = skb_share_check(skb, GFP_ATOMIC);
    316	if (!skb)
    317		return RX_HANDLER_CONSUMED;
    318
    319	memset(skb->cb, 0, sizeof(struct br_input_skb_cb));
    320
    321	p = br_port_get_rcu(skb->dev);
    322	if (p->flags & BR_VLAN_TUNNEL)
    323		br_handle_ingress_vlan_tunnel(skb, p, nbp_vlan_group_rcu(p));
    324
    325	if (unlikely(is_link_local_ether_addr(dest))) {
    326		u16 fwd_mask = p->br->group_fwd_mask_required;
    327
    328		/*
    329		 * See IEEE 802.1D Table 7-10 Reserved addresses
    330		 *
    331		 * Assignment		 		Value
    332		 * Bridge Group Address		01-80-C2-00-00-00
    333		 * (MAC Control) 802.3		01-80-C2-00-00-01
    334		 * (Link Aggregation) 802.3	01-80-C2-00-00-02
    335		 * 802.1X PAE address		01-80-C2-00-00-03
    336		 *
    337		 * 802.1AB LLDP 		01-80-C2-00-00-0E
    338		 *
    339		 * Others reserved for future standardization
    340		 */
    341		fwd_mask |= p->group_fwd_mask;
    342		switch (dest[5]) {
    343		case 0x00:	/* Bridge Group Address */
    344			/* If STP is turned off,
    345			   then must forward to keep loop detection */
    346			if (p->br->stp_enabled == BR_NO_STP ||
    347			    fwd_mask & (1u << dest[5]))
    348				goto forward;
    349			*pskb = skb;
    350			__br_handle_local_finish(skb);
    351			return RX_HANDLER_PASS;
    352
    353		case 0x01:	/* IEEE MAC (Pause) */
    354			goto drop;
    355
    356		case 0x0E:	/* 802.1AB LLDP */
    357			fwd_mask |= p->br->group_fwd_mask;
    358			if (fwd_mask & (1u << dest[5]))
    359				goto forward;
    360			*pskb = skb;
    361			__br_handle_local_finish(skb);
    362			return RX_HANDLER_PASS;
    363
    364		default:
    365			/* Allow selective forwarding for most other protocols */
    366			fwd_mask |= p->br->group_fwd_mask;
    367			if (fwd_mask & (1u << dest[5]))
    368				goto forward;
    369		}
    370
    371		/* The else clause should be hit when nf_hook():
    372		 *   - returns < 0 (drop/error)
    373		 *   - returns = 0 (stolen/nf_queue)
    374		 * Thus return 1 from the okfn() to signal the skb is ok to pass
    375		 */
    376		if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
    377			    dev_net(skb->dev), NULL, skb, skb->dev, NULL,
    378			    br_handle_local_finish) == 1) {
    379			return RX_HANDLER_PASS;
    380		} else {
    381			return RX_HANDLER_CONSUMED;
    382		}
    383	}
    384
    385	if (unlikely(br_process_frame_type(p, skb)))
    386		return RX_HANDLER_PASS;
    387
    388forward:
    389	if (br_mst_is_enabled(p->br))
    390		goto defer_stp_filtering;
    391
    392	switch (p->state) {
    393	case BR_STATE_FORWARDING:
    394	case BR_STATE_LEARNING:
    395defer_stp_filtering:
    396		if (ether_addr_equal(p->br->dev->dev_addr, dest))
    397			skb->pkt_type = PACKET_HOST;
    398
    399		return nf_hook_bridge_pre(skb, pskb);
    400	default:
    401drop:
    402		kfree_skb(skb);
    403	}
    404	return RX_HANDLER_CONSUMED;
    405}
    406
    407/* This function has no purpose other than to appease the br_port_get_rcu/rtnl
    408 * helpers which identify bridged ports according to the rx_handler installed
    409 * on them (so there _needs_ to be a bridge rx_handler even if we don't need it
    410 * to do anything useful). This bridge won't support traffic to/from the stack,
    411 * but only hardware bridging. So return RX_HANDLER_PASS so we don't steal
    412 * frames from the ETH_P_XDSA packet_type handler.
    413 */
    414static rx_handler_result_t br_handle_frame_dummy(struct sk_buff **pskb)
    415{
    416	return RX_HANDLER_PASS;
    417}
    418
    419rx_handler_func_t *br_get_rx_handler(const struct net_device *dev)
    420{
    421	if (netdev_uses_dsa(dev))
    422		return br_handle_frame_dummy;
    423
    424	return br_handle_frame;
    425}
    426
    427void br_add_frame(struct net_bridge *br, struct br_frame_type *ft)
    428{
    429	hlist_add_head_rcu(&ft->list, &br->frame_type_list);
    430}
    431
    432void br_del_frame(struct net_bridge *br, struct br_frame_type *ft)
    433{
    434	struct br_frame_type *tmp;
    435
    436	hlist_for_each_entry(tmp, &br->frame_type_list, list)
    437		if (ft == tmp) {
    438			hlist_del_rcu(&ft->list);
    439			return;
    440		}
    441}