hulk inclusion category: featrue bugzilla: https://atomgit.com/openeuler/kernel/issues/8480 -------------------------------- Add bpf_get_skb_ethhdr helper function to fetch the ether header of the ingress skb. Signed-off-by: Pu Lehui <pulehui@huawei.com> --- include/uapi/linux/bpf.h | 7 +++++++ net/core/filter.c | 22 ++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 3 files changed, 36 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 3a415984bc27..353561c18652 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -3980,6 +3980,12 @@ union bpf_attr { * Set skb ingress dst entry with valid sock rx dst entry. * Return * 0 on success, or a negative error in case of failure. + * + * int bpf_get_skb_ethhdr(struct sk_buff *skb, void *peth, int size) + * Description + * Get the ether header of ingress skb. + * Return + * 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4163,6 +4169,7 @@ union bpf_attr { FN(change_skb_dev), \ FN(ext_memcpy), \ FN(set_ingress_dst), \ + FN(get_skb_ethhdr), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/net/core/filter.c b/net/core/filter.c index ff0a2845ae13..53027d8d122b 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -3731,6 +3731,26 @@ static const struct bpf_func_proto bpf_set_ingress_dst_proto = { .arg2_type = ARG_ANYTHING, }; +BPF_CALL_3(bpf_get_skb_ethhdr, struct sk_buff *, skb, void *, peth, int, size) +{ + struct ethhdr *eth = eth_hdr(skb); + + if (size != sizeof(struct ethhdr)) + return -EINVAL; + + memcpy(peth, eth, size); + return 0; +} + +static const struct bpf_func_proto bpf_get_skb_ethhdr_proto = { + .func = bpf_get_skb_ethhdr, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_PTR_TO_MEM, + .arg3_type = ARG_CONST_SIZE, +}; + BPF_CALL_2(bpf_skb_change_skb_dev, struct sk_buff *, skb, u32, ifindex) { struct net_device *dev; @@ -7459,6 +7479,8 @@ hisock_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_skb_change_skb_dev_proto; case BPF_FUNC_set_ingress_dst: return is_ingress ? &bpf_set_ingress_dst_proto : NULL; + case BPF_FUNC_get_skb_ethhdr: + return is_ingress ? &bpf_get_skb_ethhdr_proto : NULL; default: return bpf_base_func_proto(func_id); diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 48c0c054f82d..2bf6ef95e964 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -3980,6 +3980,12 @@ union bpf_attr { * Set skb ingress dst entry with valid sock rx dst entry. * Return * 0 on success, or a negative error in case of failure. + * + * int bpf_get_skb_ethhdr(struct sk_buff *skb, void *peth, int size) + * Description + * Get the ether header of ingress skb. + * Return + * 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4163,6 +4169,7 @@ union bpf_attr { FN(change_skb_dev), \ FN(ext_memcpy), \ FN(set_ingress_dst), \ + FN(get_skb_ethhdr), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper -- 2.34.1