bpf: add icmp_send kfunc
Subsystems (6)
v10 cover bpf: add icmp_send kfunc
Hello,
This is v10 of adding the icmp_send kfunc, as suggested during LSF/MM/BPF 2025[^1]. The goal is to allow cgroup_skb programs to actively reject east-west traffic, similarly to what is possible to do with netfilter reject target. Applications can receive early feedback that something went wrong during the TCP handshake.
The first step to implement this is using ICMP control messages, with the ICMP_DEST_UNREACH type with various code ICMP_NET_UNREACH, ICMP_HOST_UNREACH, ICMP_PROT_UNREACH, etc. This is easier to implement than a TCP RST reply and will already hint the client TCP stack to abort the connection and not retry extensively.
Note that this is different than the sock_destroy kfunc, that along calls tcp_abort and thus sends a reset, destroying the underlying socket.
Caveats of this kfunc design are that a program can call this function N times, thus send N ICMP unreach control messages and that the program can return from the BPF filter with pass leading to a potential confusing situation where the TCP connection was established while the client received ICMP_DEST_UNREACH messages.
v2 updates:
- fix a build error from a missing function call rename;
- avoid changing return line in bpf_kfunc_init;
- return SK_DROP from the kfunc (similarly to bpf_redirect);
- check the return value in the selftest.
v3 update:
- fix an undefined reference build error.
v4 updates:
- prevent the kfunc to be called recursively and add a test (thanks to Martin).
- do not fetch dst route when unnecessary (thanks to Martin).
- extend the test for IPv6 (thanks to Martin).
- use SK_DROP in examples and use non blocking sockets for testing (thanks to Martin).
- test when the kfunc returns -EINVAL (thanks to Jordan).
- add the kfunc to bpf_kfunc_set_skb as suggested by Alexei.
- guard the IPv4 parts with IS_ENABLED(CONFIG_INET).
- fix a wrong initial value for client_fd (thanks to Yonghong).
- add documentation to the kfunc.
- to Jordan: I couldn't include <linux/icmp.h> because of redefines from <network_helpers.h>.
v5 updates:
- kfunc name is now icmp_send and takes the control message type as parameter for future potential extension (daniel)
- drop the net patches to route packet since now the kfunc is limited to cgroup_skb and tc progs (daniel & martin)
- linearize skb headers (sashiko)
- zero SKB control block (sashiko)
- bind to port 0 instead of fixed port (sashiko)
- poll to wait for POLLERR event (sashiko)
- do not use ASSERT_EQ in CMSG_NXTHDR loop (sashiko)
- fix comment about byte order (sashiko)
- fix endianness IP address issue (sashiko)
- add forgotten cleanup_cgroup_environment (sashiko)
- let packets pass in recursion test (sashiko)
- clarify evaluation order for recursion test (sashiko)
v6 updates (all from sashiko):
- bring back the net patches to route packet since tc ingress needs it.
- rename the ip_route_reply helpers from fetch to fill.
- call pskb_network_may_pull on the cloned pkt.
- check explicitly that we received one and only one ICMP err ctrl msg.
v7 updates:
- use consume_skb on success path (stanislav)
- replace recursion protection with CPU_ARRAY by checking the nature of the sk (daniel, offline)
- use reverse xmas tree in read_icmp_errqueue (jordan)
- use ASSERT_OK_FD instead of ASSERT_GE whenever possible (jordan)
- add a test for tc (jordan)
- better filtering from host cgroup test progs (sashiko)
v8 updates:
- mostly a resend as it's been sitting as "New" in the queue for almost one month, fixed a few nits.
- on new bpf_icmp_send kfunc cgroup_skb test (patch 4/7):
- guard a close fd with fd >= 0 (jordan)
- use ASSERT_OK_FD instead of ASSERT_GE (jordan)
- fixed comment style (sashiko)
- on recursion test (patch 7/7):
- guard a close fd with fd >= 0 (jordan)
- fixed comments style (sashiko)
- filter bpf prog on pid and ICMP message types (sashiko)
v9 updates:
- first, there was a v8.5 that I discussed here[^2] with Emil Tsalapatis. I tried once again to make tc work but the ai review found something fundamentally wrong. This version removes the tc support for now and focuses on cgroup_skb.
- use helper get_socket_local_port instead of getsockname (sashiko)
- use if_nametoindex("lo") instead of value 1 (bpf-ci)
- fix IPV6_RECVERR appearance before IPv6 patch (bpf-ci)
- precise that 0 on success mean icmp_send was called but it was just an attempt since this function does not return anything (sashiko)
- explicitly consider ICMP_FRAG_NEEDED as invalid in bpf_icmp_send as it would miss the next-hop MTU info. Also test it. (sashiko)
- test for max_code + 1 for invalid (sashiko)
- add review-by tags from Jordan and Emil but remove it on the main patch as I have significantly changed it.
- check for rec_count in recursion test (sashiko)
- re-order setup_cgroup_environmment in test (sashiko)
- reset kfunc_ret on every test run (sashiko)
- check for skb route for icmp_send as the function would quietly fail and add a test (sashiko)
v10 updates:
- guard against skbs with metadata_dst before calling icmpv6_send (sashiko)
- add more review-by tags from Emil and Jordan.
[^1]: https://lwn.net/Articles/1022034/
[^2]: https://lore.kernel.org/bpf/[email protected]/
Link to v9: https://lore.kernel.org/bpf/[email protected]/
Mahe Tardy (5):
bpf: add bpf_icmp_send kfunc
selftests/bpf: add bpf_icmp_send kfunc cgroup_skb tests selftests/bpf: add bpf_icmp_send kfunc cgroup_skb IPv6 tests selftests/bpf: add bpf_icmp_send recursion test selftests/bpf: add bpf_icmp_send no route test
net/core/filter.c | 95 +++++++ .../bpf/prog_tests/icmp_send_kfunc.c | 269 ++++++++++++++++++ tools/testing/selftests/bpf/progs/icmp_send.c | 123 ++++++++ 3 files changed, 487 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/icmp_send_kfunc.c create mode 100644 tools/testing/selftests/bpf/progs/icmp_send.c
--
2.34.1
PATCH v10 1/5 bpf: add bpf_icmp_send kfunc
This is needed in the context of Tetragon to provide improved feedback (in contrast to just dropping packets) to east-west traffic when blocked by policies using cgroup_skb programs.
This reuses concepts from netfilter reject target codepath with the differences that:
* Packets are cloned since the BPF user can still let the packet pass (SK_PASS from the cgroup_skb progs for example) and the current skb need to stay untouched (cgroup_skb hooks only allow read-only skb payload).
* We protect against recursion since the kfunc, by generating an ICMP error message, could retrigger the BPF prog that invoked it.
Only ICMP_DEST_UNREACH and ICMPV6_DEST_UNREACH are currently supported. The interface accepts a type parameter to facilitate future extension to other ICMP control message types.
For normal cgroup_skb paths, the skb dst route should already be set. However, bpf_prog_test_run_skb can create synthetic IPv4 skbs without an attached route. In that case, icmp_send returns early, and the kfunc would otherwise report success despite no ICMP reply being sent. The check also rejects metadata dsts, which are not valid struct rtable instances. For IPv6, reject metadata dsts only: icmpv6_send can reach icmp6_dev, where skb_rt6_info treats any non-NULL skb dst as a struct rt6_info, which is not valid for metadata_dst.
--- net/core/filter.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)
net/core/filter.c +95 -0
@@ -84,6 +84,9 @@#include <linux/un.h> #include <net/xdp_sock_drv.h> #include <net/inet_dscp.h> +#include <linux/icmpv6.h> +#include <net/icmp.h> +#include <net/ip6_route.h> #include "dev.h"
@@ -12546,6 +12549,88 @@ __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len)return 0 ; } +/** +* bpf_icmp_send - Send an ICMP control message +* @skb_ctx: Packet that triggered the control message +* @type: ICMP type (only ICMP_DEST_UNREACH/ICMPV6_DEST_UNREACH supported) +* @code: ICMP code (0-15 except ICMP_FRAG_NEEDED for IPv4, 0-6 for IPv6) +* +* Sends an ICMP control message in response to the packet. The original packet +* is cloned before sending the ICMP message, so the BPF program can still let +* the packet pass if desired. +* +* Currently only ICMP_DEST_UNREACH (IPv4) and ICMPV6_DEST_UNREACH (IPv6) are +* supported. +* +* Return: 0 on success (send attempt), negative error code on failure: +* -EBUSY: Recursion detected +* -EPROTONOSUPPORT: Non-IP protocol +* -EOPNOTSUPP: Unsupported ICMP type +* -EINVAL: Invalid code parameter +* -ENETUNREACH: No usable route/dst for the ICMP reply +* -ENOMEM: Memory allocation failed +*/ +__bpf_kfunc int bpf_icmp_send(struct __sk_buff *skb_ctx, int type, int code) +{ +struct sk_buff *skb = (struct sk_buff *)skb_ctx; +struct sk_buff *nskb; +struct sock *sk; + + sk =skb_to_full_sk (skb); +if (sk && sk->sk_kern_sock && + (sk->sk_protocol ==IPPROTO_ICMP || sk->sk_protocol ==IPPROTO_ICMPV6 )) +return -EBUSY ; + +switch (skb->protocol) { +#if IS_ENABLED (CONFIG_INET ) +case htons (ETH_P_IP ): { +if (type !=ICMP_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >NR_ICMP_UNREACH || + code ==ICMP_FRAG_NEEDED )/* needs a valid next-hop MTU */ +return -EINVAL ; + +/* icmp_send expects skb_dst to be a real rtable. */ +if (!skb_valid_dst (skb)) +return -ENETUNREACH ; + + nskb =skb_clone (skb,GFP_ATOMIC ); +if (!nskb) +return -ENOMEM ; + +memset (IPCB (nskb),0 ,sizeof (*IPCB (nskb))); +icmp_send (nskb, type, code,0 ); +consume_skb (nskb); +break ; + } +#endif +#if IS_ENABLED (CONFIG_IPV6 ) +case htons (ETH_P_IPV6 ): +if (type !=ICMPV6_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >ICMPV6_REJECT_ROUTE ) +return -EINVAL ; + +/* icmpv6_send may treat skb_dst as rt6_info. */ +if (skb_metadata_dst (skb)) +return -ENETUNREACH ; + + nskb =skb_clone (skb,GFP_ATOMIC ); +if (!nskb) +return -ENOMEM ; + +memset (IP6CB (nskb),0 ,sizeof (*IP6CB (nskb))); +icmpv6_send (nskb, type, code,0 ); +consume_skb (nskb); +break ; +#endif +default : +return -EPROTONOSUPPORT ; + } + +return 0 ; +} +__bpf_kfunc_end_defs (); int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,
@@ -12588,6 +12673,10 @@ BTF_KFUNCS_START(bpf_kfunc_check_set_sock_ops) BTF_ID_FLAGS(func, bpf_sock_ops_enable_tx_tstamp)BTF_KFUNCS_END (bpf_kfunc_check_set_sock_ops) +BTF_KFUNCS_START (bpf_kfunc_check_set_icmp_send ) +BTF_ID_FLAGS (func, bpf_icmp_send) +BTF_KFUNCS_END (bpf_kfunc_check_set_icmp_send) + static const struct btf_kfunc_id_set bpf_kfunc_set_skb = { .owner =THIS_MODULE , .set = &bpf_kfunc_check_set_skb,
@@ -12618,6 +12707,11 @@ static const struct btf_kfunc_id_set bpf_kfunc_set_sock_ops = { .set = &bpf_kfunc_check_set_sock_ops, }; +static const struct btf_kfunc_id_set bpf_kfunc_set_icmp_send = { + .owner =THIS_MODULE , + .set = &bpf_kfunc_check_set_icmp_send, +}; +static int __init bpf_kfunc_init (void ) {int ret;
@@ -12639,6 +12733,7 @@ static int __init bpf_kfunc_init(void) ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_CGROUP_SOCK_ADDR , &bpf_kfunc_set_sock_addr); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SCHED_CLS , &bpf_kfunc_set_tcp_reqsk); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_CGROUP_SKB , &bpf_kfunc_set_icmp_send);return ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SOCK_OPS , &bpf_kfunc_set_sock_ops); }late_initcall (bpf_kfunc_init);
--
2.34.1
Re: [PATCH bpf-next v10 1/5] bpf: add bpf_icmp_send kfunc
Quoted 112 lines · Mahe Tardy
This is needed in the context of Tetragon to provide improved feedback (in contrast to just dropping packets) to east-west traffic when blocked by policies using
cgroup_skbprograms.This reuses concepts from netfilter reject target codepath with the differences that:
* Packets are cloned since the BPF user can still let the packet pass (SK_PASSfrom thecgroup_skbprogs for example) and the current skb need to stay untouched (cgroup_skbhooks only allow read-only skb payload).
* We protect against recursion since the kfunc, by generating an ICMP error message, could retrigger the BPF prog that invoked it.Only
ICMP_DEST_UNREACHandICMPV6_DEST_UNREACHare currently supported. The interface accepts a type parameter to facilitate future extension to other ICMP control message types.For normal
cgroup_skbpaths, the skb dst route should already be set. However,bpf_prog_test_run_skbcan create synthetic IPv4 skbs without an attached route. In that case,icmp_sendreturns early, and the kfunc would otherwise report success despite no ICMP reply being sent. The check also rejects metadata dsts, which are not valid struct rtable instances. For IPv6, reject metadata dsts only:icmpv6_sendcan reachicmp6_dev, whereskb_rt6_infotreats any non-NULL skb dst as a structrt6_info, which is not valid formetadata_dst.Reviewed-by Emil Tsalapatis <[email protected]>Reviewed-by Jordan Rife <[email protected]>Signed-off-by Mahe Tardy <[email protected]>--- net/core/filter.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)net/core/filter.c +64
-0@@ -84,6 +84,9 @@#include <linux/un.h> #include <net/xdp_sock_drv.h> #include <net/inet_dscp.h> +#include <linux/icmpv6.h> +#include <net/icmp.h> +#include <net/ip6_route.h> #include "dev.h" @@ -12546,6 +12549,88 @@ __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len)return 0 ; } +/** +* bpf_icmp_send - Send an ICMP control message +* @skb_ctx: Packet that triggered the control message +* @type: ICMP type (only ICMP_DEST_UNREACH/ICMPV6_DEST_UNREACH supported) +* @code: ICMP code (0-15 except ICMP_FRAG_NEEDED for IPv4, 0-6 for IPv6) +* +* Sends an ICMP control message in response to the packet. The original packet +* is cloned before sending the ICMP message, so the BPF program can still let +* the packet pass if desired. +* +* Currently only ICMP_DEST_UNREACH (IPv4) and ICMPV6_DEST_UNREACH (IPv6) are +* supported. +* +* Return: 0 on success (send attempt), negative error code on failure: +* -EBUSY: Recursion detected +* -EPROTONOSUPPORT: Non-IP protocol +* -EOPNOTSUPP: Unsupported ICMP type +* -EINVAL: Invalid code parameter +* -ENETUNREACH: No usable route/dst for the ICMP reply +* -ENOMEM: Memory allocation failed +*/ +__bpf_kfunc int bpf_icmp_send(struct __sk_buff *skb_ctx, int type, int code) +{ +struct sk_buff *skb = (struct sk_buff *)skb_ctx; +struct sk_buff *nskb; +struct sock *sk; + + sk =skb_to_full_sk (skb); +if (sk && sk->sk_kern_sock && + (sk->sk_protocol ==IPPROTO_ICMP || sk->sk_protocol ==IPPROTO_ICMPV6 )) +return -EBUSY ; + +switch (skb->protocol) { +#if IS_ENABLED (CONFIG_INET ) +case htons (ETH_P_IP ): { +if (type !=ICMP_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >NR_ICMP_UNREACH || + code ==ICMP_FRAG_NEEDED )/* needs a valid next-hop MTU */ +return -EINVAL ; + +/* icmp_send expects skb_dst to be a real rtable. */ +if (!skb_valid_dst (skb)) +return -ENETUNREACH ; + + nskb =skb_clone (skb,GFP_ATOMIC ); +if (!nskb) +return -ENOMEM ; + +memset (IPCB (nskb),0 ,sizeof (*IPCB (nskb))); +icmp_send (nskb, type, code,0 ); +consume_skb (nskb); +break ; + } +#endif +#if IS_ENABLED (CONFIG_IPV6 ) +case htons (ETH_P_IPV6 ): +if (type !=ICMPV6_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >ICMPV6_REJECT_ROUTE ) +return -EINVAL ;
[..]
net/core/filter.c __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len) · line 12616
Quoted 3 lines
net/core/filter.c +3
-0+/* icmpv6_send may treat skb_dst as rt6_info. */ +if (skb_metadata_dst (skb)) +return -ENETUNREACH ;
A bit confused about this. Which part of icmpv6_send treats skb_dst as rt6_info? (I see the original sashiko report about dst, but icmp6 seems to be not requiring it)
Re: [PATCH bpf-next v10 1/5] bpf: add bpf_icmp_send kfunc
Quoted 1 line · Stanislav Fomichev
On 06/25, Mahe Tardy wrote:
[...]
Quoted 43 lines
Quoted 40 lines
net/core/filter.c +40
-0+__bpf_kfunc int bpf_icmp_send(struct __sk_buff *skb_ctx, int type, int code) +{ +struct sk_buff *skb = (struct sk_buff *)skb_ctx; +struct sk_buff *nskb; +struct sock *sk; + + sk =skb_to_full_sk (skb); +if (sk && sk->sk_kern_sock && + (sk->sk_protocol ==IPPROTO_ICMP || sk->sk_protocol ==IPPROTO_ICMPV6 )) +return -EBUSY ; + +switch (skb->protocol) { +#if IS_ENABLED (CONFIG_INET ) +case htons (ETH_P_IP ): { +if (type !=ICMP_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >NR_ICMP_UNREACH || + code ==ICMP_FRAG_NEEDED )/* needs a valid next-hop MTU */ +return -EINVAL ; + +/* icmp_send expects skb_dst to be a real rtable. */ +if (!skb_valid_dst (skb)) +return -ENETUNREACH ; + + nskb =skb_clone (skb,GFP_ATOMIC ); +if (!nskb) +return -ENOMEM ; + +memset (IPCB (nskb),0 ,sizeof (*IPCB (nskb))); +icmp_send (nskb, type, code,0 ); +consume_skb (nskb); +break ; + } +#endif +#if IS_ENABLED (CONFIG_IPV6 ) +case htons (ETH_P_IPV6 ): +if (type !=ICMPV6_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >ICMPV6_REJECT_ROUTE ) +return -EINVAL ;[..]
Quoted 7 lines
net/core/filter.c __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len) · line 12616
Quoted 3 lines
net/core/filter.c +3
-0+/* icmpv6_send may treat skb_dst as rt6_info. */ +if (skb_metadata_dst (skb)) +return -ENETUNREACH ;A bit confused about this. Which part of
icmpv6_sendtreatsskb_dstasrt6_info? (I see the original sashiko report about dst, but icmp6 seems to be not requiring it)
Yeah I was also a bit confused because this came out of nowhere as soon as I put the skb_valid_dst only on the IPv4 path (for different reasons), but there is actually a potential trace in which we have type confusion indeed:
- icmp6_send() checks scoped source addresses and calls icmp6_iif() at net/ipv6/icmp.c:702
- icmp6_iif() calls icmp6_dev() at net/ipv6/icmp.c:441
- icmp6_dev() does skb_rt6_info(skb) for loopback/L3 master devices at net/ipv6/icmp.c:428
- skb_rt6_info() casts any non-NULL dst to struct rt6_info at include/net/ip6_route.h:233
- rt6->rt6i_idev is then dereferenced at net/ipv6/icmp.c:434
When checking with pahole, we can find this on my local kernel:
struct rt6_info {
struct dst_entry dst; /* 0 136 */
/* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */
struct fib6_info * from; /* 136 8 */
int sernum; /* 144 4 */
struct rt6key rt6i_dst; /* 148 20 */
struct rt6key rt6i_src; /* 168 20 */
struct in6_addr rt6i_gateway; /* 188 16 */
/* XXX 4 bytes hole, try to pack */
/* --- cacheline 3 boundary (192 bytes) was 16 bytes ago --- */
struct inet6_dev * rt6i_idev; /* 208 8 */ <--- we dereference this
u32 rt6i_flags; /* 216 4 */
short unsigned int rt6i_nfheader_len; /* 220 2 */
/* size: 224, cachelines: 4, members: 9 */ /* sum members: 218, holes: 1, sum holes: 4 */ /* padding: 2 */
/* last cacheline: 32 bytes */
};
And the metadata_dst would look like this:
struct metadata_dst {
struct dst_entry dst; /* 0 136 */
/* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */
enum metadata_type type; /* 136 4 */
/* XXX 4 bytes hole, try to pack */
union {
struct ip_tunnel_info tun_info; /* 144 96 */
struct hw_port_info port_info; /* 144 16 */
struct macsec_info macsec_info; /* 144 8 */
struct xfrm_md_info xfrm_info; /* 144 16 */
} u; /* 144 96 */ <--- we land on this union
/* size: 240, cachelines: 4, members: 3 */
/* sum members: 236, holes: 1, sum holes: 4 */
/* last cacheline: 48 bytes */
};Let's say it's a struct ip_tunnel_info:
struct ip_tunnel_info {
struct ip_tunnel_key key; /* 0 64 */
/* XXX last struct has 7 bytes of padding */
/* --- cacheline 1 boundary (64 bytes) --- */
struct ip_tunnel_encap encap; /* 64 8 */ <--- 144 + 64 = 208 we land here
struct dst_cache dst_cache; /* 72 16 */
u8 options_len; /* 88 1 */
u8 mode; /* 89 1 */
/* size: 96, cachelines: 2, members: 5 */
/* padding: 6 */
/* paddings: 1, sum paddings: 7 */
/* last cacheline: 32 bytes */
};So I imagine this is fairly tricky to trigger but still a case of type confusion. I have actually no idea how likely this can happen from my call but the trace makes sense at least.
Re: [PATCH bpf-next v10 1/5] bpf: add bpf_icmp_send kfunc
Quoted 131 lines · Mahe Tardy
Quoted 1 line · Stanislav Fomichev
On 06/25, Mahe Tardy wrote:
[...]
Quoted 43 lines
Quoted 40 lines
net/core/filter.c +40
-0+__bpf_kfunc int bpf_icmp_send(struct __sk_buff *skb_ctx, int type, int code) +{ +struct sk_buff *skb = (struct sk_buff *)skb_ctx; +struct sk_buff *nskb; +struct sock *sk; + + sk =skb_to_full_sk (skb); +if (sk && sk->sk_kern_sock && + (sk->sk_protocol ==IPPROTO_ICMP || sk->sk_protocol ==IPPROTO_ICMPV6 )) +return -EBUSY ; + +switch (skb->protocol) { +#if IS_ENABLED (CONFIG_INET ) +case htons (ETH_P_IP ): { +if (type !=ICMP_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >NR_ICMP_UNREACH || + code ==ICMP_FRAG_NEEDED )/* needs a valid next-hop MTU */ +return -EINVAL ; + +/* icmp_send expects skb_dst to be a real rtable. */ +if (!skb_valid_dst (skb)) +return -ENETUNREACH ; + + nskb =skb_clone (skb,GFP_ATOMIC ); +if (!nskb) +return -ENOMEM ; + +memset (IPCB (nskb),0 ,sizeof (*IPCB (nskb))); +icmp_send (nskb, type, code,0 ); +consume_skb (nskb); +break ; + } +#endif +#if IS_ENABLED (CONFIG_IPV6 ) +case htons (ETH_P_IPV6 ): +if (type !=ICMPV6_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >ICMPV6_REJECT_ROUTE ) +return -EINVAL ;[..]
Quoted 7 lines
net/core/filter.c __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len) · line 12616
Quoted 3 lines
net/core/filter.c +3
-0+/* icmpv6_send may treat skb_dst as rt6_info. */ +if (skb_metadata_dst (skb)) +return -ENETUNREACH ;A bit confused about this. Which part of
icmpv6_sendtreatsskb_dstasrt6_info? (I see the original sashiko report about dst, but icmp6 seems to be not requiring it)Yeah I was also a bit confused because this came out of nowhere as soon as I put the
skb_valid_dstonly on the IPv4 path (for different reasons), but there is actually a potential trace in which we have type confusion indeed:-
icmp6_send()checks scoped source addresses and callsicmp6_iif()atnet/ipv6/icmp.c:702
-icmp6_iif()callsicmp6_dev()atnet/ipv6/icmp.c:441
-icmp6_dev()doesskb_rt6_info(skb)for loopback/L3 master devices atnet/ipv6/icmp.c:428
-skb_rt6_info()casts any non-NULL dst to structrt6_infoatinclude/net/ip6_route.h:233
-rt6->rt6i_idevis then dereferenced atnet/ipv6/icmp.c:434When checking with pahole, we can find this on my local kernel:
struct
rt6_info{
structdst_entrydst; /* 0 136 */
/* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */
structfib6_info* from; /* 136 8 */
int sernum; /* 144 4 */
struct rt6keyrt6i_dst; /* 148 20 */
struct rt6keyrt6i_src; /* 168 20 */
structin6_addrrt6i_gateway; /* 188 16 *//* XXX 4 bytes hole, try to pack */
/* --- cacheline 3 boundary (192 bytes) was 16 bytes ago --- */
structinet6_dev*rt6i_idev; /* 208 8 */ <--- we dereference this
u32rt6i_flags; /* 216 4 */
short unsigned intrt6i_nfheader_len; /* 220 2 *//* size: 224, cachelines: 4, members: 9 */ /* sum members: 218, holes: 1, sum holes: 4 */ /* padding: 2 */
/* last cacheline: 32 bytes */
};And the
metadata_dstwould look like this:struct metadata_dst { struct dst_entry dst; /* 0 136 */ /* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */ enum metadata_type type; /* 136 4 */ /* XXX 4 bytes hole, try to pack */ union { struct ip_tunnel_info tun_info; /* 144 96 */ struct hw_port_info port_info; /* 144 16 */ struct macsec_info macsec_info; /* 144 8 */ struct xfrm_md_info xfrm_info; /* 144 16 */ } u; /* 144 96 */ <--- we land on this union /* size: 240, cachelines: 4, members: 3 */ /* sum members: 236, holes: 1, sum holes: 4 */ /* last cacheline: 48 bytes */ };Let's say it's a struct
ip_tunnel_info:struct ip_tunnel_info { struct ip_tunnel_key key; /* 0 64 */ /* XXX last struct has 7 bytes of padding */ /* --- cacheline 1 boundary (64 bytes) --- */ struct ip_tunnel_encap encap; /* 64 8 */ <--- 144 + 64 = 208 we land here struct dst_cache dst_cache; /* 72 16 */ u8 options_len; /* 88 1 */ u8 mode; /* 89 1 */ /* size: 96, cachelines: 2, members: 5 */ /* padding: 6 */ /* paddings: 1, sum paddings: 7 */ /* last cacheline: 32 bytes */ };
Quoted 3 lines
So I imagine this is fairly tricky to trigger but still a case of type confusion. I have actually no idea how likely this can happen from my call but the trace makes sense at least.
That logic seems to exist for the icmp6_send to find the input device (since the expected use-case for calling icmp6_send is to the incoming skb). And since you're mainly doing egress, I don't think this path will ever trigger (iow the check is not needed)?
Maybe you can add cgroup_ingress test case? Looks like this rt6_info path might trigger for ipv6 lo? I don't see any ingress test in your series, so might be good to have one regardless?
Re: [PATCH bpf-next v10 1/5] bpf: add bpf_icmp_send kfunc
Quoted 136 lines · Stanislav Fomichev
Quoted 131 lines · Mahe Tardy
Quoted 1 line · Stanislav Fomichev
On 06/25, Mahe Tardy wrote:
[...]
Quoted 43 lines
Quoted 40 lines
net/core/filter.c +40
-0+__bpf_kfunc int bpf_icmp_send(struct __sk_buff *skb_ctx, int type, int code) +{ +struct sk_buff *skb = (struct sk_buff *)skb_ctx; +struct sk_buff *nskb; +struct sock *sk; + + sk =skb_to_full_sk (skb); +if (sk && sk->sk_kern_sock && + (sk->sk_protocol ==IPPROTO_ICMP || sk->sk_protocol ==IPPROTO_ICMPV6 )) +return -EBUSY ; + +switch (skb->protocol) { +#if IS_ENABLED (CONFIG_INET ) +case htons (ETH_P_IP ): { +if (type !=ICMP_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >NR_ICMP_UNREACH || + code ==ICMP_FRAG_NEEDED )/* needs a valid next-hop MTU */ +return -EINVAL ; + +/* icmp_send expects skb_dst to be a real rtable. */ +if (!skb_valid_dst (skb)) +return -ENETUNREACH ; + + nskb =skb_clone (skb,GFP_ATOMIC ); +if (!nskb) +return -ENOMEM ; + +memset (IPCB (nskb),0 ,sizeof (*IPCB (nskb))); +icmp_send (nskb, type, code,0 ); +consume_skb (nskb); +break ; + } +#endif +#if IS_ENABLED (CONFIG_IPV6 ) +case htons (ETH_P_IPV6 ): +if (type !=ICMPV6_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >ICMPV6_REJECT_ROUTE ) +return -EINVAL ;[..]
Quoted 7 lines
net/core/filter.c __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len) · line 12616
Quoted 3 lines
net/core/filter.c +3
-0+/* icmpv6_send may treat skb_dst as rt6_info. */ +if (skb_metadata_dst (skb)) +return -ENETUNREACH ;A bit confused about this. Which part of
icmpv6_sendtreatsskb_dstasrt6_info? (I see the original sashiko report about dst, but icmp6 seems to be not requiring it)Yeah I was also a bit confused because this came out of nowhere as soon as I put the
skb_valid_dstonly on the IPv4 path (for different reasons), but there is actually a potential trace in which we have type confusion indeed:-
icmp6_send()checks scoped source addresses and callsicmp6_iif()atnet/ipv6/icmp.c:702
-icmp6_iif()callsicmp6_dev()atnet/ipv6/icmp.c:441
-icmp6_dev()doesskb_rt6_info(skb)for loopback/L3 master devices atnet/ipv6/icmp.c:428
-skb_rt6_info()casts any non-NULL dst to structrt6_infoatinclude/net/ip6_route.h:233
-rt6->rt6i_idevis then dereferenced atnet/ipv6/icmp.c:434When checking with pahole, we can find this on my local kernel:
struct
rt6_info{
structdst_entrydst; /* 0 136 */
/* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */
structfib6_info* from; /* 136 8 */
int sernum; /* 144 4 */
struct rt6keyrt6i_dst; /* 148 20 */
struct rt6keyrt6i_src; /* 168 20 */
structin6_addrrt6i_gateway; /* 188 16 *//* XXX 4 bytes hole, try to pack */
/* --- cacheline 3 boundary (192 bytes) was 16 bytes ago --- */
structinet6_dev*rt6i_idev; /* 208 8 */ <--- we dereference this
u32rt6i_flags; /* 216 4 */
short unsigned intrt6i_nfheader_len; /* 220 2 *//* size: 224, cachelines: 4, members: 9 */ /* sum members: 218, holes: 1, sum holes: 4 */ /* padding: 2 */
/* last cacheline: 32 bytes */
};And the
metadata_dstwould look like this:struct metadata_dst { struct dst_entry dst; /* 0 136 */ /* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */ enum metadata_type type; /* 136 4 */ /* XXX 4 bytes hole, try to pack */ union { struct ip_tunnel_info tun_info; /* 144 96 */ struct hw_port_info port_info; /* 144 16 */ struct macsec_info macsec_info; /* 144 8 */ struct xfrm_md_info xfrm_info; /* 144 16 */ } u; /* 144 96 */ <--- we land on this union /* size: 240, cachelines: 4, members: 3 */ /* sum members: 236, holes: 1, sum holes: 4 */ /* last cacheline: 48 bytes */ };Let's say it's a struct
ip_tunnel_info:struct ip_tunnel_info { struct ip_tunnel_key key; /* 0 64 */ /* XXX last struct has 7 bytes of padding */ /* --- cacheline 1 boundary (64 bytes) --- */ struct ip_tunnel_encap encap; /* 64 8 */ <--- 144 + 64 = 208 we land here struct dst_cache dst_cache; /* 72 16 */ u8 options_len; /* 88 1 */ u8 mode; /* 89 1 */ /* size: 96, cachelines: 2, members: 5 */ /* padding: 6 */ /* paddings: 1, sum paddings: 7 */ /* last cacheline: 32 bytes */ };Quoted 3 lines
So I imagine this is fairly tricky to trigger but still a case of type confusion. I have actually no idea how likely this can happen from my call but the trace makes sense at least.
Quoted 8 lines
That logic seems to exist for the
icmp6_sendto find the input device (since the expected use-case for callingicmp6_sendis to the incoming skb). And since you're mainly doing egress, I don't think this path will ever trigger (iow the check is not needed)?Maybe you can add
cgroup_ingresstest case? Looks like thisrt6_infopath might trigger for ipv6 lo? I don't see any ingress test in your series, so might be good to have one regardless?
The initial reason I added only egress is because the use case of this makes more sense if that's your local kernel giving you feedback about a connection you are trying to establish, as a process, but is prevented.
But indeed, I could extend the test to ingress as well, I'd just like ideally getting an ack from networking maintainers since this is already v10 of this, before making some new changes.
Daniel Borkmann
maintainerRe: [PATCH bpf-next v10 1/5] bpf: add bpf_icmp_send kfunc
Quoted 146 lines · Mahe Tardy
Quoted 136 lines · Stanislav Fomichev
Quoted 131 lines · Mahe Tardy
Quoted 1 line · Stanislav Fomichev
On 06/25, Mahe Tardy wrote:
[...]
Quoted 43 lines
Quoted 40 lines
net/core/filter.c +40
-0+__bpf_kfunc int bpf_icmp_send(struct __sk_buff *skb_ctx, int type, int code) +{ +struct sk_buff *skb = (struct sk_buff *)skb_ctx; +struct sk_buff *nskb; +struct sock *sk; + + sk =skb_to_full_sk (skb); +if (sk && sk->sk_kern_sock && + (sk->sk_protocol ==IPPROTO_ICMP || sk->sk_protocol ==IPPROTO_ICMPV6 )) +return -EBUSY ; + +switch (skb->protocol) { +#if IS_ENABLED (CONFIG_INET ) +case htons (ETH_P_IP ): { +if (type !=ICMP_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >NR_ICMP_UNREACH || + code ==ICMP_FRAG_NEEDED )/* needs a valid next-hop MTU */ +return -EINVAL ; + +/* icmp_send expects skb_dst to be a real rtable. */ +if (!skb_valid_dst (skb)) +return -ENETUNREACH ; + + nskb =skb_clone (skb,GFP_ATOMIC ); +if (!nskb) +return -ENOMEM ; + +memset (IPCB (nskb),0 ,sizeof (*IPCB (nskb))); +icmp_send (nskb, type, code,0 ); +consume_skb (nskb); +break ; + } +#endif +#if IS_ENABLED (CONFIG_IPV6 ) +case htons (ETH_P_IPV6 ): +if (type !=ICMPV6_DEST_UNREACH ) +return -EOPNOTSUPP ; +if (code <0 || code >ICMPV6_REJECT_ROUTE ) +return -EINVAL ;[..]
Quoted 7 lines
net/core/filter.c __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len) · line 12616
Quoted 3 lines
net/core/filter.c +3
-0+/* icmpv6_send may treat skb_dst as rt6_info. */ +if (skb_metadata_dst (skb)) +return -ENETUNREACH ;A bit confused about this. Which part of
icmpv6_sendtreatsskb_dstasrt6_info? (I see the original sashiko report about dst, but icmp6 seems to be not requiring it)Yeah I was also a bit confused because this came out of nowhere as soon as I put the
skb_valid_dstonly on the IPv4 path (for different reasons), but there is actually a potential trace in which we have type confusion indeed:-
icmp6_send()checks scoped source addresses and callsicmp6_iif()atnet/ipv6/icmp.c:702
-icmp6_iif()callsicmp6_dev()atnet/ipv6/icmp.c:441
-icmp6_dev()doesskb_rt6_info(skb)for loopback/L3 master devices atnet/ipv6/icmp.c:428
-skb_rt6_info()casts any non-NULL dst to structrt6_infoatinclude/net/ip6_route.h:233
-rt6->rt6i_idevis then dereferenced atnet/ipv6/icmp.c:434When checking with pahole, we can find this on my local kernel:
struct
rt6_info{
structdst_entrydst; /* 0 136 */
/* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */
structfib6_info* from; /* 136 8 */
int sernum; /* 144 4 */
struct rt6keyrt6i_dst; /* 148 20 */
struct rt6keyrt6i_src; /* 168 20 */
structin6_addrrt6i_gateway; /* 188 16 *//* XXX 4 bytes hole, try to pack */
/* --- cacheline 3 boundary (192 bytes) was 16 bytes ago --- */
structinet6_dev*rt6i_idev; /* 208 8 */ <--- we dereference this
u32rt6i_flags; /* 216 4 */
short unsigned intrt6i_nfheader_len; /* 220 2 *//* size: 224, cachelines: 4, members: 9 */ /* sum members: 218, holes: 1, sum holes: 4 */ /* padding: 2 */
/* last cacheline: 32 bytes */
};And the
metadata_dstwould look like this:struct metadata_dst { struct dst_entry dst; /* 0 136 */ /* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */ enum metadata_type type; /* 136 4 */ /* XXX 4 bytes hole, try to pack */ union { struct ip_tunnel_info tun_info; /* 144 96 */ struct hw_port_info port_info; /* 144 16 */ struct macsec_info macsec_info; /* 144 8 */ struct xfrm_md_info xfrm_info; /* 144 16 */ } u; /* 144 96 */ <--- we land on this union /* size: 240, cachelines: 4, members: 3 */ /* sum members: 236, holes: 1, sum holes: 4 */ /* last cacheline: 48 bytes */ };Let's say it's a struct
ip_tunnel_info:struct ip_tunnel_info { struct ip_tunnel_key key; /* 0 64 */ /* XXX last struct has 7 bytes of padding */ /* --- cacheline 1 boundary (64 bytes) --- */ struct ip_tunnel_encap encap; /* 64 8 */ <--- 144 + 64 = 208 we land here struct dst_cache dst_cache; /* 72 16 */ u8 options_len; /* 88 1 */ u8 mode; /* 89 1 */ /* size: 96, cachelines: 2, members: 5 */ /* padding: 6 */ /* paddings: 1, sum paddings: 7 */ /* last cacheline: 32 bytes */ };Quoted 3 lines
So I imagine this is fairly tricky to trigger but still a case of type confusion. I have actually no idea how likely this can happen from my call but the trace makes sense at least.
Quoted 8 lines
That logic seems to exist for the
icmp6_sendto find the input device (since the expected use-case for callingicmp6_sendis to the incoming skb). And since you're mainly doing egress, I don't think this path will ever trigger (iow the check is not needed)?Maybe you can add
cgroup_ingresstest case? Looks like thisrt6_infopath might trigger for ipv6 lo? I don't see any ingress test in your series, so might be good to have one regardless?
Quoted 7 lines
The initial reason I added only egress is because the use case of this makes more sense if that's your local kernel giving you feedback about a connection you are trying to establish, as a process, but is prevented.
But indeed, I could extend the test to ingress as well, I'd just like ideally getting an ack from networking maintainers since this is already v10 of this, before making some new changes.
Sry for the delay, Mahe. Not speaking for net maintainers, but if the skb_metadata_dst() test in IPv6 would be made more strict and align with IPv4 by erroring out on !skb_valid_dst(skb), would this work? It would reject NULL dst for IPv6 case when someone would push a synthetic TEST_RUN skb to spoofe an icmpv6 injection without CAP_NET_ADMIN fwiw. Otherwise lgtm.
Thanks,
Daniel
PATCH v10 2/5 selftests/bpf: add bpf_icmp_send kfunc cgroup_skb tests
In reply to Mahe TardyThis test opens a server and client, enters a new cgroup, attach a cgroup_skb program on egress and calls the bpf_icmp_send function from the client egress so that an ICMP unreach control message is sent back to the client. It then fetches the message from the error queue to confirm the correct ICMP unreach code has been sent.
Note that, for the client, we have to connect in non-blocking mode to let the test execute faster. Otherwise, we need to wait for the TCP three-way handshake to timeout in the kernel before reading the errno.
Also note that we don't set IP_RECVERR on the socket in connect_to_fd_nonblock since the error will be transferred anyway in our test because the connection is rejected at the beginning of the TCP handshake. See in net/ipv4/tcp_ipv4.c:tcp_v4_err for more details.
--- .../bpf/prog_tests/icmp_send_kfunc.c | 164 ++++++++++++++++++ tools/testing/selftests/bpf/progs/icmp_send.c | 38 ++++ 2 files changed, 202 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/icmp_send_kfunc.c create mode 100644 tools/testing/selftests/bpf/progs/icmp_send.c
tools/testing/selftests/bpf/prog_tests/icmp_send_kfunc.c new file +164 -0
@@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include <network_helpers.h> +#include <linux/errqueue.h> +#include <poll.h> +#include "icmp_send.skel.h" + +#define TIMEOUT_MS 1000 + +#define ICMP_DEST_UNREACH 3 + +#define ICMP_FRAG_NEEDED 4 +#define NR_ICMP_UNREACH 15 + +#define KFUNC_RET_UNSET -1 + +static int connect_to_fd_nonblock (int server_fd) +{ +struct sockaddr_storage addr; +socklen_t len =sizeof (addr); +int fd, err; + +if (getsockname (server_fd, (struct sockaddr *)&addr, &len)) +return -1 ; + + fd =socket (addr.ss_family,SOCK_STREAM |SOCK_NONBLOCK ,0 ); +if (fd <0 ) +return -1 ; + + err =connect (fd, (struct sockaddr *)&addr, len); +if (err <0 && errno !=EINPROGRESS ) { +close (fd); +return -1 ; + } + +return fd; +} + +static void read_icmp_errqueue (int sockfd,int expected_code) +{ +struct sock_extended_err *sock_err; +char ctrl_buf[512 ]; +struct msghdr msg = { + .msg_control = ctrl_buf, + .msg_controllen =sizeof (ctrl_buf), + }; +struct pollfd pfd = { + .fd = sockfd, + .events =POLLERR , + }; +struct cmsghdr *cm; +ssize_t n; + +if (!ASSERT_GE (poll (&pfd,1 ,TIMEOUT_MS ),1 ,"poll_errqueue" )) +return ; + + n =recvmsg (sockfd, &msg,MSG_ERRQUEUE ); +if (!ASSERT_GE (n,0 ,"recvmsg_errqueue" )) +return ; + + cm =CMSG_FIRSTHDR (&msg); +if (!ASSERT_NEQ (cm,NULL ,"cm_firsthdr_null" )) +return ; + +for (; cm; cm =CMSG_NXTHDR (&msg, cm)) { +if (cm->cmsg_level !=IPPROTO_IP || cm->cmsg_type !=IP_RECVERR ) +continue ; + + sock_err = (struct sock_extended_err *)CMSG_DATA (cm); + +if (!ASSERT_EQ (sock_err->ee_origin,SO_EE_ORIGIN_ICMP , +"sock_err_origin_icmp" )) +return ; +if (!ASSERT_EQ (sock_err->ee_type,ICMP_DEST_UNREACH , +"sock_err_type_dest_unreach" )) +return ; +ASSERT_EQ (sock_err->ee_code, expected_code,"sock_err_code" ); +return ; + } + +ASSERT_FAIL ("no IP_RECVERR control message found" ); +} + +static bool valid_unreach_code (int code) +{ +if (code <0 ) +return false; + +return code <=NR_ICMP_UNREACH && code !=ICMP_FRAG_NEEDED ; +} + +static void trigger_prog_read_icmp_errqueue (struct icmp_send *skel,int code) +{ +int srv_fd =-1 , client_fd =-1 ; +int port; + + srv_fd =start_server (AF_INET ,SOCK_STREAM ,"127.0.0.1" ,0 ,TIMEOUT_MS ); +if (!ASSERT_OK_FD (srv_fd,"start_server" )) +return ; + + port =get_socket_local_port (srv_fd); +if (!ASSERT_GE (port,0 ,"get_socket_local_port" )) { +close (srv_fd); +return ; + } + + skel->bss->server_port =ntohs (port); + skel->bss->unreach_code = code; + skel->data->kfunc_ret =KFUNC_RET_UNSET ; + + client_fd =connect_to_fd_nonblock (srv_fd); +if (!ASSERT_OK_FD (client_fd,"client_connect_nonblock" )) { +close (srv_fd); +return ; + } + +if (valid_unreach_code (code)) +read_icmp_errqueue (client_fd, code); + +close (client_fd); +close (srv_fd); +} + +void test_icmp_send_unreach_cgroup (void ) +{ +struct icmp_send *skel; +int cgroup_fd =-1 ; + + skel =icmp_send__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open" )) + goto cleanup; + + cgroup_fd =test__join_cgroup ("/icmp_send_unreach_cgroup" ); +if (!ASSERT_OK_FD (cgroup_fd,"join_cgroup" )) + goto cleanup; + + skel->links.egress = +bpf_program__attach_cgroup (skel->progs.egress, cgroup_fd); +if (!ASSERT_OK_PTR (skel->links.egress,"prog_attach_cgroup" )) + goto cleanup; + +for (int code =0 ; code <=NR_ICMP_UNREACH ; code++) { +if (code ==ICMP_FRAG_NEEDED ) +continue ; + +trigger_prog_read_icmp_errqueue (skel, code); +ASSERT_EQ (skel->data->kfunc_ret,0 ,"kfunc_ret" ); + } + +/* Test invalid codes */ +trigger_prog_read_icmp_errqueue (skel,-1 ); +ASSERT_EQ (skel->data->kfunc_ret, -EINVAL ,"kfunc_ret" ); + +trigger_prog_read_icmp_errqueue (skel,NR_ICMP_UNREACH +1 ); +ASSERT_EQ (skel->data->kfunc_ret, -EINVAL ,"kfunc_ret" ); + +trigger_prog_read_icmp_errqueue (skel,ICMP_FRAG_NEEDED ); +ASSERT_EQ (skel->data->kfunc_ret, -EINVAL ,"kfunc_ret" ); + +cleanup: +icmp_send__destroy (skel); +if (cgroup_fd >=0 ) +close (cgroup_fd); +}
tools/testing/selftests/bpf/progs/icmp_send.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_endian.h> + +/* 127.0.0.1 in host byte order */ +#define SERVER_IP 0x7F000001 + +#define ICMP_DEST_UNREACH 3 + +__u16 server_port =0 ; +int unreach_code =0 ; +int kfunc_ret =-1 ; + +SEC("cgroup_skb/egress") +int egress (struct __sk_buff *skb) +{ +void *data = (void *)(long )skb->data; +void *data_end = (void *)(long )skb->data_end; +struct iphdr *iph; +struct tcphdr *tcph; + + iph = data; +if ((void *)(iph +1 ) > data_end || iph->version !=4 || + iph->protocol !=IPPROTO_TCP || iph->daddr !=bpf_htonl (SERVER_IP )) +return SK_PASS ; + + tcph = (void *)iph + iph->ihl *4 ; +if ((void *)(tcph +1 ) > data_end || + tcph->dest !=bpf_htons (server_port)) +return SK_PASS ; + + kfunc_ret =bpf_icmp_send (skb,ICMP_DEST_UNREACH , unreach_code); + +return SK_DROP ; +} + +char LICENSE[] SEC("license") = "Dual BSD/GPL";
--
2.34.1
PATCH v10 3/5 selftests/bpf: add bpf_icmp_send kfunc cgroup_skb IPv6 tests
In reply to Mahe TardyThis test extends the existing cgroup_skb tests with IPv6 support.
Note that we need to set IPV6_RECVERR on the socket for IPv6 in connect_to_fd_nonblock otherwise the error will be ignored even if we are in the middle of the TCP handshake. See in net/ipv6/datagram.c:ipv6_icmp_error for more details.
--- .../bpf/prog_tests/icmp_send_kfunc.c | 91 +++++++++++++------ tools/testing/selftests/bpf/progs/icmp_send.c | 48 ++++++++-- 2 files changed, 101 insertions(+), 38 deletions(-)
tools/testing/selftests/bpf/prog_tests/icmp_send_kfunc.c +62 -29
@@ -8,9 +8,11 @@#define TIMEOUT_MS 1000#define ICMP_DEST_UNREACH 3 +#define ICMPV6_DEST_UNREACH 1#define ICMP_FRAG_NEEDED 4#define NR_ICMP_UNREACH 15 +#define ICMPV6_REJECT_ROUTE 6#define KFUNC_RET_UNSET -1
@@ -18,7 +20,7 @@ static int connect_to_fd_nonblock(int server_fd) {struct sockaddr_storage addr;socklen_t len =sizeof (addr); -int fd, err; +int fd, err, on =1 ;if (getsockname (server_fd, (struct sockaddr *)&addr, &len))return -1 ;
@@ -27,6 +29,12 @@ static int connect_to_fd_nonblock(int server_fd)if (fd <0 )return -1 ; +if (addr.ss_family ==AF_INET6 && +setsockopt (fd,IPPROTO_IPV6 ,IPV6_RECVERR , &on,sizeof (on)) <0 ) { +close (fd); +return -1 ; + } + err =connect (fd, (struct sockaddr *)&addr, len);if (err <0 && errno !=EINPROGRESS ) {close (fd);
@@ -36,8 +44,14 @@ static int connect_to_fd_nonblock(int server_fd)return fd; } -static void read_icmp_errqueue (int sockfd,int expected_code) +static void read_icmp_errqueue (int sockfd,int expected_code,int af) { +int expected_ee_type = (af ==AF_INET ) ?ICMP_DEST_UNREACH : +ICMPV6_DEST_UNREACH ; +int expected_origin = (af ==AF_INET ) ?SO_EE_ORIGIN_ICMP : +SO_EE_ORIGIN_ICMP6 ; +int expected_level = (af ==AF_INET ) ?IPPROTO_IP :IPPROTO_IPV6 ; +int expected_type = (af ==AF_INET ) ?IP_RECVERR :IPV6_RECVERR ;struct sock_extended_err *sock_err;char ctrl_buf[512 ];struct msghdr msg = {
@@ -63,38 +77,43 @@ static void read_icmp_errqueue(int sockfd, int expected_code)return ;for (; cm; cm =CMSG_NXTHDR (&msg, cm)) { -if (cm->cmsg_level !=IPPROTO_IP || cm->cmsg_type !=IP_RECVERR ) +if (cm->cmsg_level != expected_level || + cm->cmsg_type != expected_type)continue ; sock_err = (struct sock_extended_err *)CMSG_DATA (cm); -if (!ASSERT_EQ (sock_err->ee_origin,SO_EE_ORIGIN_ICMP , -"sock_err_origin_icmp" )) +if (!ASSERT_EQ (sock_err->ee_origin, expected_origin, +"sock_err_origin" ))return ; -if (!ASSERT_EQ (sock_err->ee_type,ICMP_DEST_UNREACH , +if (!ASSERT_EQ (sock_err->ee_type, expected_ee_type,"sock_err_type_dest_unreach" ))return ;ASSERT_EQ (sock_err->ee_code, expected_code,"sock_err_code" );return ; } -ASSERT_FAIL ("no IP_RECVERR control message found" ); +ASSERT_FAIL ("no IP_RECVERR/IPV6_RECVERR control message found" ); } -static bool valid_unreach_code (int code) +static bool valid_unreach_code (int code,int af) {if (code <0 )return false; -return code <=NR_ICMP_UNREACH && code !=ICMP_FRAG_NEEDED ; +if (af ==AF_INET ) +return code <=NR_ICMP_UNREACH && code !=ICMP_FRAG_NEEDED ; + +return code <=ICMPV6_REJECT_ROUTE ; } -static void trigger_prog_read_icmp_errqueue (struct icmp_send *skel,int code) +static void trigger_prog_read_icmp_errqueue (struct icmp_send *skel,int code, +int af,const char *ip) {int srv_fd =-1 , client_fd =-1 ;int port; - srv_fd =start_server (AF_INET ,SOCK_STREAM ,"127.0.0.1" ,0 ,TIMEOUT_MS ); + srv_fd =start_server (af,SOCK_STREAM , ip,0 ,TIMEOUT_MS );if (!ASSERT_OK_FD (srv_fd,"start_server" ))return ;
@@ -105,6 +124,8 @@ static void trigger_prog_read_icmp_errqueue(struct icmp_send *skel, int code) } skel->bss->server_port =ntohs (port); + skel->bss->unreach_type = (af ==AF_INET ) ?ICMP_DEST_UNREACH : +ICMPV6_DEST_UNREACH ; skel->bss->unreach_code = code; skel->data->kfunc_ret =KFUNC_RET_UNSET ;
@@ -114,13 +135,37 @@ static void trigger_prog_read_icmp_errqueue(struct icmp_send *skel, int code)return ; } -if (valid_unreach_code (code)) -read_icmp_errqueue (client_fd, code); +if (valid_unreach_code (code, af)) +read_icmp_errqueue (client_fd, code, af);close (client_fd);close (srv_fd); } +static void run_icmp_test (struct icmp_send *skel,int af,const char *ip, +int max_code) +{ +for (int code =0 ; code <= max_code; code++) { +if (af ==AF_INET && code ==ICMP_FRAG_NEEDED ) +continue ; + +trigger_prog_read_icmp_errqueue (skel, code, af, ip); +ASSERT_EQ (skel->data->kfunc_ret,0 ,"kfunc_ret" ); + } + +/* Test invalid codes */ +trigger_prog_read_icmp_errqueue (skel,-1 , af, ip); +ASSERT_EQ (skel->data->kfunc_ret, -EINVAL ,"kfunc_ret" ); + +trigger_prog_read_icmp_errqueue (skel, max_code +1 , af, ip); +ASSERT_EQ (skel->data->kfunc_ret, -EINVAL ,"kfunc_ret" ); + +if (af ==AF_INET ) { +trigger_prog_read_icmp_errqueue (skel,ICMP_FRAG_NEEDED , af, ip); +ASSERT_EQ (skel->data->kfunc_ret, -EINVAL ,"kfunc_ret" ); + } +} +void test_icmp_send_unreach_cgroup (void ) {struct icmp_send *skel;
@@ -139,23 +184,11 @@ void test_icmp_send_unreach_cgroup(void)if (!ASSERT_OK_PTR (skel->links.egress,"prog_attach_cgroup" )) goto cleanup; -for (int code =0 ; code <=NR_ICMP_UNREACH ; code++) { -if (code ==ICMP_FRAG_NEEDED ) -continue ; - -trigger_prog_read_icmp_errqueue (skel, code); -ASSERT_EQ (skel->data->kfunc_ret,0 ,"kfunc_ret" ); - } - -/* Test invalid codes */ -trigger_prog_read_icmp_errqueue (skel,-1 ); -ASSERT_EQ (skel->data->kfunc_ret, -EINVAL ,"kfunc_ret" ); +if (test__start_subtest ("ipv4" )) +run_icmp_test (skel,AF_INET ,"127.0.0.1" ,NR_ICMP_UNREACH ); -trigger_prog_read_icmp_errqueue (skel,NR_ICMP_UNREACH +1 ); -ASSERT_EQ (skel->data->kfunc_ret, -EINVAL ,"kfunc_ret" ); - -trigger_prog_read_icmp_errqueue (skel,ICMP_FRAG_NEEDED ); -ASSERT_EQ (skel->data->kfunc_ret, -EINVAL ,"kfunc_ret" ); +if (test__start_subtest ("ipv6" )) +run_icmp_test (skel,AF_INET6 ,"::1" ,ICMPV6_REJECT_ROUTE ); cleanup:icmp_send__destroy (skel);
tools/testing/selftests/bpf/progs/icmp_send.c +39 -9
68 lines not on this page · raw message
--
2.34.1
PATCH v10 4/5 selftests/bpf: add bpf_icmp_send recursion test
In reply to Mahe TardyThis test is similar to test_icmp_send_unreach_cgroup but checks that, in case of recursion, meaning that the BPF program calling the kfunc was re-triggered by the icmp_send done by the kfunc, the kfunc will stop early and return -EBUSY.
The test attaches to the root cgroup to ensure the ICMP packet generated by the kfunc re-triggers the BPF program.
--- .../bpf/prog_tests/icmp_send_kfunc.c | 46 ++++++++++++++++ tools/testing/selftests/bpf/progs/icmp_send.c | 55 +++++++++++++++++++ 2 files changed, 101 insertions(+)
tools/testing/selftests/bpf/prog_tests/icmp_send_kfunc.c +46 -0
@@ -1,8 +1,10 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> #include <network_helpers.h> +#include <cgroup_helpers.h> #include <linux/errqueue.h> #include <poll.h> +#include <unistd.h> #include "icmp_send.skel.h" #define TIMEOUT_MS 1000
@@ -10,6 +12,7 @@#define ICMP_DEST_UNREACH 3#define ICMPV6_DEST_UNREACH 1 +#define ICMP_HOST_UNREACH 1#define ICMP_FRAG_NEEDED 4#define NR_ICMP_UNREACH 15#define ICMPV6_REJECT_ROUTE 6
@@ -195,3 +198,46 @@ void test_icmp_send_unreach_cgroup(void)if (cgroup_fd >=0 )close (cgroup_fd); } + +void test_icmp_send_unreach_recursion (void ) +{ +struct icmp_send *skel; +int cgroup_fd =-1 ; +int err; + + err =setup_cgroup_environment (); +if (!ASSERT_OK (err,"setup_cgroup_environment" )) +return ; + + skel =icmp_send__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open" )) + goto cleanup; + + cgroup_fd =get_root_cgroup (); +if (!ASSERT_OK_FD (cgroup_fd,"get_root_cgroup" )) + goto cleanup; + + skel->data->target_pid =getpid (); + skel->links.recursion = +bpf_program__attach_cgroup (skel->progs.recursion, cgroup_fd); +if (!ASSERT_OK_PTR (skel->links.recursion,"prog_attach_cgroup" )) + goto cleanup; + +trigger_prog_read_icmp_errqueue (skel,ICMP_HOST_UNREACH ,AF_INET , +"127.0.0.1" ); + +/* +* Because there's recursion involved, the first call will return at +* index 1 since it will return the second, and the second call will +* return at index 0 since it will return the first. +*/ +ASSERT_EQ (skel->bss->rec_count,2 ,"rec_count" ); +ASSERT_EQ (skel->data->rec_kfunc_rets[0 ], -EBUSY ,"kfunc_rets[0]" ); +ASSERT_EQ (skel->data->rec_kfunc_rets[1 ],0 ,"kfunc_rets[1]" ); + +cleanup: +icmp_send__destroy (skel); +if (cgroup_fd >=0 ) +close (cgroup_fd); +cleanup_cgroup_environment (); +}
tools/testing/selftests/bpf/progs/icmp_send.c +55 -0
65 lines not on this page · raw message
--
2.34.1
PATCH v10 5/5 selftests/bpf: add bpf_icmp_send no route test
In reply to Mahe TardyFor normal live cgroup_skb paths, the skb should already be routed. The exception is for test run via BPF_PROG_TEST_RUN with packets created via bpf_prog_test_run_skb. Those lack dst route and thus the icmp_send would quietly fail by returning early.
This test exercises this and makes sure the kfunc returns -ENETUNREACH.
--- .../bpf/prog_tests/icmp_send_kfunc.c | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+)
tools/testing/selftests/bpf/prog_tests/icmp_send_kfunc.c +26 -0
@@ -169,6 +169,29 @@ static void run_icmp_test(struct icmp_send *skel, int af, const char *ip, } } +static void run_icmp_no_route_test (struct icmp_send *skel) +{ +struct ipv4_packet pkt = pkt_v4; +LIBBPF_OPTS (bpf_test_run_opts, opts, + .data_in = &pkt, + .data_size_in = sizeof(pkt), + ); +int err; + + pkt.iph.version =4 ; + pkt.iph.daddr =inet_addr ("127.0.0.1" ); + pkt.tcp.dest =htons (80 ); + skel->bss->server_port =80 ; + skel->bss->unreach_type =ICMP_DEST_UNREACH ; + skel->bss->unreach_code =ICMP_HOST_UNREACH ; + skel->data->kfunc_ret =KFUNC_RET_UNSET ; + + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.egress), &opts); +if (!ASSERT_OK (err,"test_run" )) +return ; +ASSERT_EQ (skel->data->kfunc_ret, -ENETUNREACH ,"kfunc_ret_no_route" ); +} +void test_icmp_send_unreach_cgroup (void ) {struct icmp_send *skel;
@@ -193,6 +216,9 @@ void test_icmp_send_unreach_cgroup(void)if (test__start_subtest ("ipv6" ))run_icmp_test (skel,AF_INET6 ,"::1" ,ICMPV6_REJECT_ROUTE ); +if (test__start_subtest ("no_route" )) +run_icmp_no_route_test (skel); + cleanup:icmp_send__destroy (skel);if (cgroup_fd >=0 )
--
2.34.1