Skip to content

Commit 45ee629

Browse files
jrfastabkernel-patches-bot
authored andcommitted
bpf, sockmap: add skb_adjust_room to pop bytes off ingress payload
This implements a new helper skb_adjust_room() so users can push/pop extra bytes from a BPF_SK_SKB_STREAM_VERDICT program. Some protocols may include headers and other information that we may not want to include when doing a redirect from a BPF_SK_SKB_STREAM_VERDICT program. One use case is to redirect TLS packets into a receive socket that doesn't expect TLS data. In TLS case the first 13B or so contain the protocol header. With KTLS the payload is decrypted so we should be able to redirect this to a receiving socket, but the receiving socket may not be expecting to receive a TLS header and discard the data. Using the above helper we can pop the header off and put an appropriate header on the payload. This allows for creating a proxy between protocols without extra hops through the stack or userspace. So in order to fix this case add skb_adjust_room() so users can strip the header. After this the user can strip the header and an unmodified receiver thread will work correctly when data is redirected into the ingress path of a sock. Signed-off-by: John Fastabend <[email protected]>
1 parent 18cba61 commit 45ee629

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

net/core/filter.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include <net/bpf_sk_storage.h>
7777
#include <net/transp_v6.h>
7878
#include <linux/btf_ids.h>
79+
#include <net/tls.h>
7980

8081
static const struct bpf_func_proto *
8182
bpf_sk_base_func_proto(enum bpf_func_id func_id);
@@ -3479,6 +3480,48 @@ static u32 __bpf_skb_max_len(const struct sk_buff *skb)
34793480
SKB_MAX_ALLOC;
34803481
}
34813482

3483+
BPF_CALL_4(sk_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3484+
u32, mode, u64, flags)
3485+
{
3486+
u32 len_diff_abs = abs(len_diff);
3487+
bool shrink = len_diff < 0;
3488+
int ret = 0;
3489+
3490+
if (unlikely(flags || mode))
3491+
return -EINVAL;
3492+
if (unlikely(len_diff_abs > 0xfffU))
3493+
return -EFAULT;
3494+
3495+
if (!shrink) {
3496+
ret = skb_cow(skb, len_diff);
3497+
if (unlikely(ret < 0))
3498+
return ret;
3499+
__skb_push(skb, len_diff_abs);
3500+
memset(skb->data, 0, len_diff_abs);
3501+
} else {
3502+
if (unlikely(!pskb_may_pull(skb, len_diff_abs)))
3503+
return -ENOMEM;
3504+
__skb_pull(skb, len_diff_abs);
3505+
}
3506+
bpf_compute_data_end_sk_skb(skb);
3507+
if (tls_sw_has_ctx_rx(skb->sk)) {
3508+
struct strp_msg *rxm = strp_msg(skb);
3509+
3510+
rxm->full_len += len_diff;
3511+
}
3512+
return ret;
3513+
}
3514+
3515+
static const struct bpf_func_proto sk_skb_adjust_room_proto = {
3516+
.func = sk_skb_adjust_room,
3517+
.gpl_only = false,
3518+
.ret_type = RET_INTEGER,
3519+
.arg1_type = ARG_PTR_TO_CTX,
3520+
.arg2_type = ARG_ANYTHING,
3521+
.arg3_type = ARG_ANYTHING,
3522+
.arg4_type = ARG_ANYTHING,
3523+
};
3524+
34823525
BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
34833526
u32, mode, u64, flags)
34843527
{
@@ -6745,6 +6788,7 @@ bool bpf_helper_changes_pkt_data(void *func)
67456788
func == bpf_skb_change_tail ||
67466789
func == sk_skb_change_tail ||
67476790
func == bpf_skb_adjust_room ||
6791+
func == sk_skb_adjust_room ||
67486792
func == bpf_skb_pull_data ||
67496793
func == sk_skb_pull_data ||
67506794
func == bpf_clone_redirect ||
@@ -7218,6 +7262,8 @@ sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
72187262
return &sk_skb_change_tail_proto;
72197263
case BPF_FUNC_skb_change_head:
72207264
return &sk_skb_change_head_proto;
7265+
case BPF_FUNC_skb_adjust_room:
7266+
return &sk_skb_adjust_room_proto;
72217267
case BPF_FUNC_get_socket_cookie:
72227268
return &bpf_get_socket_cookie_proto;
72237269
case BPF_FUNC_get_socket_uid:

0 commit comments

Comments
 (0)