Skip to content

Commit 875f7b8

Browse files
Xuesen Huangkernel-patches-bot
authored andcommitted
bpf: add bpf_skb_adjust_room flag BPF_F_ADJ_ROOM_ENCAP_L2_ETH
bpf_skb_adjust_room sets the inner_protocol as skb->protocol for packets encapsulation. But that is not appropriate when pushing Ethernet header. Add an option to further specify encap L2 type and set the inner_protocol as ETH_P_TEB. Suggested-by: Willem de Bruijn <willemb@google.com> Signed-off-by: Xuesen Huang <huangxuesen@kuaishou.com> Signed-off-by: Zhiyong Cheng <chengzhiyong@kuaishou.com> Signed-off-by: Li Wang <wangli09@kuaishou.com>
1 parent c2f8ce5 commit 875f7b8

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

include/uapi/linux/bpf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,10 @@ union bpf_attr {
17651765
* Use with ENCAP_L3/L4 flags to further specify the tunnel
17661766
* type; *len* is the length of the inner MAC header.
17671767
*
1768+
* * **BPF_F_ADJ_ROOM_ENCAP_L2_ETH**:
1769+
* Use with BPF_F_ADJ_ROOM_ENCAP_L2 flag to further specify the
1770+
* L2 type as Ethernet.
1771+
*
17681772
* A call to this helper is susceptible to change the underlying
17691773
* packet buffer. Therefore, at load time, all checks on pointers
17701774
* previously done by the verifier are invalidated and must be
@@ -4169,6 +4173,7 @@ enum {
41694173
BPF_F_ADJ_ROOM_ENCAP_L4_GRE = (1ULL << 3),
41704174
BPF_F_ADJ_ROOM_ENCAP_L4_UDP = (1ULL << 4),
41714175
BPF_F_ADJ_ROOM_NO_CSUM_RESET = (1ULL << 5),
4176+
BPF_F_ADJ_ROOM_ENCAP_L2_ETH = (1ULL << 6),
41724177
};
41734178

41744179
enum {

net/core/filter.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3412,6 +3412,7 @@ static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
34123412
BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
34133413
BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
34143414
BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3415+
BPF_F_ADJ_ROOM_ENCAP_L2_ETH | \
34153416
BPF_F_ADJ_ROOM_ENCAP_L2( \
34163417
BPF_ADJ_ROOM_ENCAP_L2_MASK))
34173418

@@ -3448,6 +3449,10 @@ static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
34483449
flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
34493450
return -EINVAL;
34503451

3452+
if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH &&
3453+
inner_mac_len < ETH_HLEN)
3454+
return -EINVAL;
3455+
34513456
if (skb->encapsulation)
34523457
return -EALREADY;
34533458

@@ -3466,7 +3471,11 @@ static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
34663471
skb->inner_mac_header = inner_net - inner_mac_len;
34673472
skb->inner_network_header = inner_net;
34683473
skb->inner_transport_header = inner_trans;
3469-
skb_set_inner_protocol(skb, skb->protocol);
3474+
3475+
if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH)
3476+
skb_set_inner_protocol(skb, htons(ETH_P_TEB));
3477+
else
3478+
skb_set_inner_protocol(skb, skb->protocol);
34703479

34713480
skb->encapsulation = 1;
34723481
skb_set_network_header(skb, mac_len);

tools/include/uapi/linux/bpf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,10 @@ union bpf_attr {
17651765
* Use with ENCAP_L3/L4 flags to further specify the tunnel
17661766
* type; *len* is the length of the inner MAC header.
17671767
*
1768+
* * **BPF_F_ADJ_ROOM_ENCAP_L2_ETH**:
1769+
* Use with BPF_F_ADJ_ROOM_ENCAP_L2 flag to further specify the
1770+
* L2 type as Ethernet.
1771+
*
17681772
* A call to this helper is susceptible to change the underlying
17691773
* packet buffer. Therefore, at load time, all checks on pointers
17701774
* previously done by the verifier are invalidated and must be
@@ -4169,6 +4173,7 @@ enum {
41694173
BPF_F_ADJ_ROOM_ENCAP_L4_GRE = (1ULL << 3),
41704174
BPF_F_ADJ_ROOM_ENCAP_L4_UDP = (1ULL << 4),
41714175
BPF_F_ADJ_ROOM_NO_CSUM_RESET = (1ULL << 5),
4176+
BPF_F_ADJ_ROOM_ENCAP_L2_ETH = (1ULL << 6),
41724177
};
41734178

41744179
enum {

0 commit comments

Comments
 (0)