22 Commits

Author SHA1 Message Date
Vincent Li
58b4babde2 xdp-tools: add IFB setup in tc-basic-classifier
commit f0812939ca4f6332d34715673c4fe44fbb1a491b (HEAD -> main, origin/main, origin/HEAD)
    Author: Vincent Li <vincent.mc.li@gmail.com>
    Date:   Mon Nov 3 21:46:30 2025 -0800

        tc-basic-classifier: add IFB setup in user space

        Add the required kernel modules setup and green0 ingress mirror to ifb0
        egress setup in class_filter so it is easier for user. see [0] for
        required kernel modules and ingress shaping. also change the default
        path to /usr/lib/bpf/class_filter.bpf.o  also change the default bpf
        object path to /usr/lib/bpf/class_filter.bpf.o.

        [0]: https://std.rocks/gnulinux_network_traffic_control.html

        for example:

        Complete setup with one command (IFB mirroring + BPF attachment)

        ./class_filter -i green0 -3 10mbit -I -a -v

        Setting up IFB mirroring for ingress traffic shaping on green0
        Loading required kernel modules...
        Executing: modprobe ifb numifbs=1
        Loaded module: ifb numifbs=1
        Executing: modprobe sch_fq_codel
        Loaded module: sch_fq_codel
        Executing: modprobe act_mirred
        Loaded module: act_mirred
        Executing: modprobe act_connmark
        Loaded module: act_connmark
        Successfully loaded all required kernel modules
        Executing: ip link set dev ifb0 up
        Executing: tc qdisc del dev green0 ingress 2>/dev/null
        Executing: tc qdisc add dev green0 ingress handle ffff:
        Executing: tc filter add dev green0 parent ffff: protocol ip u32 match u32 0 0 action connmark action mirred egress redirect dev ifb0 flowid ffff:1
        Successfully setup IFB mirroring:
          - Ingress traffic from green0 mirrored to ifb0 egress

        Verification - IFB status:
        Executing: ip link show ifb0
        7: ifb0: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc htb state UNKNOWN mode DEFAULT group default qlen 32
            link/ether 8e:52:c9:93:13:34 brd ff:ff:ff:ff:ff:ff

        TC configuration on green0 ingress:
        Executing: tc qdisc show dev green0
        qdisc fq_codel 8001: root refcnt 9 limit 10240p flows 1024 quantum 1514 target 5ms interval 100ms memory_limit 32Mb ecn drop_batch 64
        qdisc ingress ffff: parent ffff:fff1 ----------------
        Executing: tc filter show dev green0 parent ffff:
        filter protocol ip pref 49152 u32 chain 0
        filter protocol ip pref 49152 u32 chain 0 fh 800: ht divisor 1
        filter protocol ip pref 49152 u32 chain 0 fh 800::800 order 2048 key ht 800 bkt 0 flowid ffff:1 not_in_hw
          match 00000000/00000000 at 0
                action order 1: connmark zone 0 pipe
                 index 1 ref 1 bind 1

                action order 2: mirred (Egress Redirect to device ifb0) stolen
                index 1 ref 1 bind 1

        Setting up TC qdisc and classes on ifb0 for egress shaping...
        Executing: tc qdisc del dev ifb0 root 2>/dev/null
        Executing: tc qdisc add dev ifb0 root handle 1:0 htb default 30
        Executing: tc class add dev ifb0 parent 1:0 classid 1:1 htb rate 100mbit
        Warning: sch_htb: quantum of class 10001 is big. Consider r2q change.
        Executing: tc class add dev ifb0 parent 1:1 classid 1:30 htb rate 5mbit ceil 10mbit
        Successfully setup TC qdisc and classes on ifb0

        TC configuration on ifb0:
        Executing: tc qdisc show dev ifb0
        qdisc htb 1: root refcnt 2 r2q 10 default 0x30 direct_packets_stat 1 direct_qlen 32
        Executing: tc class show dev ifb0
        class htb 1:1 root rate 100Mbit ceil 100Mbit burst 1600b cburst 1600b
        class htb 1:30 parent 1:1 prio 0 rate 5Mbit ceil 10Mbit burst 1600b cburst 1600b

        Automatically attaching BPF program to ifb0 for classification...
        Attaching BPF program to ifb0...
        Executing: tc filter del dev ifb0 protocol ip parent 1:0 2>/dev/null
        Command failed but ignoring (may be normal): tc filter del dev ifb0 protocol ip parent 1:0 2>/dev/null
        Executing: tc filter add dev ifb0 protocol ip parent 1:0 bpf obj ./class_filter.bpf.o classid 1: direct-action
        Successfully attached BPF program to ifb0 egress (parent 1:0)
        BPF maps automatically pinned to /sys/fs/bpf/tc/globals/
        Verification:
        Executing: tc filter show dev ifb0 parent 1:0
        filter protocol ip pref 49152 bpf chain 0
        filter protocol ip pref 49152 bpf chain 0 handle 0x1 flowid 1: class_filter.bpf.o:[classifier] direct-action not_in_hw id 100 name cls_filter tag 7163f8c072cf7015 jited
        Pinned maps:
        Executing: ls -la /sys/fs/bpf/tc/globals/ 2>/dev/null || echo 'No pinned maps found'
        total 0
        drwx------ 2 root root 0 Nov  3 11:43 .
        drwx------ 3 root root 0 Nov  3 11:24 ..
        -rw------- 1 root root 0 Nov  3 11:43 cls_filter_ip_trie_map
        -rw------- 1 root root 0 Nov  3 11:43 cls_filter_port_map

        Complete setup finished:
          - IFB mirroring configured on green0 -> ifb0
          - TC qdisc and classes setup on ifb0
          - BPF classifier attached to ifb0 egress
          - Ready for port/IP based traffic classification!

        Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2025-11-04 02:18:19 +00:00
Vincent Li
101831d1b7 xdp-tools: tc-basic-classifier ingress shaping
commit 9b82374a47906be38eb50f6ee5cc429fc4de4a4c (HEAD -> main, origin/main, origin/HEAD)
Author: Vincent Li <vincent.mc.li@gmail.com>
Date:   Sat Nov 1 09:03:11 2025 -0700

    tc-basic-classifier: add source ip address classification

    For ingress shaping based on local network source address, according to [0] [1], iptables firewallrule mark is used to mark the packet originated from the local network source ip, but with eBPF tc, we can do the eBPF TC classification on the ifb0 interface to eliminate use of iptables, this improves programability and flexibility. so change the tc bpf program to do both destination and source ip classification, the setup is like:

    modprobe ifb numifbs=1
    modprobe sch_fq_codel
    modprobe act_mirred
    modprobe act_connmark

    ip link set ifb0 up

    tc qdisc add dev green0 ingress handle ffff:

    tc filter add dev green0 parent ffff: protocol ip u32 match u32 0 0 action connmark action mirred egress redirect dev ifb0 flowid ffff:1

    ./class_filter -a -b ./class_filter.bpf.o -i ifb0 -v

    Setting up TC qdisc and classes...
    Setting up TC qdisc and classes on ifb0
    Executing: tc qdisc del dev ifb0 root 2>/dev/null
    Command failed but ignoring (may be normal): tc qdisc del dev ifb0 root 2>/dev/null
    Executing: tc qdisc add dev ifb0 root handle 1:0 htb default 30
    Executing: tc class add dev ifb0 parent 1:0 classid 1:1 htb rate 100mbit
    Warning: sch_htb: quantum of class 10001 is big. Consider r2q change.
    Executing: tc class add dev ifb0 parent 1:1 classid 1:30 htb rate 5mbit ceil 20mbit
    Successfully setup TC qdisc and classes on ifb0

    TC qdisc configuration:
    Executing: tc qdisc show dev ifb0
    qdisc htb 1: root refcnt 2 r2q 10 default 0x30 direct_packets_stat 2 direct_qlen 32

    TC classes configuration:
    Executing: tc class show dev ifb0
    class htb 1:1 root rate 100Mbit ceil 100Mbit burst 1600b cburst 1600b
    class htb 1:30 parent 1:1 prio 0 rate 5Mbit ceil 20Mbit burst 1600b cburst 1600b
    Executing: tc filter del dev ifb0 protocol ip parent 1:0 2>/dev/null
    Command failed but ignoring (may be normal): tc filter del dev ifb0 protocol ip parent 1:0 2>/dev/null
    Executing: tc filter add dev ifb0 protocol ip parent 1:0 bpf obj ./class_filter.bpf.o classid 1: direct-action
    Successfully attached BPF program to ifb0 egress (parent 1:0)
    BPF maps automatically pinned to /sys/fs/bpf/tc/globals/
    Verification:
    Executing: tc filter show dev ifb0 parent 1:0
    filter protocol ip pref 49152 bpf chain 0
    filter protocol ip pref 49152 bpf chain 0 handle 0x1 flowid 1: class_filter.bpf.o:[classifier] direct-action not_in_hw id 93 name cls_filter tag 7163f8c072cf7015 jited
    Pinned maps:
    Executing: ls -la /sys/fs/bpf/tc/globals/ 2>/dev/null || echo 'No pinned maps found'
    total 0
    drwx------ 2 root root 0 Nov  1 09:01 .
    drwx------ 3 root root 0 Nov  1 08:01 ..
    -rw------- 1 root root 0 Nov  1 09:01 cls_filter_ip_trie_map
    -rw------- 1 root root 0 Nov  1 09:01 cls_filter_port_map

    BPF program loaded and attached successfully to ifb0 egress
    Default class configured: 1:30 (rate: 5mbit, ceil: 20mbit)

    Use --add-port to create additional classes with rates (supports single ports and ranges).

    ./class_filter -i ifb0 -r 192.168.9.13/32:40:80mbit

    Added TC class: 1:40 (rate: 5mbit, ceil: 80mbit)
    Added IP mapping: 192.168.9.13/32 -> 1:40 (rate: 80mbit)

    ./class_filter -m

    IP Range Mappings:
    CIDR            -> TC_CLASS  (BPF_VALUE)
    ----------------------------------------
    192.168.9.13/32 -> 1:40     (0x40)
    Total: 1 IP range mappings

    tc -g class show dev ifb0

    +---(1:1) htb rate 100Mbit ceil 100Mbit burst 1600b cburst 1600b
         +---(1:40) htb prio 0 rate 5Mbit ceil 80Mbit burst 1600b cburst 1600b
         +---(1:30) htb prio 0 rate 5Mbit ceil 20Mbit burst 1600b cburst 1600b

    on 192.168.9.13:

    root@vincent-iMac:~# iperf3 -t 4 -c 10.0.0.206 -p 8080
    Connecting to host 10.0.0.206, port 8080
    [  5] local 192.168.9.13 port 51072 connected to 10.0.0.206 port 8080
    [ ID] Interval           Transfer     Bitrate         Retr  Cwnd
    [  5]   0.00-1.00   sec  9.63 MBytes  80.8 Mbits/sec    5    119 KBytes
    [  5]   1.00-2.00   sec  9.26 MBytes  77.7 Mbits/sec    7   86.3 KBytes
    [  5]   2.00-3.00   sec  9.20 MBytes  77.1 Mbits/sec    7    116 KBytes
    [  5]   3.00-4.00   sec  8.82 MBytes  74.0 Mbits/sec    2    102 KBytes
    - - - - - - - - - - - - - - - - - - - - - - - - -
    [ ID] Interval           Transfer     Bitrate         Retr
    [  5]   0.00-4.00   sec  36.9 MBytes  77.4 Mbits/sec   21             sender
    [  5]   0.00-4.05   sec  36.5 MBytes  75.6 Mbits/sec                  receiver

    [0]: https://std.rocks/gnulinux_network_traffic_control.html
    [1]: https://wiki.archlinux.org/title/Advanced_traffic_control#Shape_ingress_traffic_on_SNAT

    Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2025-11-03 02:56:04 +00:00
Vincent Li
0b4a8acd52 xdp-tools: a few improvement on class_filter
commit fa10708e3c87519f8e9c85cdff7a2bdd80dbd699 (HEAD -> main)
    Author: Vincent Li <vincent.mc.li@gmail.com>
    Date:   Fri Oct 24 20:58:18 2025 -0700

        tc-basic-classifier: allow port range for classification

        Improve user space class_filter to add port range like:

        ./class_filter -i red0 --add-port "8080-8082:10:40mbit"
        or
        ./class_filter -i red0 --add-port "8083:20:60mbit"

        each port will still be added as individual entry in port map, so no
        code change required in bpf side.

        Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>

    commit 5852f6184e3d129667df01011c4ecdf6df994266
    Author: Vincent Li <vincent.mc.li@gmail.com>
    Date:   Wed Oct 22 19:41:05 2025 -0700

        tc-basic-classifier: make tcp and udp port classification

        make port classification for both UDP and TCP.

        Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>

    commit 0bda902c82c755bda1d5ba40d1404051de4e9102
    Author: Vincent Li <vincent.mc.li@gmail.com>
    Date:   Tue Oct 21 19:28:15 2025 -0700

        tc-basic-classifier: fix IP endiness and classification on red0

        the IP in user space is stored in host order but bpf program IP lookup
        by network order. also tested IP classification green0 not working. so
        should do destination IP classification on red0 interface, and the
        destination IP is going to be Internet destination IP addresses. need to
        figure out how to do IP based classification on LAN/green0 network IPs
        if required.

        Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2025-10-25 02:26:32 +00:00
Vincent Li
04033ad1a5 xdp-tools: rebase on upstream 1.5.7 and add tc classifier
commit ced17feca631f6963a2439f41ef09a7db048f316 (HEAD -> main)
    Author: Vincent Li <vincent.mc.li@gmail.com>
    Date:   Mon Oct 20 21:28:17 2025 -0700

        tc-basic-classifier: fix tcp port Endianess bug by AI

        AI generated class_filter program stored the tcp port in network order,
        but in bpf program the tcp port is converted to host order, and result in
        tcp port lookup failure, unable to get the correct classid, fail to do rate
        classification.

        Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>

    commit a18fe4be0374ab1efb21c1228a5c5790ded7636e
    Author: Vincent Li <vincent.mc.li@gmail.com>
    Date:   Mon Oct 20 19:15:34 2025 -0700

        tc-basic-classifier: classify port and IP from bpf map

        add port and IP rate classification based on bpf map so user could
        add or remove port/IP rate classification dynamically at run time

        since TC class works on egress direction, so port rate classification
        works on red0 egress, and IP rate classification works on green0 egress

        port rate classification:

        class_filter -a -b ./class_filter.bpf.o -i red0 -v

        class_filter -i red0 --add-port 8080:10:80mbit
        class_filter -i red0 --add-port 8081:20:40mbit

        class_filter -i red0 --delete-port 8080

        class_filter -i red0 --list-ports

        IP rate classification:

        class_filter -a -b ./class_filter.bpf.o -i green0 -v

        class_filter -i red0 --add-ip 192.168.1.0/24:40:30mbit
        class_filter -i red0 --delete-ip 192.168.1.0/24

        class_filter -i red0 --list-ips

        Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>

    commit 12280ef22ae49f75eda047144ed3e9dc0f73e04a
    Author: Vincent Li <vincent.mc.li@gmail.com>
    Date:   Fri Oct 17 19:45:07 2025 -0700

        tc-basic-classifier: add user space program

        the bpf skel header is generated only if USER_TARGETS
        is added in Makefile, so add a dummy user space program.

        Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>

    commit fe5cc1814af4c995f61ec08708110deef7a65c45
    Author: Vincent Li <vincent.mc.li@gmail.com>
    Date:   Fri Oct 17 19:28:45 2025 -0700

        xdp-tools: rebase on upstream xdp-tools main branch

        Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>

    commit 7dee7fd954c06a3c58bedbb5561b9ee65c3f749f
    Author: Vincent Li <vincent.mc.li@gmail.com>
    Date:   Fri Oct 17 18:39:59 2025 -0700

        tc-basic-classifier: rename the class filter

        Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2025-10-21 01:47:29 +00:00
Vincent Li
b78ee945cd xdp-tools: add dnsdist XDP program
upgrade xdp-tools to 1.5.5 and add dnsdist_xdp.bpf.o
for dnsdist xsk AF_XDP

xdp-loader load green0 -P 90 -p /sys/fs/bpf/dnsdist \
    -n xdp_dns_filter /usr/lib/bpf/dnsdist_xdp.bpf.o

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2025-06-21 17:40:40 +00:00
Vincent Li
93a5a7af7b xdp-tools: rebased on upstream 1.5.4
included recent changes:

1 fix for xdp-dns for [0]
2 tc-loader to load tc ebpf program

[0]: https://github.com/vincentmli/BPFire/issues/87

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2025-05-14 20:35:57 +00:00
Vincent Li
09c182c75a xdp-tools: XDP UDP DDoS for online game protection
UDP DDoS has pattern of flooding game server with
random source IP and UDP with random payload. game
server UDP traffic requires certain payload
pattern, so this XDP program can serve as example
to stop UDP DDoS attack with UDP payload that does not
match game UDP traffic payload pattern.

without UDP DDoS protection, under DDoS attack:

BPFire UI RED Traffic: in 9xx Mbit/s.

with UDP DDoS protection, under DDoS attack:

BPFire UI RED Traffic: in 1xx Mbit/s.

Tested-by: Muhammad Haikal <eykalpirates@gmail.com>
Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-11-27 18:32:10 +00:00
Vincent Li
5de3f44cc7 xdp-synproxy: enable or disable window scaling
XDP generated SYNACK tcp options with window
scaling and timestamp could intermittently cause
small packet transmission on DDoS protected server.
allow user to disable window scaling when such
problem occurs. see [0]

[0]: https://github.com/vincentmli/xdp-tools/issues/7

Reported-by: DNSPROXY.ORG LLC <dnsproxyorg@gmail.com>
Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-11-12 01:22:27 +00:00
Vincent Li
a600787c67 xdp-synproxy: drop IP don't fragment check
When XDP DDoS syncookie program is attached
to red0 interface, green network client internet
connection to website like gmail/youtube... failed.
it is because these sites does not have IP DF flag
set for each tcp packet, and syncookie_xdp program
would drop these packets when they arrived at red0
interface.

see https://github.com/vincentmli/BPFire/issues/59

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-10-25 20:35:33 +00:00
Vincent Li
3e17c7b30b xdp-tools: build xdp-tools with libbpf 1.4.6
Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-10-18 17:16:17 +00:00
Vincent Li
86a9264a25 xdp-geoip: add XDP GeoIP program
Add XDP GeoIP program to do location
IP block in XDP.

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-10-12 20:33:12 +00:00
Vincent Li
a118df6060 xdp-sni: switch LPM trie map to hash map
switch xdp_sni.bpf.o LPM trie map to hash map
to reduce code complexity and avoid verifier error

now need to add domain and its sub domain to hash
map to block each domain and its sub domain site.

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-10-09 02:48:38 +00:00
Vincent Li
d334d39e3f xdp-sni: add XDP TLS SNI logging
add XDP TLS SNI logging with bpf ringbuf
drop xdp_sni.bpf.o reverse_string due to
bpf verifier complaining program is too large.

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-10-08 01:05:01 +00:00
Vincent Li
8b3cdb2ebe xdp-tools: fix xdp-dns XDP program byte reverse
domain name in xdp_dns.bpf.o not reversed properly
result in domain name mismatch with domain inserted
from user space xdp_dns

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-10-04 21:36:09 +00:00
Vincent Li
ccf49b1105 xdp-dns: update xdp_dns to correct map
change xdp_dns to use
/sys/fs/bpf/xdp-dns-denylist/domain_denylist

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-10-04 04:06:00 +00:00
Vincent Li
13530fa1ef xdp-tools: remove dns query from xdp-dnsrrl
also change user space xdp_dns_log program to
use map /sys/fs/bpf/xdp-dns-denylist/dns_ringbuf

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-10-02 20:20:48 +00:00
Vincent Li
652ab98e1a xdp-tools: add xdp-dns system logging
add bpf ringbuf to xdp-dns program and
user space program to log DNS query to
system log.

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-10-01 23:45:03 +00:00
Vincent Li
32c15c3fe3 xdp-tools: add xdp-sni
add XDP TLS/SSL SNI parsing

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-09-30 03:24:30 +00:00
Vincent Li
74cf8a3943 xdp-tools: add XDP DNS domain denylist
upgrade xdp-tools and add XDP DNS domain
denylist bpf and user space program.

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-09-12 17:12:16 +00:00
Vincent Li
be1fc5ce77 xdp-tools: add xdp-udp
Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-04-24 00:48:04 +00:00
Vincent Li
9f86b661cb Add xdp dns rate limit program with bpf_printk deleted
XDP dns rate limit program has static tail call
which requires revert xdp-tool commit:
(039bdea "xdp-loader: Only load the BPF program we need from object files")

XDP dns rate limit program also uses bpf_printk helper which is not
supported on FireBeeOS since kernel CONFIG_BPF_EVENTS which allows user
to do kprobe, uprobe, tracepoint is not enabled, so bpf_printk helper is
not available, so removed bpf_printk

see discussion in [0] xdp-loader load xdp program with bpf tail call result in Bad file descriptor(-9)

[0] https://github.com/xdp-project/xdp-tools/issues/377

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-04-08 19:15:32 +00:00
Vincent Li
f8ca312cfa Add xdp-tools add-on with XDP Synproxy
add xdp-tools utilities with addition of
SYN flooding DDoS attack protection in XDP

Signed-off-by: Vincent Li <vincent.mc.li@gmail.com>
2024-03-01 04:08:02 +00:00