bpf: add helper for getting xfrm states
Subsystems (6)
v2 cover bpf: add helper for getting xfrm states
This patchset adds support for fetching XFRM state information from an eBPF program called from TC.
The first patch introduces a helper for fetching an XFRM state from the skb's secpath. The XFRM state is modeled using a new virtual struct which contains the SPI, peer address, and reqid values of the state; This struct can be extended in the future to provide additional state information.
The second patch adds a test example in test_tunnel_bpf.sh. The sample validates the correct extraction of state information by the eBPF program.
---
v2:
- Fixed two comments by Daniel Borkmann:
- disallow reserved flags in helper call
- avoid compiling in helper code when CONFIG_XFRM is off
Eyal Birger (2):
bpf: add helper for getting xfrm states samples/bpf: extend test_tunnel_bpf.sh with xfrm state test
include/uapi/linux/bpf.h | 25 ++++++++++- net/core/filter.c | 48 +++++++++++++++++++++ samples/bpf/tcbpf2_kern.c | 15 +++++++ samples/bpf/test_tunnel_bpf.sh | 71 +++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 25 ++++++++++- tools/testing/selftests/bpf/bpf_helpers.h | 4 +- 6 files changed, 185 insertions(+), 3 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/2 bpf: add helper for getting xfrm states
This commit introduces a helper which allows fetching xfrm state parameters by eBPF programs attached to TC.
Prototype:bpf_skb_get_xfrm_state(skb, index, xfrm_state, size, flags)
skb: pointer to skb
index: the index in the skb xfrm_state secpath arrayxfrm_state: pointer to 'struct bpf_xfrm_state'
size: size of 'struct bpf_xfrm_state'
flags: reserved for future extensions
The helper returns 0 on success. Non zero if no xfrm state at the index is found - or non exists at all.
struct bpf_xfrm_state currently includes the SPI, peer IPv4/IPv6 address and the reqid; it can be further extended by adding elements to its end - indicating the populated fields by the 'size' argument - keeping backwards compatibility.
Typical usage:
struct bpf_xfrm_state x = {};bpf_skb_get_xfrm_state(skb, 0, &x, sizeof(x), 0);
...
--- include/uapi/linux/bpf.h | 25 ++++++++++++++++++++++++- net/core/filter.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +24 -1
@@ -762,6 +762,15 @@ union bpf_attr {* @xdp_md: pointer to xdp_md * @delta: A negative integer to be added to xdp_md.data_end * Return: 0 on success or negative on error +* +* int bpf_skb_get_xfrm_state(skb, index, xfrm_state, size, flags) +* retrieve XFRM state +* @skb: pointer to skb +* @index: index of the xfrm state in the secpath +* @key: pointer to 'struct bpf_xfrm_state' +* @size: size of 'struct bpf_xfrm_state' +* @flags: room for future extensions +* Return: 0 on success or negative error */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -829,7 +838,8 @@ union bpf_attr {FN (msg_cork_bytes), \FN (msg_pull_data), \FN (bind), \ -FN (xdp_adjust_tail), +FN (xdp_adjust_tail), \ +FN (skb_get_xfrm_state),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -935,6 +945,19 @@ struct bpf_tunnel_key {__u32 tunnel_label; }; +/* user accessible mirror of in-kernel xfrm_state. +* new fields can only be added to the end of this structure +*/ +struct bpf_xfrm_state { +__u32 reqid; +__u32 spi; +__u16 family; +union { +__u32 remote_ipv4; +__u32 remote_ipv6[4 ]; + }; +}; +/* Generic BPF return codes which all BPF program types may support. * The values are binary compatible with their TC_ACT_* counter-part to * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
net/core/filter.c +48 -0
66 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH v2 2/2 samples/bpf: extend test_tunnel_bpf.sh with xfrm state test
In reply to Eyal BirgerAdd a test for fetching xfrm state parameters from a tc program running on ingress.
--- samples/bpf/tcbpf2_kern.c | 15 +++++++ samples/bpf/test_tunnel_bpf.sh | 71 +++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 25 ++++++++++- tools/testing/selftests/bpf/bpf_helpers.h | 4 +- 4 files changed, 113 insertions(+), 2 deletions(-)
samples/bpf/tcbpf2_kern.c +15 -0
@@ -593,4 +593,19 @@ int _ip6ip6_get_tunnel(struct __sk_buff *skb)return TC_ACT_OK ; } +SEC("xfrm_get_state") +int _xfrm_get_state (struct __sk_buff *skb) +{ +struct bpf_xfrm_state x; +char fmt[] ="reqid %d spi 0x%x remote ip 0x%x\n" ; +int ret; + + ret =bpf_skb_get_xfrm_state (skb,0 , &x,sizeof (x),0 ); +if (ret <0 ) +return TC_ACT_OK ; + +bpf_trace_printk (fmt,sizeof (fmt), x.reqid, x.spi, x.remote_ipv4); +return TC_ACT_OK ; +} + char _license[] SEC("license") = "GPL";
samples/bpf/test_tunnel_bpf.sh +71 -0
93 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +24 -1
@@ -762,6 +762,15 @@ union bpf_attr {* @xdp_md: pointer to xdp_md * @delta: A negative integer to be added to xdp_md.data_end * Return: 0 on success or negative on error +* +* int bpf_skb_get_xfrm_state(skb, index, xfrm_state, size, flags) +* retrieve XFRM state +* @skb: pointer to skb +* @index: index of the xfrm state in the secpath +* @key: pointer to 'struct bpf_xfrm_state' +* @size: size of 'struct bpf_xfrm_state' +* @flags: room for future extensions +* Return: 0 on success or negative error */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -829,7 +838,8 @@ union bpf_attr {FN (msg_cork_bytes), \FN (msg_pull_data), \FN (bind), \ -FN (xdp_adjust_tail), +FN (xdp_adjust_tail), \ +FN (skb_get_xfrm_state),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -934,6 +944,19 @@ struct bpf_tunnel_key {__u32 tunnel_label; }; +/* user accessible mirror of in-kernel xfrm_state. +* new fields can only be added to the end of this structure +*/ +struct bpf_xfrm_state { +__u32 reqid; +__u32 spi; +__u16 family; +union { +__u32 remote_ipv4; +__u32 remote_ipv6[4 ]; + }; +}; +/* Generic BPF return codes which all BPF program types may support. * The values are binary compatible with their TC_ACT_* counter-part to * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
tools/testing/selftests/bpf/bpf_helpers.h +3 -1
@@ -98,7 +98,9 @@ static int (*bpf_bind)(void *ctx, void *addr, int addr_len) = (void *) BPF_FUNC_bind;static int (*bpf_xdp_adjust_tail)(void *ctx,int offset) = (void *) BPF_FUNC_xdp_adjust_tail; - +static int (*bpf_skb_get_xfrm_state)(void *ctx,int index,void *state, +int size,int flags) = + (void *) BPF_FUNC_skb_get_xfrm_state;/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
Signature · 1 line
2.7.4
Quoted 112 lines · Eyal Birger
This commit introduces a helper which allows fetching xfrm state parameters by eBPF programs attached to TC.
Prototype:
bpf_skb_get_xfrm_state(skb, index, xfrm_state, size, flags)skb: pointer to skb
index: the index in the skbxfrm_statesecpath arrayxfrm_state: pointer to 'structbpf_xfrm_state'
size: size of 'structbpf_xfrm_state'
flags: reserved for future extensionsThe helper returns 0 on success. Non zero if no xfrm state at the index is found - or non exists at all.
struct
bpf_xfrm_statecurrently includes the SPI, peer IPv4/IPv6 address and the reqid; it can be further extended by adding elements to its end - indicating the populated fields by the 'size' argument - keeping backwards compatibility.Typical usage:
struct
bpf_xfrm_statex = {};bpf_skb_get_xfrm_state(skb, 0, &x, sizeof(x), 0);
...Signed-off-by Eyal Birger <[email protected]>--- include/uapi/linux/bpf.h | 25 ++++++++++++++++++++++++- net/core/filter.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-)include/uapi/linux/bpf.h +24
-1@@ -762,6 +762,15 @@ union bpf_attr {* @xdp_md: pointer to xdp_md * @delta: A negative integer to be added to xdp_md.data_end * Return: 0 on success or negative on error +* +* int bpf_skb_get_xfrm_state(skb, index, xfrm_state, size, flags) +* retrieve XFRM state +* @skb: pointer to skb +* @index: index of the xfrm state in the secpath +* @key: pointer to 'struct bpf_xfrm_state' +* @size: size of 'struct bpf_xfrm_state' +* @flags: room for future extensions +* Return: 0 on success or negative error */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -829,7 +838,8 @@ union bpf_attr {FN (msg_cork_bytes), \FN (msg_pull_data), \FN (bind), \ -FN (xdp_adjust_tail), +FN (xdp_adjust_tail), \ +FN (skb_get_xfrm_state),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -935,6 +945,19 @@ struct bpf_tunnel_key {__u32 tunnel_label; }; +/* user accessible mirror of in-kernel xfrm_state. +* new fields can only be added to the end of this structure +*/ +struct bpf_xfrm_state { +__u32 reqid; +__u32 spi; +__u16 family; +union { +__u32 remote_ipv4; +__u32 remote_ipv6[4 ]; + }; +}; +/* Generic BPF return codes which all BPF program types may support. * The values are binary compatible with their TC_ACT_* counter-part to * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT net/core/filter.c +15
-0@@ -57,6 +57,7 @@#include <net/sock_reuseport.h> #include <net/busy_poll.h> #include <net/tcp.h> +#include <net/xfrm.h> #include <linux/bpf_trace.h> /** @@ -3749,6 +3750,49 @@ static const struct bpf_func_proto bpf_bind_proto = { .arg3_type = ARG_CONST_SIZE, }; +#ifdef CONFIG_XFRM +BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index, + struct bpf_xfrm_state *, to, u32, size, u64, flags) +{ +const struct sec_path *sp =skb_sec_path (skb); +const struct xfrm_state *x; + +if (!sp ||unlikely (index >= sp->len || flags)) + goto err_clear; + + x = sp->xvec[index]; + +if (unlikely (size !=sizeof (struct bpf_xfrm_state ))) + goto err_clear;
net/core/filter.c static const struct bpf_func_proto bpf_bind_proto = { · line 3776
Quoted 10 lines
net/core/filter.c +10
-0+ + to->reqid = x->props.reqid; + to->spi =be32_to_cpu (x->id.spi); + to->family = x->props.family; +if (to->family ==AF_INET6 ) { +memcpy (to->remote_ipv6, x->props.saddr.a6, +sizeof (to->remote_ipv6)); + }else { + to->remote_ipv4 =be32_to_cpu (x->props.saddr.a4); + }
that looks inconsistent. Why v4 is cpu endian, but v6 not?
Why change endianness of the spi?
Hi,
Quoted 13 lines · On Wed, 18 Apr 2018 15:31:03 -0700 Alexei Starovoitov…
Quoted 12 lines · Eyal Birger
This commit introduces a helper which allows fetching xfrm state parameters by eBPF programs attached to TC.
Prototype:
bpf_skb_get_xfrm_state(skb, index, xfrm_state, size, flags)skb: pointer to skb
index: the index in the skbxfrm_statesecpath arrayxfrm_state: pointer to 'structbpf_xfrm_state'
size: size of 'structbpf_xfrm_state'
flags: reserved for future extensions
<snip>
Quoted 19 lines
Quoted 19 lines
(quoted diff) +2
-0+#ifdef CONFIG_XFRM +BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32,index,
net/core/filter.c +16
-0+ struct bpf_xfrm_state *, to, u32, size, u64, flags) +{ + const struct sec_path *sp = skb_sec_path(skb); +const struct xfrm_state *x; + +if (!sp ||unlikely (index >= sp->len || flags)) + goto err_clear; + + x = sp->xvec[index]; + +if (unlikely (size !=sizeof (struct bpf_xfrm_state ))) + goto err_clear; + + to->reqid = x->props.reqid; + to->spi =be32_to_cpu (x->id.spi); + to->family = x->props.family;
Quoted 8 lines
net/core/filter.c static const struct bpf_func_proto bpf_bind_proto = { · line 3776
Quoted 6 lines
net/core/filter.c +6
-0+if (to->family ==AF_INET6 ) { +memcpy (to->remote_ipv6, x->props.saddr.a6, +sizeof (to->remote_ipv6)); + }else { + to->remote_ipv4 =be32_to_cpu (x->props.saddr.a4); + }that looks inconsistent. Why v4 is cpu endian, but v6 not?
I agree. I followed the reference in bpf_skb_get_tunnel_key(). I can keep v4 in net endianess too.
Quoted 1 line
Why change endianness of the spi?
I felt it was more consistent with other fields and usually helpful for programs. I can keep it in network order.
In which case, do you expect it to be typed as __be32 in bpf.h? (I haven't seen other cases)?
Thanks for your feedback!
Quoted 49 lines · Eyal Birger
Hi,
Quoted 13 lines · On Wed, 18 Apr 2018 15:31:03 -0700 Alexei Starovoitov…
Quoted 12 lines · Eyal Birger
This commit introduces a helper which allows fetching xfrm state parameters by eBPF programs attached to TC.
Prototype:
bpf_skb_get_xfrm_state(skb, index, xfrm_state, size, flags)skb: pointer to skb
index: the index in the skbxfrm_statesecpath arrayxfrm_state: pointer to 'structbpf_xfrm_state'
size: size of 'structbpf_xfrm_state'
flags: reserved for future extensions<snip>
Quoted 19 lines
Quoted 19 lines
(quoted diff) +2
-0+#ifdef CONFIG_XFRM +BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32,index,
net/core/filter.c +16
-0+ struct bpf_xfrm_state *, to, u32, size, u64, flags) +{ + const struct sec_path *sp = skb_sec_path(skb); +const struct xfrm_state *x; + +if (!sp ||unlikely (index >= sp->len || flags)) + goto err_clear; + + x = sp->xvec[index]; + +if (unlikely (size !=sizeof (struct bpf_xfrm_state ))) + goto err_clear; + + to->reqid = x->props.reqid; + to->spi =be32_to_cpu (x->id.spi); + to->family = x->props.family;Quoted 8 lines
net/core/filter.c static const struct bpf_func_proto bpf_bind_proto = { · line 3776
Quoted 6 lines
net/core/filter.c +6
-0+if (to->family ==AF_INET6 ) { +memcpy (to->remote_ipv6, x->props.saddr.a6, +sizeof (to->remote_ipv6)); + }else { + to->remote_ipv4 =be32_to_cpu (x->props.saddr.a4); + }that looks inconsistent. Why v4 is cpu endian, but v6 not?
Quoted 2 lines
I agree. I followed the reference in
bpf_skb_get_tunnel_key(). I can keep v4 in net endianess too.
argh.
On one side it makes sense to be consistent with bpf_skb_get_tunnel_key() but it's certainly confusing to have v4 and v6 in different endianness. Imagine man page that says that bpf folks made a mistake in that helper can kept repeating it in other helpers for consistency... Daniel, what do you think?
Do you remember the history with bpf_skb_get_tunnel_key and why it happened that way?
Quoted 7 lines
Quoted 1 line
Why change endianness of the spi?
I felt it was more consistent with other fields and usually helpful for programs. I can keep it in network order.
In which case, do you expect it to be typed as
__be32inbpf.h? (I haven't seen other cases)?
It can be __u32 with a comment /* Stored in network byte order */ like in bunch of other fields.
Daniel Borkmann
maintainerQuoted 49 lines · Alexei Starovoitov
Quoted 45 lines · Eyal Birger
Quoted 12 lines · On Wed, 18 Apr 2018 15:31:03 -0700 Alexei Starovoitov…
Quoted 11 lines · Eyal Birger
This commit introduces a helper which allows fetching xfrm state parameters by eBPF programs attached to TC.
Prototype:
bpf_skb_get_xfrm_state(skb, index, xfrm_state, size, flags)skb: pointer to skb
index: the index in the skbxfrm_statesecpath arrayxfrm_state: pointer to 'structbpf_xfrm_state'
size: size of 'structbpf_xfrm_state'
flags: reserved for future extensions<snip>
Quoted 19 lines
Quoted 19 lines
(quoted diff) +2
-0+#ifdef CONFIG_XFRM +BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32,index,
net/core/filter.c +16
-0+ struct bpf_xfrm_state *, to, u32, size, u64, flags) +{ + const struct sec_path *sp = skb_sec_path(skb); +const struct xfrm_state *x; + +if (!sp ||unlikely (index >= sp->len || flags)) + goto err_clear; + + x = sp->xvec[index]; + +if (unlikely (size !=sizeof (struct bpf_xfrm_state ))) + goto err_clear; + + to->reqid = x->props.reqid; + to->spi =be32_to_cpu (x->id.spi); + to->family = x->props.family;Quoted 8 lines
net/core/filter.c static const struct bpf_func_proto bpf_bind_proto = { · line 3776
Quoted 6 lines
net/core/filter.c +6
-0+if (to->family ==AF_INET6 ) { +memcpy (to->remote_ipv6, x->props.saddr.a6, +sizeof (to->remote_ipv6)); + }else { + to->remote_ipv4 =be32_to_cpu (x->props.saddr.a4); + }that looks inconsistent. Why v4 is cpu endian, but v6 not?
Quoted 2 lines
I agree. I followed the reference in
bpf_skb_get_tunnel_key(). I can keep v4 in net endianess too.
Quoted 8 lines
argh.
On one side it makes sense to be consistent withbpf_skb_get_tunnel_key()but it's certainly confusing to have v4 and v6 in different endianness. Imagine man page that says that bpf folks made a mistake in that helper can kept repeating it in other helpers for consistency... Daniel, what do you think?
Do you remember the history withbpf_skb_get_tunnel_keyand why it happened that way?
Check out d3aa45ce6b94 ("bpf: add helpers to access tunnel metadata"). I presume there was no particular reason for doing it this way, perhaps to mimic old ld_abs kind of behavior, I don't know.
Quoted 10 lines
Quoted 7 lines
Quoted 1 line
Why change endianness of the spi?
I felt it was more consistent with other fields and usually helpful for programs. I can keep it in network order.
In which case, do you expect it to be typed as
__be32inbpf.h? (I haven't seen other cases)?It can be
__u32with a comment /* Stored in network byte order */ like in bunch of other fields.
Yeah, agree. I guess I would have been fine either way given this is the way things are with the get/set tunnel helpers, but on the other hand this helper does not really have a concrete tie to them, so given we start fresh on this one, we should make both v4/v6 consistent and document it appropriately.
Eyal, please respin the series with that. The rest was good to go from my pov.
Thank you,
Daniel