-
Notifications
You must be signed in to change notification settings - Fork 29
/
https.c
52 lines (38 loc) · 1.23 KB
/
https.c
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
/*
* SPDX-FileCopyrightText: (c) 2024 Ring Zero Desenvolvimento de Software LTDA
* SPDX-License-Identifier: MIT OR GPL-2.0-only
*/
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
extern int bpf_luaxdp_run(char *key, size_t key__sz, struct xdp_md *xdp_ctx, void *arg, size_t arg__sz) __ksym;
static char runtime[] = "examples/filter/sni";
struct bpf_luaxdp_arg {
__u16 offset;
} __attribute__((packed));
SEC("xdp")
int filter_https(struct xdp_md *ctx)
{
struct bpf_luaxdp_arg arg;
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct iphdr *ip = data + sizeof(struct ethhdr);
if (ip + 1 > (struct iphdr *)data_end)
goto pass;
if (ip->protocol != IPPROTO_TCP)
goto pass;
struct tcphdr *tcp = (void *)ip + (ip->ihl * 4);
if (tcp + 1 > (struct tcphdr *)data_end)
goto pass;
if (bpf_ntohs(tcp->dest) != 443 || !tcp->psh)
goto pass;
void *payload = (void *)tcp + (tcp->doff * 4);
if (payload > data_end)
goto pass;
arg.offset = bpf_htons((__u16)(payload - data));
int action = bpf_luaxdp_run(runtime, sizeof(runtime), ctx, &arg, sizeof(arg));
return action < 0 ? XDP_PASS : action;
pass:
return XDP_PASS;
}
char _license[] SEC("license") = "Dual MIT/GPL";