Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e4a8e817 authored by Daniel Borkmann's avatar Daniel Borkmann Committed by David S. Miller
Browse files

bpf: misc xdp redirect cleanups



Few cleanups including: bpf_redirect_map() is really XDP only due to
the return code. Move it to a more appropriate location where we do
the XDP redirect handling and change it's name into bpf_xdp_redirect_map()
to make it consistent to the bpf_xdp_redirect() helper.

xdp_do_redirect_map() helper can be static since only used out of filter.c
file. Drop the goto in xdp_do_generic_redirect() and only return errors
directly. In xdp_do_flush_map() only clear ri->map_to_flush which is the
arg we're using in that function, ri->map is cleared earlier along with
ri->ifindex.

Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent cd36c3a2
Loading
Loading
Loading
Loading
+32 −40
Original line number Diff line number Diff line
@@ -1835,29 +1835,6 @@ static const struct bpf_func_proto bpf_redirect_proto = {
	.arg2_type      = ARG_ANYTHING,
};

BPF_CALL_3(bpf_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags)
{
	struct redirect_info *ri = this_cpu_ptr(&redirect_info);

	if (unlikely(flags))
		return XDP_ABORTED;

	ri->ifindex = ifindex;
	ri->flags = flags;
	ri->map = map;

	return XDP_REDIRECT;
}

static const struct bpf_func_proto bpf_redirect_map_proto = {
	.func           = bpf_redirect_map,
	.gpl_only       = false,
	.ret_type       = RET_INTEGER,
	.arg1_type      = ARG_CONST_MAP_PTR,
	.arg2_type      = ARG_ANYTHING,
	.arg3_type      = ARG_ANYTHING,
};

BPF_CALL_3(bpf_sk_redirect_map, struct bpf_map *, map, u32, key, u64, flags)
{
	struct redirect_info *ri = this_cpu_ptr(&redirect_info);
@@ -2506,13 +2483,11 @@ static int __bpf_tx_xdp(struct net_device *dev,
	err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
	if (err)
		return err;

	if (map)
		__dev_map_insert_ctx(map, index);
	else
		dev->netdev_ops->ndo_xdp_flush(dev);

	return err;
	return 0;
}

void xdp_do_flush_map(void)
@@ -2520,15 +2495,13 @@ void xdp_do_flush_map(void)
	struct redirect_info *ri = this_cpu_ptr(&redirect_info);
	struct bpf_map *map = ri->map_to_flush;

	ri->map = NULL;
	ri->map_to_flush = NULL;

	if (map)
		__dev_map_flush(map);
}
EXPORT_SYMBOL_GPL(xdp_do_flush_map);

int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
			       struct bpf_prog *xdp_prog)
{
	struct redirect_info *ri = this_cpu_ptr(&redirect_info);
@@ -2545,14 +2518,12 @@ int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
		err = -EINVAL;
		goto out;
	}

	if (ri->map_to_flush && (ri->map_to_flush != map))
	if (ri->map_to_flush && ri->map_to_flush != map)
		xdp_do_flush_map();

	err = __bpf_tx_xdp(fwd, map, xdp, index);
	if (likely(!err))
		ri->map_to_flush = map;

out:
	trace_xdp_redirect(dev, fwd, xdp_prog, XDP_REDIRECT, err);
	return err;
@@ -2594,20 +2565,17 @@ int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb)
	ri->ifindex = 0;
	if (unlikely(!dev)) {
		bpf_warn_invalid_xdp_redirect(index);
		goto err;
		return -EINVAL;
	}

	if (unlikely(!(dev->flags & IFF_UP)))
		goto err;

		return -ENETDOWN;
	len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
	if (skb->len > len)
		goto err;
		return -E2BIG;

	skb->dev = dev;
	return 0;
err:
	return -EINVAL;
}
EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);

@@ -2620,6 +2588,7 @@ BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)

	ri->ifindex = ifindex;
	ri->flags = flags;

	return XDP_REDIRECT;
}

@@ -2631,6 +2600,29 @@ static const struct bpf_func_proto bpf_xdp_redirect_proto = {
	.arg2_type      = ARG_ANYTHING,
};

BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags)
{
	struct redirect_info *ri = this_cpu_ptr(&redirect_info);

	if (unlikely(flags))
		return XDP_ABORTED;

	ri->ifindex = ifindex;
	ri->flags = flags;
	ri->map = map;

	return XDP_REDIRECT;
}

static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
	.func           = bpf_xdp_redirect_map,
	.gpl_only       = false,
	.ret_type       = RET_INTEGER,
	.arg1_type      = ARG_CONST_MAP_PTR,
	.arg2_type      = ARG_ANYTHING,
	.arg3_type      = ARG_ANYTHING,
};

bool bpf_helper_changes_pkt_data(void *func)
{
	if (func == bpf_skb_vlan_push ||
@@ -3233,7 +3225,7 @@ xdp_func_proto(enum bpf_func_id func_id)
	case BPF_FUNC_redirect:
		return &bpf_xdp_redirect_proto;
	case BPF_FUNC_redirect_map:
		return &bpf_redirect_map_proto;
		return &bpf_xdp_redirect_map_proto;
	default:
		return bpf_base_func_proto(func_id);
	}