Skip to content

Commit f371c92

Browse files
author
Alexei Starovoitov
committed
Merge branch 'csum-fixes'
Daniel Borkmann says: ==================== This series fixes an issue originally reported by Lorenz Bauer where using the bpf_skb_adjust_room() helper hid a checksum bug since it wasn't adjusting CHECKSUM_UNNECESSARY's skb->csum_level after decap. The fix is two-fold: i) We do a safe reset in bpf_skb_adjust_room() to CHECKSUM_NONE with an opt- out flag BPF_F_ADJ_ROOM_NO_CSUM_RESET. ii) We add a new bpf_csum_level() for the latter in order to allow users to manually inc/dec the skb->csum_level when needed. The series is rebased against latest bpf-next tree. It can be applied there, or to bpf after the merge win sync from net-next. Thanks! ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 9a25c1d + c4ba153 commit f371c92

File tree

5 files changed

+158
-7
lines changed

5 files changed

+158
-7
lines changed

include/linux/skbuff.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3919,6 +3919,14 @@ static inline void __skb_incr_checksum_unnecessary(struct sk_buff *skb)
39193919
}
39203920
}
39213921

3922+
static inline void __skb_reset_checksum_unnecessary(struct sk_buff *skb)
3923+
{
3924+
if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
3925+
skb->ip_summed = CHECKSUM_NONE;
3926+
skb->csum_level = 0;
3927+
}
3928+
}
3929+
39223930
/* Check if we need to perform checksum complete validation.
39233931
*
39243932
* Returns true if checksum complete is needed, false otherwise

include/uapi/linux/bpf.h

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,13 @@ union bpf_attr {
16351635
* Grow or shrink the room for data in the packet associated to
16361636
* *skb* by *len_diff*, and according to the selected *mode*.
16371637
*
1638+
* By default, the helper will reset any offloaded checksum
1639+
* indicator of the skb to CHECKSUM_NONE. This can be avoided
1640+
* by the following flag:
1641+
*
1642+
* * **BPF_F_ADJ_ROOM_NO_CSUM_RESET**: Do not reset offloaded
1643+
* checksum data of the skb to CHECKSUM_NONE.
1644+
*
16381645
* There are two supported modes at this time:
16391646
*
16401647
* * **BPF_ADJ_ROOM_MAC**: Adjust room at the mac layer
@@ -3213,6 +3220,38 @@ union bpf_attr {
32133220
* calculation.
32143221
* Return
32153222
* Requested value, or 0, if flags are not recognized.
3223+
*
3224+
* int bpf_csum_level(struct sk_buff *skb, u64 level)
3225+
* Description
3226+
* Change the skbs checksum level by one layer up or down, or
3227+
* reset it entirely to none in order to have the stack perform
3228+
* checksum validation. The level is applicable to the following
3229+
* protocols: TCP, UDP, GRE, SCTP, FCOE. For example, a decap of
3230+
* | ETH | IP | UDP | GUE | IP | TCP | into | ETH | IP | TCP |
3231+
* through **bpf_skb_adjust_room**\ () helper with passing in
3232+
* **BPF_F_ADJ_ROOM_NO_CSUM_RESET** flag would require one call
3233+
* to **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_DEC** since
3234+
* the UDP header is removed. Similarly, an encap of the latter
3235+
* into the former could be accompanied by a helper call to
3236+
* **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_INC** if the
3237+
* skb is still intended to be processed in higher layers of the
3238+
* stack instead of just egressing at tc.
3239+
*
3240+
* There are three supported level settings at this time:
3241+
*
3242+
* * **BPF_CSUM_LEVEL_INC**: Increases skb->csum_level for skbs
3243+
* with CHECKSUM_UNNECESSARY.
3244+
* * **BPF_CSUM_LEVEL_DEC**: Decreases skb->csum_level for skbs
3245+
* with CHECKSUM_UNNECESSARY.
3246+
* * **BPF_CSUM_LEVEL_RESET**: Resets skb->csum_level to 0 and
3247+
* sets CHECKSUM_NONE to force checksum validation by the stack.
3248+
* * **BPF_CSUM_LEVEL_QUERY**: No-op, returns the current
3249+
* skb->csum_level.
3250+
* Return
3251+
* 0 on success, or a negative error in case of failure. In the
3252+
* case of **BPF_CSUM_LEVEL_QUERY**, the current skb->csum_level
3253+
* is returned or the error code -EACCES in case the skb is not
3254+
* subject to CHECKSUM_UNNECESSARY.
32163255
*/
32173256
#define __BPF_FUNC_MAPPER(FN) \
32183257
FN(unspec), \
@@ -3349,7 +3388,8 @@ union bpf_attr {
33493388
FN(ringbuf_reserve), \
33503389
FN(ringbuf_submit), \
33513390
FN(ringbuf_discard), \
3352-
FN(ringbuf_query),
3391+
FN(ringbuf_query), \
3392+
FN(csum_level),
33533393

33543394
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
33553395
* function eBPF program intends to call
@@ -3426,13 +3466,22 @@ enum {
34263466
BPF_F_CURRENT_NETNS = (-1L),
34273467
};
34283468

3469+
/* BPF_FUNC_csum_level level values. */
3470+
enum {
3471+
BPF_CSUM_LEVEL_QUERY,
3472+
BPF_CSUM_LEVEL_INC,
3473+
BPF_CSUM_LEVEL_DEC,
3474+
BPF_CSUM_LEVEL_RESET,
3475+
};
3476+
34293477
/* BPF_FUNC_skb_adjust_room flags. */
34303478
enum {
34313479
BPF_F_ADJ_ROOM_FIXED_GSO = (1ULL << 0),
34323480
BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = (1ULL << 1),
34333481
BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = (1ULL << 2),
34343482
BPF_F_ADJ_ROOM_ENCAP_L4_GRE = (1ULL << 3),
34353483
BPF_F_ADJ_ROOM_ENCAP_L4_UDP = (1ULL << 4),
3484+
BPF_F_ADJ_ROOM_NO_CSUM_RESET = (1ULL << 5),
34363485
};
34373486

34383487
enum {

net/core/filter.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,40 @@ static const struct bpf_func_proto bpf_csum_update_proto = {
20152015
.arg2_type = ARG_ANYTHING,
20162016
};
20172017

2018+
BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level)
2019+
{
2020+
/* The interface is to be used in combination with bpf_skb_adjust_room()
2021+
* for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET
2022+
* is passed as flags, for example.
2023+
*/
2024+
switch (level) {
2025+
case BPF_CSUM_LEVEL_INC:
2026+
__skb_incr_checksum_unnecessary(skb);
2027+
break;
2028+
case BPF_CSUM_LEVEL_DEC:
2029+
__skb_decr_checksum_unnecessary(skb);
2030+
break;
2031+
case BPF_CSUM_LEVEL_RESET:
2032+
__skb_reset_checksum_unnecessary(skb);
2033+
break;
2034+
case BPF_CSUM_LEVEL_QUERY:
2035+
return skb->ip_summed == CHECKSUM_UNNECESSARY ?
2036+
skb->csum_level : -EACCES;
2037+
default:
2038+
return -EINVAL;
2039+
}
2040+
2041+
return 0;
2042+
}
2043+
2044+
static const struct bpf_func_proto bpf_csum_level_proto = {
2045+
.func = bpf_csum_level,
2046+
.gpl_only = false,
2047+
.ret_type = RET_INTEGER,
2048+
.arg1_type = ARG_PTR_TO_CTX,
2049+
.arg2_type = ARG_ANYTHING,
2050+
};
2051+
20182052
static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
20192053
{
20202054
return dev_forward_skb(dev, skb);
@@ -3113,7 +3147,8 @@ static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
31133147
{
31143148
int ret;
31153149

3116-
if (flags & ~BPF_F_ADJ_ROOM_FIXED_GSO)
3150+
if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3151+
BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
31173152
return -EINVAL;
31183153

31193154
if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
@@ -3163,7 +3198,8 @@ BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
31633198
u32 off;
31643199
int ret;
31653200

3166-
if (unlikely(flags & ~BPF_F_ADJ_ROOM_MASK))
3201+
if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK |
3202+
BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
31673203
return -EINVAL;
31683204
if (unlikely(len_diff_abs > 0xfffU))
31693205
return -EFAULT;
@@ -3191,6 +3227,8 @@ BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
31913227

31923228
ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
31933229
bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3230+
if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET))
3231+
__skb_reset_checksum_unnecessary(skb);
31943232

31953233
bpf_compute_data_pointers(skb);
31963234
return ret;
@@ -6276,6 +6314,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
62766314
return &bpf_csum_diff_proto;
62776315
case BPF_FUNC_csum_update:
62786316
return &bpf_csum_update_proto;
6317+
case BPF_FUNC_csum_level:
6318+
return &bpf_csum_level_proto;
62796319
case BPF_FUNC_l3_csum_replace:
62806320
return &bpf_l3_csum_replace_proto;
62816321
case BPF_FUNC_l4_csum_replace:
@@ -6609,6 +6649,8 @@ lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
66096649
return &bpf_skb_store_bytes_proto;
66106650
case BPF_FUNC_csum_update:
66116651
return &bpf_csum_update_proto;
6652+
case BPF_FUNC_csum_level:
6653+
return &bpf_csum_level_proto;
66126654
case BPF_FUNC_l3_csum_replace:
66136655
return &bpf_l3_csum_replace_proto;
66146656
case BPF_FUNC_l4_csum_replace:

tools/include/uapi/linux/bpf.h

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,13 @@ union bpf_attr {
16351635
* Grow or shrink the room for data in the packet associated to
16361636
* *skb* by *len_diff*, and according to the selected *mode*.
16371637
*
1638+
* By default, the helper will reset any offloaded checksum
1639+
* indicator of the skb to CHECKSUM_NONE. This can be avoided
1640+
* by the following flag:
1641+
*
1642+
* * **BPF_F_ADJ_ROOM_NO_CSUM_RESET**: Do not reset offloaded
1643+
* checksum data of the skb to CHECKSUM_NONE.
1644+
*
16381645
* There are two supported modes at this time:
16391646
*
16401647
* * **BPF_ADJ_ROOM_MAC**: Adjust room at the mac layer
@@ -3213,6 +3220,38 @@ union bpf_attr {
32133220
* calculation.
32143221
* Return
32153222
* Requested value, or 0, if flags are not recognized.
3223+
*
3224+
* int bpf_csum_level(struct sk_buff *skb, u64 level)
3225+
* Description
3226+
* Change the skbs checksum level by one layer up or down, or
3227+
* reset it entirely to none in order to have the stack perform
3228+
* checksum validation. The level is applicable to the following
3229+
* protocols: TCP, UDP, GRE, SCTP, FCOE. For example, a decap of
3230+
* | ETH | IP | UDP | GUE | IP | TCP | into | ETH | IP | TCP |
3231+
* through **bpf_skb_adjust_room**\ () helper with passing in
3232+
* **BPF_F_ADJ_ROOM_NO_CSUM_RESET** flag would require one call
3233+
* to **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_DEC** since
3234+
* the UDP header is removed. Similarly, an encap of the latter
3235+
* into the former could be accompanied by a helper call to
3236+
* **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_INC** if the
3237+
* skb is still intended to be processed in higher layers of the
3238+
* stack instead of just egressing at tc.
3239+
*
3240+
* There are three supported level settings at this time:
3241+
*
3242+
* * **BPF_CSUM_LEVEL_INC**: Increases skb->csum_level for skbs
3243+
* with CHECKSUM_UNNECESSARY.
3244+
* * **BPF_CSUM_LEVEL_DEC**: Decreases skb->csum_level for skbs
3245+
* with CHECKSUM_UNNECESSARY.
3246+
* * **BPF_CSUM_LEVEL_RESET**: Resets skb->csum_level to 0 and
3247+
* sets CHECKSUM_NONE to force checksum validation by the stack.
3248+
* * **BPF_CSUM_LEVEL_QUERY**: No-op, returns the current
3249+
* skb->csum_level.
3250+
* Return
3251+
* 0 on success, or a negative error in case of failure. In the
3252+
* case of **BPF_CSUM_LEVEL_QUERY**, the current skb->csum_level
3253+
* is returned or the error code -EACCES in case the skb is not
3254+
* subject to CHECKSUM_UNNECESSARY.
32163255
*/
32173256
#define __BPF_FUNC_MAPPER(FN) \
32183257
FN(unspec), \
@@ -3349,7 +3388,8 @@ union bpf_attr {
33493388
FN(ringbuf_reserve), \
33503389
FN(ringbuf_submit), \
33513390
FN(ringbuf_discard), \
3352-
FN(ringbuf_query),
3391+
FN(ringbuf_query), \
3392+
FN(csum_level),
33533393

33543394
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
33553395
* function eBPF program intends to call
@@ -3426,13 +3466,22 @@ enum {
34263466
BPF_F_CURRENT_NETNS = (-1L),
34273467
};
34283468

3469+
/* BPF_FUNC_csum_level level values. */
3470+
enum {
3471+
BPF_CSUM_LEVEL_QUERY,
3472+
BPF_CSUM_LEVEL_INC,
3473+
BPF_CSUM_LEVEL_DEC,
3474+
BPF_CSUM_LEVEL_RESET,
3475+
};
3476+
34293477
/* BPF_FUNC_skb_adjust_room flags. */
34303478
enum {
34313479
BPF_F_ADJ_ROOM_FIXED_GSO = (1ULL << 0),
34323480
BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = (1ULL << 1),
34333481
BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = (1ULL << 2),
34343482
BPF_F_ADJ_ROOM_ENCAP_L4_GRE = (1ULL << 3),
34353483
BPF_F_ADJ_ROOM_ENCAP_L4_UDP = (1ULL << 4),
3484+
BPF_F_ADJ_ROOM_NO_CSUM_RESET = (1ULL << 5),
34363485
};
34373486

34383487
enum {

tools/testing/selftests/bpf/progs/test_cls_redirect.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,10 @@ static ret_t accept_locally(struct __sk_buff *skb, encap_headers_t *encap)
380380
}
381381

382382
if (bpf_skb_adjust_room(skb, -encap_overhead, BPF_ADJ_ROOM_MAC,
383-
BPF_F_ADJ_ROOM_FIXED_GSO)) {
383+
BPF_F_ADJ_ROOM_FIXED_GSO |
384+
BPF_F_ADJ_ROOM_NO_CSUM_RESET) ||
385+
bpf_csum_level(skb, BPF_CSUM_LEVEL_DEC))
384386
return TC_ACT_SHOT;
385-
}
386387

387388
return bpf_redirect(skb->ifindex, BPF_F_INGRESS);
388389
}
@@ -472,7 +473,9 @@ static ret_t forward_with_gre(struct __sk_buff *skb, encap_headers_t *encap,
472473
}
473474

474475
if (bpf_skb_adjust_room(skb, delta, BPF_ADJ_ROOM_NET,
475-
BPF_F_ADJ_ROOM_FIXED_GSO)) {
476+
BPF_F_ADJ_ROOM_FIXED_GSO |
477+
BPF_F_ADJ_ROOM_NO_CSUM_RESET) ||
478+
bpf_csum_level(skb, BPF_CSUM_LEVEL_INC)) {
476479
metrics->errors_total_encap_adjust_failed++;
477480
return TC_ACT_SHOT;
478481
}

0 commit comments

Comments
 (0)