Skip to content

Commit d219df6

Browse files
Ziyang XuanMartin KaFai Lau
authored andcommitted
bpf: Add ipip6 and ip6ip decap support for bpf_skb_adjust_room()
Add ipip6 and ip6ip decap support for bpf_skb_adjust_room(). Main use case is for using cls_bpf on ingress hook to decapsulate IPv4 over IPv6 and IPv6 over IPv4 tunnel packets. Add two new flags BPF_F_ADJ_ROOM_DECAP_L3_IPV{4,6} to indicate the new IP header version after decapsulating the outer IP header. Suggested-by: Willem de Bruijn <willemb@google.com> Signed-off-by: Ziyang Xuan <william.xuanziyang@huawei.com> Reviewed-by: Willem de Bruijn <willemb@google.com> Link: https://lore.kernel.org/r/b268ec7f0ff9431f4f43b1b40ab856ebb28cb4e1.1673574419.git.william.xuanziyang@huawei.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent 1c48391 commit d219df6

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,6 +2647,11 @@ union bpf_attr {
26472647
* Use with BPF_F_ADJ_ROOM_ENCAP_L2 flag to further specify the
26482648
* L2 type as Ethernet.
26492649
*
2650+
* * **BPF_F_ADJ_ROOM_DECAP_L3_IPV4**,
2651+
* **BPF_F_ADJ_ROOM_DECAP_L3_IPV6**:
2652+
* Indicate the new IP header version after decapsulating the outer
2653+
* IP header. Used when the inner and outer IP versions are different.
2654+
*
26502655
* A call to this helper is susceptible to change the underlying
26512656
* packet buffer. Therefore, at load time, all checks on pointers
26522657
* previously done by the verifier are invalidated and must be
@@ -5807,6 +5812,8 @@ enum {
58075812
BPF_F_ADJ_ROOM_ENCAP_L4_UDP = (1ULL << 4),
58085813
BPF_F_ADJ_ROOM_NO_CSUM_RESET = (1ULL << 5),
58095814
BPF_F_ADJ_ROOM_ENCAP_L2_ETH = (1ULL << 6),
5815+
BPF_F_ADJ_ROOM_DECAP_L3_IPV4 = (1ULL << 7),
5816+
BPF_F_ADJ_ROOM_DECAP_L3_IPV6 = (1ULL << 8),
58105817
};
58115818

58125819
enum {

net/core/filter.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3381,13 +3381,17 @@ static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
33813381
#define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
33823382
BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
33833383

3384+
#define BPF_F_ADJ_ROOM_DECAP_L3_MASK (BPF_F_ADJ_ROOM_DECAP_L3_IPV4 | \
3385+
BPF_F_ADJ_ROOM_DECAP_L3_IPV6)
3386+
33843387
#define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
33853388
BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
33863389
BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
33873390
BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
33883391
BPF_F_ADJ_ROOM_ENCAP_L2_ETH | \
33893392
BPF_F_ADJ_ROOM_ENCAP_L2( \
3390-
BPF_ADJ_ROOM_ENCAP_L2_MASK))
3393+
BPF_ADJ_ROOM_ENCAP_L2_MASK) | \
3394+
BPF_F_ADJ_ROOM_DECAP_L3_MASK)
33913395

33923396
static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
33933397
u64 flags)
@@ -3501,6 +3505,7 @@ static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
35013505
int ret;
35023506

35033507
if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3508+
BPF_F_ADJ_ROOM_DECAP_L3_MASK |
35043509
BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
35053510
return -EINVAL;
35063511

@@ -3519,6 +3524,14 @@ static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
35193524
if (unlikely(ret < 0))
35203525
return ret;
35213526

3527+
/* Match skb->protocol to new outer l3 protocol */
3528+
if (skb->protocol == htons(ETH_P_IP) &&
3529+
flags & BPF_F_ADJ_ROOM_DECAP_L3_IPV6)
3530+
skb->protocol = htons(ETH_P_IPV6);
3531+
else if (skb->protocol == htons(ETH_P_IPV6) &&
3532+
flags & BPF_F_ADJ_ROOM_DECAP_L3_IPV4)
3533+
skb->protocol = htons(ETH_P_IP);
3534+
35223535
if (skb_is_gso(skb)) {
35233536
struct skb_shared_info *shinfo = skb_shinfo(skb);
35243537

@@ -3608,6 +3621,22 @@ BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
36083621
return -ENOTSUPP;
36093622
}
36103623

3624+
if (flags & BPF_F_ADJ_ROOM_DECAP_L3_MASK) {
3625+
if (!shrink)
3626+
return -EINVAL;
3627+
3628+
switch (flags & BPF_F_ADJ_ROOM_DECAP_L3_MASK) {
3629+
case BPF_F_ADJ_ROOM_DECAP_L3_IPV4:
3630+
len_min = sizeof(struct iphdr);
3631+
break;
3632+
case BPF_F_ADJ_ROOM_DECAP_L3_IPV6:
3633+
len_min = sizeof(struct ipv6hdr);
3634+
break;
3635+
default:
3636+
return -EINVAL;
3637+
}
3638+
}
3639+
36113640
len_cur = skb->len - skb_network_offset(skb);
36123641
if ((shrink && (len_diff_abs >= len_cur ||
36133642
len_cur - len_diff_abs < len_min)) ||

tools/include/uapi/linux/bpf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,6 +2647,11 @@ union bpf_attr {
26472647
* Use with BPF_F_ADJ_ROOM_ENCAP_L2 flag to further specify the
26482648
* L2 type as Ethernet.
26492649
*
2650+
* * **BPF_F_ADJ_ROOM_DECAP_L3_IPV4**,
2651+
* **BPF_F_ADJ_ROOM_DECAP_L3_IPV6**:
2652+
* Indicate the new IP header version after decapsulating the outer
2653+
* IP header. Used when the inner and outer IP versions are different.
2654+
*
26502655
* A call to this helper is susceptible to change the underlying
26512656
* packet buffer. Therefore, at load time, all checks on pointers
26522657
* previously done by the verifier are invalidated and must be
@@ -5807,6 +5812,8 @@ enum {
58075812
BPF_F_ADJ_ROOM_ENCAP_L4_UDP = (1ULL << 4),
58085813
BPF_F_ADJ_ROOM_NO_CSUM_RESET = (1ULL << 5),
58095814
BPF_F_ADJ_ROOM_ENCAP_L2_ETH = (1ULL << 6),
5815+
BPF_F_ADJ_ROOM_DECAP_L3_IPV4 = (1ULL << 7),
5816+
BPF_F_ADJ_ROOM_DECAP_L3_IPV6 = (1ULL << 8),
58105817
};
58115818

58125819
enum {

0 commit comments

Comments
 (0)