Skip to content

Commit 7d67234

Browse files
borkmanndavem330
authored andcommitted
bpf: add generic bpf_csum_diff helper
For L4 checksums, we currently have bpf_l4_csum_replace() helper. It's currently limited to handle 2 and 4 byte changes in a header and feeds the from/to into inet_proto_csum_replace{2,4}() helpers of the kernel. When working with IPv6, for example, this makes it rather cumbersome to deal with, similarly when editing larger parts of a header. Instead, extend the API in a more generic way: For bpf_l4_csum_replace(), add a case for header field mask of 0 to change the checksum at a given offset through inet_proto_csum_replace_by_diff(), and provide a helper bpf_csum_diff() that can generically calculate a from/to diff for arbitrary amounts of data. This can be used in multiple ways: for the bpf_l4_csum_replace() only part, this even provides us with the option to insert precalculated diffs from user space f.e. from a map, or from bpf_csum_diff() during runtime. bpf_csum_diff() has a optional from/to stack buffer input, so we can calculate a diff by using a scratchbuffer for scenarios where we're inserting (from is NULL), removing (to is NULL) or diffing (from/to buffers don't need to be of equal size) data. Also, bpf_csum_diff() allows to feed a previous csum into csum_partial(), so the function can also be cascaded. Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8e2fe1d commit 7d67234

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Diff for: include/uapi/linux/bpf.h

+11
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,17 @@ enum bpf_func_id {
287287
* Return: >= 0 stackid on success or negative error
288288
*/
289289
BPF_FUNC_get_stackid,
290+
291+
/**
292+
* bpf_csum_diff(from, from_size, to, to_size, seed) - calculate csum diff
293+
* @from: raw from buffer
294+
* @from_size: length of from buffer
295+
* @to: raw to buffer
296+
* @to_size: length of to buffer
297+
* @seed: optional seed
298+
* Return: csum result
299+
*/
300+
BPF_FUNC_csum_diff,
290301
__BPF_FUNC_MAX_ID,
291302
};
292303

Diff for: net/core/filter.c

+53
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,12 @@ static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
14911491
return -EFAULT;
14921492

14931493
switch (flags & BPF_F_HDR_FIELD_MASK) {
1494+
case 0:
1495+
if (unlikely(from != 0))
1496+
return -EINVAL;
1497+
1498+
inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1499+
break;
14941500
case 2:
14951501
inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
14961502
break;
@@ -1519,6 +1525,51 @@ const struct bpf_func_proto bpf_l4_csum_replace_proto = {
15191525
.arg5_type = ARG_ANYTHING,
15201526
};
15211527

1528+
struct bpf_csum_scratchpad {
1529+
__be32 diff[128];
1530+
};
1531+
1532+
static DEFINE_PER_CPU(struct bpf_csum_scratchpad, bpf_csum_sp);
1533+
1534+
static u64 bpf_csum_diff(u64 r1, u64 from_size, u64 r3, u64 to_size, u64 seed)
1535+
{
1536+
struct bpf_csum_scratchpad *sp = this_cpu_ptr(&bpf_csum_sp);
1537+
u64 diff_size = from_size + to_size;
1538+
__be32 *from = (__be32 *) (long) r1;
1539+
__be32 *to = (__be32 *) (long) r3;
1540+
int i, j = 0;
1541+
1542+
/* This is quite flexible, some examples:
1543+
*
1544+
* from_size == 0, to_size > 0, seed := csum --> pushing data
1545+
* from_size > 0, to_size == 0, seed := csum --> pulling data
1546+
* from_size > 0, to_size > 0, seed := 0 --> diffing data
1547+
*
1548+
* Even for diffing, from_size and to_size don't need to be equal.
1549+
*/
1550+
if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1551+
diff_size > sizeof(sp->diff)))
1552+
return -EINVAL;
1553+
1554+
for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1555+
sp->diff[j] = ~from[i];
1556+
for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1557+
sp->diff[j] = to[i];
1558+
1559+
return csum_partial(sp->diff, diff_size, seed);
1560+
}
1561+
1562+
const struct bpf_func_proto bpf_csum_diff_proto = {
1563+
.func = bpf_csum_diff,
1564+
.gpl_only = false,
1565+
.ret_type = RET_INTEGER,
1566+
.arg1_type = ARG_PTR_TO_STACK,
1567+
.arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1568+
.arg3_type = ARG_PTR_TO_STACK,
1569+
.arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1570+
.arg5_type = ARG_ANYTHING,
1571+
};
1572+
15221573
static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
15231574
{
15241575
struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2;
@@ -1849,6 +1900,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id)
18491900
return &bpf_skb_store_bytes_proto;
18501901
case BPF_FUNC_skb_load_bytes:
18511902
return &bpf_skb_load_bytes_proto;
1903+
case BPF_FUNC_csum_diff:
1904+
return &bpf_csum_diff_proto;
18521905
case BPF_FUNC_l3_csum_replace:
18531906
return &bpf_l3_csum_replace_proto;
18541907
case BPF_FUNC_l4_csum_replace:

0 commit comments

Comments
 (0)