filter: add BPF_ADJ_ROOM_DATA mode to bpf_skb_adjust_room()
Subsystems (4)
PATCH 1/1 filter: add BPF_ADJ_ROOM_DATA mode to bpf_skb_adjust_room()
This new mode enables to add or remove an l2 header in a programmatic way with cls_bpf.
For example, it enables to play with mpls headers.
--- include/uapi/linux/bpf.h | 3 ++ net/core/filter.c | 54 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 3 ++ 3 files changed, 60 insertions(+)
include/uapi/linux/bpf.h +3 -0
@@ -1467,6 +1467,8 @@ union bpf_attr {* * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer * (room space is added or removed below the layer 3 header). +* * **BPF_ADJ_ROOM_DATA**: Adjust room at the beginning of the +* packet (room space is added or removed below skb->data). * * All values for *flags* are reserved for future usage, and must * be left at zero.
@@ -2408,6 +2410,7 @@ enum bpf_func_id {/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET , +BPF_ADJ_ROOM_DATA , };/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */
net/core/filter.c +54 -0
66 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +3 -0
@@ -1467,6 +1467,8 @@ union bpf_attr {* * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer * (room space is added or removed below the layer 3 header). +* * **BPF_ADJ_ROOM_DATA**: Adjust room at the beginning of the +* packet (room space is added or removed below skb->data). * * All values for *flags* are reserved for future usage, and must * be left at zero.
@@ -2408,6 +2410,7 @@ enum bpf_func_id {/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET , +BPF_ADJ_ROOM_DATA , };/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */
Signature · 1 line
2.18.0
Quoted 45 lines · Nicolas Dichtel
This new mode enables to add or remove an l2 header in a programmatic way with
cls_bpf.
For example, it enables to play with mpls headers.Signed-off-by Nicolas Dichtel <[email protected]>--- include/uapi/linux/bpf.h | 3 ++ net/core/filter.c | 54 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 3 ++ 3 files changed, 60 insertions(+)include/uapi/linux/bpf.h +3
-0@@ -1467,6 +1467,8 @@ union bpf_attr {* * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer * (room space is added or removed below the layer 3 header). +* * **BPF_ADJ_ROOM_DATA**: Adjust room at the beginning of the +* packet (room space is added or removed below skb->data). * * All values for *flags* are reserved for future usage, and must * be left at zero. @@ -2408,6 +2410,7 @@ enum bpf_func_id {/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET , +BPF_ADJ_ROOM_DATA , };/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */ net/core/filter.c +5
-0@@ -2884,6 +2884,58 @@ static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)return ret; } +static int bpf_skb_data_shrink (struct sk_buff *skb,u32 len) +{ +unsigned short hhlen = skb->dev->header_ops ? + skb->dev->hard_header_len :0 ; +int ret;
net/core/filter.c static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff) · line 2901
Quoted 10 lines
net/core/filter.c +10
-0+ + ret =skb_unclone (skb,GFP_ATOMIC ); +if (unlikely (ret <0 )) +return ret; + +__skb_pull (skb, len); +skb_reset_mac_header (skb); +skb_reset_network_header (skb); + skb->network_header += hhlen; +skb_reset_transport_header (skb);
hmm...why transport_header does not need += hhlen here while network_header does?
Quoted 73 lines
net/core/filter.c +39
-0+return 0 ; +} + +static int bpf_skb_data_grow (struct sk_buff *skb,u32 len) +{ +unsigned short hhlen = skb->dev->header_ops ? + skb->dev->hard_header_len :0 ; +int ret; + + ret =skb_cow (skb, len); +if (unlikely (ret <0 )) +return ret; + +skb_push (skb, len); +skb_reset_mac_header (skb); +return 0 ; +} + +static int bpf_skb_adjust_data (struct sk_buff *skb,s32 len_diff) +{ +u32 len_diff_abs =abs (len_diff); +bool shrink = len_diff <0 ; +int ret; + +if (unlikely (len_diff_abs >0xfffU )) +return -EFAULT ; + +if (shrink && len_diff_abs >=skb_headlen (skb)) +return -EFAULT ; + + ret = shrink ?bpf_skb_data_shrink (skb, len_diff_abs) : +bpf_skb_data_grow (skb, len_diff_abs); + +bpf_compute_data_pointers (skb); +return ret; +} + BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff, u32, mode, u64, flags) {@@ -2891,6 +2943,8 @@ BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,return -EINVAL ;if (likely (mode ==BPF_ADJ_ROOM_NET ))return bpf_skb_adjust_net (skb, len_diff); +if (likely (mode ==BPF_ADJ_ROOM_DATA )) +return bpf_skb_adjust_data (skb, len_diff);return -ENOTSUPP ; }tools/include/uapi/linux/bpf.h +3
-0@@ -1467,6 +1467,8 @@ union bpf_attr {* * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer * (room space is added or removed below the layer 3 header). +* * **BPF_ADJ_ROOM_DATA**: Adjust room at the beginning of the +* packet (room space is added or removed below skb->data). * * All values for *flags* are reserved for future usage, and must * be left at zero. @@ -2408,6 +2410,7 @@ enum bpf_func_id {/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET , +BPF_ADJ_ROOM_DATA , };/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */ Signature · 1 line
2.18.0
Le 09/11/2018 à 19:51, Martin Lau a écrit :
Quoted 1 line · Nicolas Dichtel
[snip]
Quoted 9 lines
Quoted 9 lines
net/core/filter.c +9
-0+static int bpf_skb_data_shrink (struct sk_buff *skb,u32 len) +{ +unsigned short hhlen = skb->dev->header_ops ? + skb->dev->hard_header_len :0 ; +int ret; + + ret =skb_unclone (skb,GFP_ATOMIC ); +if (unlikely (ret <0 )) +return ret;
Quoted 8 lines
net/core/filter.c static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff) · line 2901
Quoted 6 lines
net/core/filter.c +6
-0+ +__skb_pull (skb, len); +skb_reset_mac_header (skb); +skb_reset_network_header (skb); + skb->network_header += hhlen; +skb_reset_transport_header (skb);hmm...why
transport_headerdoes not need += hhlen here whilenetwork_headerdoes?
network_header is mandatory because bpf_redirect(BPF_F_INGRESS) can be called and network_header is expected to be correctly set in this case. For transport_header, I choose to not set it, because the stack will set it later (for example ip_rcv_core()).
Regards,
Nicolas
Martin Lau
Acked-byQuoted 17 lines · Nicolas Dichtel
Le 09/11/2018 à 19:51, Martin Lau a écrit :
Quoted 1 line · Nicolas Dichtel
[snip]
Quoted 6 lines
Quoted 6 lines
net/core/filter.c +6
-0+static int bpf_skb_data_shrink (struct sk_buff *skb,u32 len) +{ +unsigned short hhlen = skb->dev->header_ops ? + skb->dev->hard_header_len :0 ; +int ret; +Quoted 8 lines
net/core/filter.c static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff) · line 2900
Quoted 8 lines
net/core/filter.c +8
-0+ ret =skb_unclone (skb,GFP_ATOMIC ); +if (unlikely (ret <0 )) +return ret; + +__skb_pull (skb, len); +skb_reset_mac_header (skb); +skb_reset_network_header (skb); + skb->network_header += hhlen;
Nit. skb_set_network_header(skb, hhlen);
Othen than that
Quoted 8 lines
Quoted 3 lines
net/core/filter.c static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff) · line 2901
Quoted 1 line
net/core/filter.c +1
-0+skb_reset_transport_header (skb);hmm...why
transport_headerdoes not need += hhlen here whilenetwork_headerdoes?
network_headeris mandatory becausebpf_redirect(BPF_F_INGRESS)can be called andnetwork_headeris expected to be correctly set in this case. Fortransport_header, I choose to not set it, because the stack will set it later (for exampleip_rcv_core()).
ic. make sense.
PATCH v2 1/1 filter: add BPF_ADJ_ROOM_DATA mode to bpf_skb_adjust_room()
This new mode enables to add or remove an l2 header in a programmatic way with cls_bpf.
For example, it enables to play with mpls headers.
---
v2: use skb_set_network_header()
include/uapi/linux/bpf.h | 3 ++ net/core/filter.c | 53 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 3 ++ 3 files changed, 59 insertions(+)
include/uapi/linux/bpf.h +3 -0
@@ -1467,6 +1467,8 @@ union bpf_attr {* * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer * (room space is added or removed below the layer 3 header). +* * **BPF_ADJ_ROOM_DATA**: Adjust room at the beginning of the +* packet (room space is added or removed below skb->data). * * All values for *flags* are reserved for future usage, and must * be left at zero.
@@ -2412,6 +2414,7 @@ enum bpf_func_id {/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET , +BPF_ADJ_ROOM_DATA , };/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */
net/core/filter.c +53 -0
65 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +3 -0
@@ -1467,6 +1467,8 @@ union bpf_attr {* * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer * (room space is added or removed below the layer 3 header). +* * **BPF_ADJ_ROOM_DATA**: Adjust room at the beginning of the +* packet (room space is added or removed below skb->data). * * All values for *flags* are reserved for future usage, and must * be left at zero.
@@ -2408,6 +2410,7 @@ enum bpf_func_id {/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET , +BPF_ADJ_ROOM_DATA , };/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */
Signature · 1 line
2.18.0
Quoted 6 lines · Nicolas Dichtel
This new mode enables to add or remove an l2 header in a programmatic way with
cls_bpf.
For example, it enables to play with mpls headers.Signed-off-by Nicolas Dichtel <[email protected]>Acked-by Martin KaFai Lau <[email protected]>
Daniel, thoughts?
Daniel Borkmann
maintainerQuoted 6 lines · Nicolas Dichtel
This new mode enables to add or remove an l2 header in a programmatic way with
cls_bpf.
For example, it enables to play with mpls headers.Signed-off-by Nicolas Dichtel <[email protected]>Acked-by Martin KaFai Lau <[email protected]>
(Sorry for late reply, swamped due to Plumbers.)
Quoted 14 lines
---
v2: use
skb_set_network_header()include/uapi/linux/bpf.h | 3 ++ net/core/filter.c | 53 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 3 ++ 3 files changed, 59 insertions(+)include/uapi/linux/bpf.h +0
-0
include/uapi/linux/bpf.h union bpf_attr { · line 1471
Quoted 5 lines
include/uapi/linux/bpf.h +2
-0@@ -1467,6 +1467,8 @@ union bpf_attr {* * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer * (room space is added or removed below the layer 3 header). +* * **BPF_ADJ_ROOM_DATA**: Adjust room at the beginning of the +* packet (room space is added or removed below skb->data).
Nit: in this case it would probably make more sense to name it BPF_ADJ_ROOM_MAC. BPF_ADJ_ROOM_DATA reads like we're adjusting payload data.
Quoted 37 lines
include/uapi/linux/bpf.h +1
-0* All values for *flags* are reserved for future usage, and must * be left at zero. @@ -2412,6 +2414,7 @@ enum bpf_func_id {/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET , +BPF_ADJ_ROOM_DATA , };/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */ net/core/filter.c +19
-0@@ -2884,6 +2884,57 @@ static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)return ret; } +static int bpf_skb_data_shrink (struct sk_buff *skb,u32 len) +{ +unsigned short hhlen = skb->dev->header_ops ? + skb->dev->hard_header_len :0 ; +int ret; + + ret =skb_unclone (skb,GFP_ATOMIC ); +if (unlikely (ret <0 )) +return ret; + +__skb_pull (skb, len); +skb_reset_mac_header (skb); +skb_set_network_header (skb, hhlen); +skb_reset_transport_header (skb); +return 0 ; +} + +static int bpf_skb_data_grow (struct sk_buff *skb,u32 len) +{
net/core/filter.c static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff) · line 2915
Quoted 10 lines
net/core/filter.c +10
-0+unsigned short hhlen = skb->dev->header_ops ? + skb->dev->hard_header_len :0 ; +int ret; + + ret =skb_cow (skb, len); +if (unlikely (ret <0 )) +return ret; + +skb_push (skb, len); +skb_reset_mac_header (skb);
Looks like this could leak uninitialized mem into the BPF prog as it's not zeroed out. Can't we just reuse bpf_skb_generic_push() and the bpf_skb_generic_pop()?
bpf_skb_vlan_push() and bpf_skb_vlan_pop() do a bpf_push_mac_rcsum() / bpf_pull_mac_rcsum() in order to not screw up things like csum complete. Wouldn't this be needed here as well?
How does this interact with MPLS GSO?
If the packet gets pushed back into the stack, would AF_MPLS handle it? Probably good to add test cases to BPF kselftest suite with it.
Don't we need to reject the packet in case of skb->encapsulation?
Looking at push_mpls() and pop_mpls() from OVS implementation, do we also need to deal with skb inner network header / proto?
Given all this would it rather make sense to add MPLS specific helpers in similar fashion to the vlan ones we have?
Is there any other use case aside from MPLS?
Quoted 7 lines
net/core/filter.c +7
-0+return 0 ; +} + +static int bpf_skb_adjust_data (struct sk_buff *skb,s32 len_diff) +{ +u32 len_diff_abs =abs (len_diff); +bool shrink = len_diff <0 ;
net/core/filter.c static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff) · line 2932
Quoted 10 lines
net/core/filter.c +10
-0+int ret; + +if (unlikely (len_diff_abs >0xfffU )) +return -EFAULT ; + +if (shrink && len_diff_abs >=skb_headlen (skb)) +return -EFAULT ; + + ret = shrink ?bpf_skb_data_shrink (skb, len_diff_abs) : +bpf_skb_data_grow (skb, len_diff_abs);
Hmm, I think this has a bug in that the above two do not adjust skb->mac_len. Then things like cls_bpf_classify() will __skb_pull() based on the old mac_len, getting you to a wrong offset in the packet when this is for example pushed back into ingress, etc.
Quoted 38 lines
net/core/filter.c +6
-0+bpf_compute_data_pointers (skb); +return ret; +} + BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff, u32, mode, u64, flags) {@@ -2891,6 +2942,8 @@ BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,return -EINVAL ;if (likely (mode ==BPF_ADJ_ROOM_NET ))return bpf_skb_adjust_net (skb, len_diff); +if (likely (mode ==BPF_ADJ_ROOM_DATA )) +return bpf_skb_adjust_data (skb, len_diff);return -ENOTSUPP ; }tools/include/uapi/linux/bpf.h +3
-0@@ -1467,6 +1467,8 @@ union bpf_attr {* * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer * (room space is added or removed below the layer 3 header). +* * **BPF_ADJ_ROOM_DATA**: Adjust room at the beginning of the +* packet (room space is added or removed below skb->data). * * All values for *flags* are reserved for future usage, and must * be left at zero. @@ -2408,6 +2410,7 @@ enum bpf_func_id {/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET , +BPF_ADJ_ROOM_DATA , };/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */
Le 20/11/2018 à 02:06, Daniel Borkmann a écrit :
Quoted 9 lines · Nicolas Dichtel
Quoted 6 lines
This new mode enables to add or remove an l2 header in a programmatic way with
cls_bpf.
For example, it enables to play with mpls headers.Signed-off-by Nicolas Dichtel <[email protected]>Acked-by Martin KaFai Lau <[email protected]>(Sorry for late reply, swamped due to Plumbers.)
I thought so, no problem ;-)
Quoted 10 lines
Quoted 10 lines
---
v2: use
skb_set_network_header()include/uapi/linux/bpf.h | 3 ++ net/core/filter.c | 53 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 3 ++ 3 files changed, 59 insertions(+)
Quoted 13 lines
include/uapi/linux/bpf.h union bpf_attr { · line 1471
Quoted 10 lines
include/uapi/linux/bpf.h +2
-0@@ -1467,6 +1467,8 @@ union bpf_attr {* * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer * (room space is added or removed below the layer 3 header). +* * **BPF_ADJ_ROOM_DATA**: Adjust room at the beginning of the +* packet (room space is added or removed below skb->data). Nit: in this case it would probably make more sense to name it
BPF_ADJ_ROOM_MAC.BPF_ADJ_ROOM_DATAreads like we're adjusting payload data.
Yep, I first had in mind skb->data, but you're right.
Quoted 44 lines
Quoted 44 lines
include/uapi/linux/bpf.h +1
-0* All values for *flags* are reserved for future usage, and must * be left at zero. @@ -2412,6 +2414,7 @@ enum bpf_func_id {/* Mode for BPF_FUNC_skb_adjust_room helper. */ enum bpf_adj_room_mode {BPF_ADJ_ROOM_NET , +BPF_ADJ_ROOM_DATA , };/* Mode for BPF_FUNC_skb_load_bytes_relative helper. */ net/core/filter.c +25
-0@@ -2884,6 +2884,57 @@ static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)return ret; } +static int bpf_skb_data_shrink (struct sk_buff *skb,u32 len) +{ +unsigned short hhlen = skb->dev->header_ops ? + skb->dev->hard_header_len :0 ; +int ret; + + ret =skb_unclone (skb,GFP_ATOMIC ); +if (unlikely (ret <0 )) +return ret; + +__skb_pull (skb, len); +skb_reset_mac_header (skb); +skb_set_network_header (skb, hhlen); +skb_reset_transport_header (skb); +return 0 ; +} + +static int bpf_skb_data_grow (struct sk_buff *skb,u32 len) +{ +unsigned short hhlen = skb->dev->header_ops ? + skb->dev->hard_header_len :0 ; +int ret; + + ret =skb_cow (skb, len); +if (unlikely (ret <0 ))
Quoted 8 lines
net/core/filter.c static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff) · line 2915
Quoted 4 lines
net/core/filter.c +4
-0+return ret; + +skb_push (skb, len); +skb_reset_mac_header (skb);Looks like this could leak uninitialized mem into the BPF prog as it's not zeroed out. Can't we just reuse
bpf_skb_generic_push()and thebpf_skb_generic_pop()?
Hmm, at some point, it was not possible, but it was an intermediate version and I don't see why now.
Quoted 6 lines
bpf_skb_vlan_push()andbpf_skb_vlan_pop()do abpf_push_mac_rcsum()/bpf_pull_mac_rcsum()in order to not screw up things like csum complete. Wouldn't this be needed here as well?How does this interact with MPLS GSO?
I overlooked both points.
Quoted 3 lines
If the packet gets pushed back into the stack, would
AF_MPLShandle it? Probably good to add test cases to BPF kselftest suite with it.
Ok, I will have a look to this.
Quoted 10 lines
Don't we need to reject the packet in case of
skb->encapsulation?Looking at
push_mpls()andpop_mpls()from OVS implementation, do we also need to deal with skb inner network header / proto?Given all this would it rather make sense to add MPLS specific helpers in similar fashion to the vlan ones we have?
Is there any other use case aside from MPLS?
Yes, I was targeting a generic encap/decap at l2 level. Maybe more complex than I thought.
Quoted 15 lines
Quoted 15 lines
net/core/filter.c +14
-0+return 0 ; +} + +static int bpf_skb_adjust_data (struct sk_buff *skb,s32 len_diff) +{ +u32 len_diff_abs =abs (len_diff); +bool shrink = len_diff <0 ; +int ret; + +if (unlikely (len_diff_abs >0xfffU )) +return -EFAULT ; + +if (shrink && len_diff_abs >=skb_headlen (skb)) +return -EFAULT ;
Quoted 8 lines
net/core/filter.c static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff) · line 2932
Quoted 3 lines
net/core/filter.c +3
-0+ + ret = shrink ?bpf_skb_data_shrink (skb, len_diff_abs) : +bpf_skb_data_grow (skb, len_diff_abs);Hmm, I think this has a bug in that the above two do not adjust
skb->mac_len. Then things likecls_bpf_classify()will__skb_pull()based on the oldmac_len, getting you to a wrong offset in the packet when this is for example pushed back into ingress, etc.
Nice catch!
Thank you,
Nicolas