-
Notifications
You must be signed in to change notification settings - Fork 4
/
bpf_skb_utils.h
174 lines (134 loc) · 3.37 KB
/
bpf_skb_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Felix Fietkau <[email protected]>
*/
#ifndef __BPF_SKB_UTILS_H
#define __BPF_SKB_UTILS_H
#include <uapi/linux/bpf.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/ip.h>
#include <uapi/linux/ipv6.h>
#include <linux/if_vlan.h>
#include <linux/ip.h>
#include <net/ipv6.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
struct skb_parser_info {
struct __sk_buff *skb;
__u32 offset;
int proto;
};
static __always_inline void *__skb_data(struct __sk_buff *skb)
{
return (void *)(long)READ_ONCE(skb->data);
}
static __always_inline void *
skb_ptr(struct __sk_buff *skb, __u32 offset, __u32 len)
{
void *ptr = __skb_data(skb) + offset;
void *end = (void *)(long)(skb->data_end);
if (ptr + len >= end)
return NULL;
return ptr;
}
static __always_inline void *
skb_info_ptr(struct skb_parser_info *info, __u32 len)
{
__u32 offset = info->offset;
return skb_ptr(info->skb, offset, len);
}
static __always_inline void
skb_parse_init(struct skb_parser_info *info, struct __sk_buff *skb)
{
*info = (struct skb_parser_info){
.skb = skb
};
}
static __always_inline struct ethhdr *
skb_parse_ethernet(struct skb_parser_info *info)
{
struct ethhdr *eth;
int len;
len = sizeof(*eth) + 2 * sizeof(struct vlan_hdr) + sizeof(struct ipv6hdr);
if (len > info->skb->len)
len = info->skb->len;
bpf_skb_pull_data(info->skb, len);
eth = skb_info_ptr(info, sizeof(*eth));
if (!eth)
return NULL;
info->proto = eth->h_proto;
info->offset += sizeof(*eth);
return eth;
}
static __always_inline struct vlan_hdr *
skb_parse_vlan(struct skb_parser_info *info)
{
struct vlan_hdr *vlh;
if (info->proto != bpf_htons(ETH_P_8021Q) &&
info->proto != bpf_htons(ETH_P_8021AD))
return NULL;
vlh = skb_info_ptr(info, sizeof(*vlh));
if (!vlh)
return NULL;
info->proto = vlh->h_vlan_encapsulated_proto;
info->offset += sizeof(*vlh);
return vlh;
}
static __always_inline struct iphdr *
skb_parse_ipv4(struct skb_parser_info *info, int min_l4_bytes)
{
struct iphdr *iph;
int proto, hdr_len;
__u32 pull_len;
if (info->proto != bpf_htons(ETH_P_IP))
return NULL;
iph = skb_info_ptr(info, sizeof(*iph));
if (!iph)
return NULL;
hdr_len = iph->ihl * 4;
if (hdr_len < sizeof(*iph))
return NULL;
pull_len = info->offset + hdr_len + min_l4_bytes;
if (pull_len > info->skb->len)
pull_len = info->skb->len;
if (bpf_skb_pull_data(info->skb, pull_len))
return NULL;
iph = skb_info_ptr(info, sizeof(*iph));
if (!iph)
return NULL;
info->proto = iph->protocol;
info->offset += hdr_len;
return iph;
}
static __always_inline struct ipv6hdr *
skb_parse_ipv6(struct skb_parser_info *info, int max_l4_bytes)
{
struct ipv6hdr *ip6h;
__u32 pull_len;
if (info->proto != bpf_htons(ETH_P_IPV6))
return NULL;
pull_len = info->offset + sizeof(*ip6h) + max_l4_bytes;
if (pull_len > info->skb->len)
pull_len = info->skb->len;
if (bpf_skb_pull_data(info->skb, pull_len))
return NULL;
ip6h = skb_info_ptr(info, sizeof(*ip6h));
if (!ip6h)
return NULL;
info->proto = READ_ONCE(ip6h->nexthdr);
info->offset += sizeof(*ip6h);
return ip6h;
}
static __always_inline struct tcphdr *
skb_parse_tcp(struct skb_parser_info *info)
{
struct tcphdr *tcph;
if (info->proto != IPPROTO_TCP)
return NULL;
tcph = skb_info_ptr(info, sizeof(*tcph));
if (!tcph)
return NULL;
info->offset += tcph->doff * 4;
return tcph;
}
#endif