1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Unstable XFRM state BPF helpers.
4 * Note that it is allowed to break compatibility for these functions since the
5 * interface they are exposed through to BPF programs is explicitly unstable.
10 #include <linux/btf_ids.h>
14 /* bpf_xfrm_state_opts - Options for XFRM state lookup helpers
17 * @error - Out parameter, set for any errors encountered
19 * -EINVAL - netns_id is less than -1
20 * -EINVAL - opts__sz isn't BPF_XFRM_STATE_OPTS_SZ
21 * -ENONET - No network namespace found for netns_id
22 * -ENOENT - No xfrm_state found
23 * @netns_id - Specify the network namespace for lookup
25 * BPF_F_CURRENT_NETNS (-1)
26 * Use namespace associated with ctx
28 * Network Namespace ID
29 * @mark - XFRM mark to match on
30 * @daddr - Destination address to match on
31 * @spi - Security parameter index to match on
32 * @proto - IP protocol to match on (eg. IPPROTO_ESP)
33 * @family - Protocol family to match on (AF_INET/AF_INET6)
35 struct bpf_xfrm_state_opts
{
46 BPF_XFRM_STATE_OPTS_SZ
= sizeof(struct bpf_xfrm_state_opts
),
49 __bpf_kfunc_start_defs();
51 /* bpf_xdp_get_xfrm_state - Get XFRM state
53 * A `struct xfrm_state *`, if found, must be released with a corresponding
54 * bpf_xdp_xfrm_state_release.
57 * @ctx - Pointer to ctx (xdp_md) in XDP program
59 * @opts - Options for lookup (documented above)
61 * @opts__sz - Length of the bpf_xfrm_state_opts structure
62 * Must be BPF_XFRM_STATE_OPTS_SZ
64 __bpf_kfunc
struct xfrm_state
*
65 bpf_xdp_get_xfrm_state(struct xdp_md
*ctx
, struct bpf_xfrm_state_opts
*opts
, u32 opts__sz
)
67 struct xdp_buff
*xdp
= (struct xdp_buff
*)ctx
;
68 struct net
*net
= dev_net(xdp
->rxq
->dev
);
71 if (!opts
|| opts__sz
< sizeof(opts
->error
))
74 if (opts__sz
!= BPF_XFRM_STATE_OPTS_SZ
) {
75 opts
->error
= -EINVAL
;
79 if (unlikely(opts
->netns_id
< BPF_F_CURRENT_NETNS
)) {
80 opts
->error
= -EINVAL
;
84 if (opts
->netns_id
>= 0) {
85 net
= get_net_ns_by_id(net
, opts
->netns_id
);
87 opts
->error
= -ENONET
;
92 x
= xfrm_state_lookup(net
, opts
->mark
, &opts
->daddr
, opts
->spi
,
93 opts
->proto
, opts
->family
);
95 if (opts
->netns_id
>= 0)
98 opts
->error
= -ENOENT
;
103 /* bpf_xdp_xfrm_state_release - Release acquired xfrm_state object
105 * This must be invoked for referenced PTR_TO_BTF_ID, and the verifier rejects
106 * the program if any references remain in the program in all of the explored
110 * @x - Pointer to referenced xfrm_state object, obtained using
111 * bpf_xdp_get_xfrm_state.
113 __bpf_kfunc
void bpf_xdp_xfrm_state_release(struct xfrm_state
*x
)
118 __bpf_kfunc_end_defs();
120 BTF_KFUNCS_START(xfrm_state_kfunc_set
)
121 BTF_ID_FLAGS(func
, bpf_xdp_get_xfrm_state
, KF_RET_NULL
| KF_ACQUIRE
)
122 BTF_ID_FLAGS(func
, bpf_xdp_xfrm_state_release
, KF_RELEASE
)
123 BTF_KFUNCS_END(xfrm_state_kfunc_set
)
125 static const struct btf_kfunc_id_set xfrm_state_xdp_kfunc_set
= {
126 .owner
= THIS_MODULE
,
127 .set
= &xfrm_state_kfunc_set
,
130 int __init
register_xfrm_state_bpf(void)
132 return register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP
,
133 &xfrm_state_xdp_kfunc_set
);