hulk inclusion category: featrue bugzilla: https://atomgit.com/openeuler/kernel/issues/8480 -------------------------------- Add bpf_set_ingress/egress_dev helper function to set network device to ingress or egress skb. Signed-off-by: Pu Lehui <pulehui@huawei.com> --- include/uapi/linux/bpf.h | 14 +++++++++++ net/core/filter.c | 44 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 14 +++++++++++ 3 files changed, 72 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 353561c18652..8d393472de02 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -3986,6 +3986,18 @@ union bpf_attr { * Get the ether header of ingress skb. * Return * 0 on success, or a negative error in case of failure. + * + * int bpf_set_ingress_dev(struct sk_buff *skb, void *dev) + * Description + * Set valid net device to ingress skb. + * Return + * 0 on success, or negative error in case of failure. + * + * int bpf_set_egress_dev(struct sk_buff *skb, void *dev) + * Description + * Set valid net device to egress skb. + * Return + * 0 on success, or negative error in case of failure. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4170,6 +4182,8 @@ union bpf_attr { FN(ext_memcpy), \ FN(set_ingress_dst), \ FN(get_skb_ethhdr), \ + FN(set_ingress_dev), \ + FN(set_egress_dev), \ /* */ /* 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 53027d8d122b..0ee0c974b17b 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -3751,6 +3751,46 @@ static const struct bpf_func_proto bpf_get_skb_ethhdr_proto = { .arg3_type = ARG_CONST_SIZE, }; +BPF_CALL_2(bpf_set_ingress_dev, struct sk_buff *, skb, void *, _dev) +{ + struct net_device *dev = (struct net_device *)_dev; + + if (!dev || !virt_addr_valid(dev)) + return -EFAULT; + + skb->dev = dev; + skb->skb_iif = dev->ifindex; + skb->pkt_type = PACKET_HOST; + return 0; +} + +static const struct bpf_func_proto bpf_set_ingress_dev_proto = { + .func = bpf_set_ingress_dev, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_ANYTHING, +}; + +BPF_CALL_2(bpf_set_egress_dev, struct sk_buff *, skb, void *, _dev) +{ + struct net_device *dev = (struct net_device *)_dev; + + if (!dev || !virt_addr_valid(dev)) + return -EFAULT; + + skb->dev = dev; + return 0; +} + +static const struct bpf_func_proto bpf_set_egress_dev_proto = { + .func = bpf_set_egress_dev, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_ANYTHING, +}; + BPF_CALL_2(bpf_skb_change_skb_dev, struct sk_buff *, skb, u32, ifindex) { struct net_device *dev; @@ -7481,6 +7521,10 @@ hisock_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return is_ingress ? &bpf_set_ingress_dst_proto : NULL; case BPF_FUNC_get_skb_ethhdr: return is_ingress ? &bpf_get_skb_ethhdr_proto : NULL; + case BPF_FUNC_set_ingress_dev: + return is_ingress ? &bpf_set_ingress_dev_proto : NULL; + case BPF_FUNC_set_egress_dev: + return !is_ingress ? &bpf_set_egress_dev_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 2bf6ef95e964..9dfe2ea7a018 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -3986,6 +3986,18 @@ union bpf_attr { * Get the ether header of ingress skb. * Return * 0 on success, or a negative error in case of failure. + * + * int bpf_set_ingress_dev(struct sk_buff *skb, void *dev) + * Description + * Set valid net device to ingress skb. + * Return + * 0 on success, or negative error in case of failure. + * + * int bpf_set_egress_dev(struct sk_buff *skb, void *dev) + * Description + * Set valid net device to egress skb. + * Return + * 0 on success, or negative error in case of failure. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4170,6 +4182,8 @@ union bpf_attr { FN(ext_memcpy), \ FN(set_ingress_dst), \ FN(get_skb_ethhdr), \ + FN(set_ingress_dev), \ + FN(set_egress_dev), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper -- 2.34.1