1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Jesper Dangaard Brouer */
5 #include <bpf/bpf_helpers.h>
6 #include <linux/if_ether.h>
11 char _license[] SEC("license") = "GPL";
13 /* Userspace will update with MTU it can see on device */
14 volatile const int GLOBAL_USER_MTU;
15 volatile const __u32 GLOBAL_USER_IFINDEX;
17 /* BPF-prog will update these with MTU values it can see */
18 __u32 global_bpf_mtu_xdp = 0;
19 __u32 global_bpf_mtu_tc = 0;
22 int xdp_use_helper_basic(struct xdp_md *ctx)
26 if (bpf_check_mtu(ctx, 0, &mtu_len, 0, 0))
33 int xdp_use_helper(struct xdp_md *ctx)
35 int retval = XDP_PASS; /* Expected retval on successful test */
40 /* When ifindex is zero, save net_device lookup and use ctx netdev */
41 if (GLOBAL_USER_IFINDEX > 0)
42 ifindex = GLOBAL_USER_IFINDEX;
44 if (bpf_check_mtu(ctx, ifindex, &mtu_len, delta, 0)) {
45 /* mtu_len is also valid when check fail */
50 if (mtu_len != GLOBAL_USER_MTU)
54 global_bpf_mtu_xdp = mtu_len;
59 int xdp_exceed_mtu(struct xdp_md *ctx)
61 void *data_end = (void *)(long)ctx->data_end;
62 void *data = (void *)(long)ctx->data;
63 __u32 ifindex = GLOBAL_USER_IFINDEX;
64 __u32 data_len = data_end - data;
65 int retval = XDP_ABORTED; /* Fail */
70 /* Exceed MTU with 1 via delta adjust */
71 delta = GLOBAL_USER_MTU - (data_len - ETH_HLEN) + 1;
73 err = bpf_check_mtu(ctx, ifindex, &mtu_len, delta, 0);
75 retval = XDP_PASS; /* Success in exceeding MTU check */
76 if (err != BPF_MTU_CHK_RET_FRAG_NEEDED)
80 global_bpf_mtu_xdp = mtu_len;
85 int xdp_minus_delta(struct xdp_md *ctx)
87 int retval = XDP_PASS; /* Expected retval on successful test */
88 void *data_end = (void *)(long)ctx->data_end;
89 void *data = (void *)(long)ctx->data;
90 __u32 ifindex = GLOBAL_USER_IFINDEX;
91 __u32 data_len = data_end - data;
95 /* Borderline test case: Minus delta exceeding packet length allowed */
96 delta = -((data_len - ETH_HLEN) + 1);
98 /* Minus length (adjusted via delta) still pass MTU check, other helpers
99 * are responsible for catching this, when doing actual size adjust
101 if (bpf_check_mtu(ctx, ifindex, &mtu_len, delta, 0))
102 retval = XDP_ABORTED;
104 global_bpf_mtu_xdp = mtu_len;
109 int xdp_input_len(struct xdp_md *ctx)
111 int retval = XDP_PASS; /* Expected retval on successful test */
112 void *data_end = (void *)(long)ctx->data_end;
113 void *data = (void *)(long)ctx->data;
114 __u32 ifindex = GLOBAL_USER_IFINDEX;
115 __u32 data_len = data_end - data;
117 /* API allow user give length to check as input via mtu_len param,
118 * resulting MTU value is still output in mtu_len param after call.
120 * Input len is L3, like MTU and iph->tot_len.
121 * Remember XDP data_len is L2.
123 __u32 mtu_len = data_len - ETH_HLEN;
125 if (bpf_check_mtu(ctx, ifindex, &mtu_len, 0, 0))
126 retval = XDP_ABORTED;
128 global_bpf_mtu_xdp = mtu_len;
133 int xdp_input_len_exceed(struct xdp_md *ctx)
135 int retval = XDP_ABORTED; /* Fail */
136 __u32 ifindex = GLOBAL_USER_IFINDEX;
139 /* API allow user give length to check as input via mtu_len param,
140 * resulting MTU value is still output in mtu_len param after call.
142 * Input length value is L3 size like MTU.
144 __u32 mtu_len = GLOBAL_USER_MTU;
146 mtu_len += 1; /* Exceed with 1 */
148 err = bpf_check_mtu(ctx, ifindex, &mtu_len, 0, 0);
149 if (err == BPF_MTU_CHK_RET_FRAG_NEEDED)
150 retval = XDP_PASS ; /* Success in exceeding MTU check */
152 global_bpf_mtu_xdp = mtu_len;
157 int tc_use_helper(struct __sk_buff *ctx)
159 int retval = BPF_OK; /* Expected retval on successful test */
163 if (bpf_check_mtu(ctx, 0, &mtu_len, delta, 0)) {
168 if (mtu_len != GLOBAL_USER_MTU)
169 retval = BPF_REDIRECT;
171 global_bpf_mtu_tc = mtu_len;
176 int tc_exceed_mtu(struct __sk_buff *ctx)
178 __u32 ifindex = GLOBAL_USER_IFINDEX;
179 int retval = BPF_DROP; /* Fail */
180 __u32 skb_len = ctx->len;
185 /* Exceed MTU with 1 via delta adjust */
186 delta = GLOBAL_USER_MTU - (skb_len - ETH_HLEN) + 1;
188 err = bpf_check_mtu(ctx, ifindex, &mtu_len, delta, 0);
190 retval = BPF_OK; /* Success in exceeding MTU check */
191 if (err != BPF_MTU_CHK_RET_FRAG_NEEDED)
195 global_bpf_mtu_tc = mtu_len;
200 int tc_exceed_mtu_da(struct __sk_buff *ctx)
202 /* SKB Direct-Access variant */
203 void *data_end = (void *)(long)ctx->data_end;
204 void *data = (void *)(long)ctx->data;
205 __u32 ifindex = GLOBAL_USER_IFINDEX;
206 __u32 data_len = data_end - data;
207 int retval = BPF_DROP; /* Fail */
212 /* Exceed MTU with 1 via delta adjust */
213 delta = GLOBAL_USER_MTU - (data_len - ETH_HLEN) + 1;
215 err = bpf_check_mtu(ctx, ifindex, &mtu_len, delta, 0);
217 retval = BPF_OK; /* Success in exceeding MTU check */
218 if (err != BPF_MTU_CHK_RET_FRAG_NEEDED)
222 global_bpf_mtu_tc = mtu_len;
227 int tc_minus_delta(struct __sk_buff *ctx)
229 int retval = BPF_OK; /* Expected retval on successful test */
230 __u32 ifindex = GLOBAL_USER_IFINDEX;
231 __u32 skb_len = ctx->len;
235 /* Borderline test case: Minus delta exceeding packet length allowed */
236 delta = -((skb_len - ETH_HLEN) + 1);
238 /* Minus length (adjusted via delta) still pass MTU check, other helpers
239 * are responsible for catching this, when doing actual size adjust
241 if (bpf_check_mtu(ctx, ifindex, &mtu_len, delta, 0))
244 global_bpf_mtu_xdp = mtu_len;
249 int tc_input_len(struct __sk_buff *ctx)
251 int retval = BPF_OK; /* Expected retval on successful test */
252 __u32 ifindex = GLOBAL_USER_IFINDEX;
254 /* API allow user give length to check as input via mtu_len param,
255 * resulting MTU value is still output in mtu_len param after call.
257 * Input length value is L3 size.
259 __u32 mtu_len = GLOBAL_USER_MTU;
261 if (bpf_check_mtu(ctx, ifindex, &mtu_len, 0, 0))
264 global_bpf_mtu_xdp = mtu_len;
269 int tc_input_len_exceed(struct __sk_buff *ctx)
271 int retval = BPF_DROP; /* Fail */
272 __u32 ifindex = GLOBAL_USER_IFINDEX;
275 /* API allow user give length to check as input via mtu_len param,
276 * resulting MTU value is still output in mtu_len param after call.
278 * Input length value is L3 size like MTU.
280 __u32 mtu_len = GLOBAL_USER_MTU;
282 mtu_len += 1; /* Exceed with 1 */
284 err = bpf_check_mtu(ctx, ifindex, &mtu_len, 0, 0);
285 if (err == BPF_MTU_CHK_RET_FRAG_NEEDED)
286 retval = BPF_OK; /* Success in exceeding MTU check */
288 global_bpf_mtu_xdp = mtu_len;