Skip to content

Commit 60db5e7

Browse files
mtardyKernel Patches Daemon
authored andcommitted
bpf: add bpf_icmp_send_unreach cgroup_skb 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 reuse concepts from netfilter reject target codepath with the differences that: * Packets are cloned since the BPF user can still return SK_PASS from the cgroup_skb progs and the current skb need to stay untouched (cgroup_skb hooks only allow read-only skb payload). * Since cgroup_skb programs are called late in the stack, checksums do not need to be computed or verified, and IPv4 fragmentation does not need to be checked (ip_local_deliver should take care of that earlier). Signed-off-by: Mahe Tardy <[email protected]>
1 parent bf83922 commit 60db5e7

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

net/core/filter.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
#include <linux/un.h>
8686
#include <net/xdp_sock_drv.h>
8787
#include <net/inet_dscp.h>
88+
#include <linux/icmp.h>
89+
#include <net/icmp.h>
90+
#include <net/route.h>
91+
#include <net/ip6_route.h>
8892

8993
#include "dev.h"
9094

@@ -12148,6 +12152,53 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,
1214812152
return 0;
1214912153
}
1215012154

12155+
__bpf_kfunc int bpf_icmp_send_unreach(struct __sk_buff *__skb, int code)
12156+
{
12157+
struct sk_buff *skb = (struct sk_buff *)__skb;
12158+
struct sk_buff *nskb;
12159+
12160+
switch (skb->protocol) {
12161+
case htons(ETH_P_IP):
12162+
if (code < 0 || code > NR_ICMP_UNREACH)
12163+
return -EINVAL;
12164+
12165+
nskb = skb_clone(skb, GFP_ATOMIC);
12166+
if (!nskb)
12167+
return -ENOMEM;
12168+
12169+
if (ip_route_reply_fetch_dst(nskb) < 0) {
12170+
kfree_skb(nskb);
12171+
return -EHOSTUNREACH;
12172+
}
12173+
12174+
icmp_send(nskb, ICMP_DEST_UNREACH, code, 0);
12175+
kfree_skb(nskb);
12176+
break;
12177+
#if IS_ENABLED(CONFIG_IPV6)
12178+
case htons(ETH_P_IPV6):
12179+
if (code < 0 || code > ICMPV6_REJECT_ROUTE)
12180+
return -EINVAL;
12181+
12182+
nskb = skb_clone(skb, GFP_ATOMIC);
12183+
if (!nskb)
12184+
return -ENOMEM;
12185+
12186+
if (ip6_route_reply_fetch_dst(nskb) < 0) {
12187+
kfree_skb(nskb);
12188+
return -EHOSTUNREACH;
12189+
}
12190+
12191+
icmpv6_send(nskb, ICMPV6_DEST_UNREACH, code, 0);
12192+
kfree_skb(nskb);
12193+
break;
12194+
#endif
12195+
default:
12196+
return -EPROTONOSUPPORT;
12197+
}
12198+
12199+
return SK_DROP;
12200+
}
12201+
1215112202
__bpf_kfunc_end_defs();
1215212203

1215312204
int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,
@@ -12185,6 +12236,10 @@ BTF_KFUNCS_START(bpf_kfunc_check_set_sock_ops)
1218512236
BTF_ID_FLAGS(func, bpf_sock_ops_enable_tx_tstamp, KF_TRUSTED_ARGS)
1218612237
BTF_KFUNCS_END(bpf_kfunc_check_set_sock_ops)
1218712238

12239+
BTF_KFUNCS_START(bpf_kfunc_check_set_icmp_send_unreach)
12240+
BTF_ID_FLAGS(func, bpf_icmp_send_unreach, KF_TRUSTED_ARGS)
12241+
BTF_KFUNCS_END(bpf_kfunc_check_set_icmp_send_unreach)
12242+
1218812243
static const struct btf_kfunc_id_set bpf_kfunc_set_skb = {
1218912244
.owner = THIS_MODULE,
1219012245
.set = &bpf_kfunc_check_set_skb,
@@ -12210,6 +12265,11 @@ static const struct btf_kfunc_id_set bpf_kfunc_set_sock_ops = {
1221012265
.set = &bpf_kfunc_check_set_sock_ops,
1221112266
};
1221212267

12268+
static const struct btf_kfunc_id_set bpf_kfunc_set_icmp_send_unreach = {
12269+
.owner = THIS_MODULE,
12270+
.set = &bpf_kfunc_check_set_icmp_send_unreach,
12271+
};
12272+
1221312273
static int __init bpf_kfunc_init(void)
1221412274
{
1221512275
int ret;
@@ -12229,6 +12289,7 @@ static int __init bpf_kfunc_init(void)
1222912289
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
1223012290
&bpf_kfunc_set_sock_addr);
1223112291
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_tcp_reqsk);
12292+
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &bpf_kfunc_set_icmp_send_unreach);
1223212293
return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SOCK_OPS, &bpf_kfunc_set_sock_ops);
1223312294
}
1223412295
late_initcall(bpf_kfunc_init);

0 commit comments

Comments
 (0)