Skip to content

feat(bpf): implement allow_port program#42

Open
leodido wants to merge 4 commits intofeat/allow-ipfrom
feat/allow-port
Open

feat(bpf): implement allow_port program#42
leodido wants to merge 4 commits intofeat/allow-ipfrom
feat/allow-port

Conversation

@leodido
Copy link
Copy Markdown
Owner

@leodido leodido commented Apr 26, 2026

Description

Allowlist-mode BPF program that drops all TCP/UDP packets except those with destination port equal to the input port number. Non-TCP/UDP protocols (ICMP, etc.) pass through unaffected.

This is the inverse of block_port: instead of blocking one port, it allows only one port and drops all other TCP/UDP traffic. Intended for agent quarantine scenarios where an agent should only communicate on a specific proxy port.

Depends on #41 (allow_ip).

Commits

  1. feat(bpf): implement allow_port program — BPF program, api.lua registration, input_parse.h wiring
  2. test(bpf): add allow_port tests — 7 new tests (43 total)
  3. docs: add allow_port to built-in programs — README.txt, docs/README.md

How to test

xmake clean -a
xmake f --generate-vmlinux=y
xmake
sudo XMAKE_ROOT=y xmake run test

All 43 tests pass (36 from PR #41 + 7 new).

@leodido leodido changed the base branch from main to feat/allow-ip April 26, 2026 15:31
@leodido leodido self-assigned this Apr 26, 2026
@leodido leodido changed the title feat(bpf): implement allow_port program feat(bpf): implement allow_port program Apr 26, 2026
@leodido leodido marked this pull request as ready for review April 27, 2026 11:30
@leodido leodido requested a review from fntlnz April 28, 2026 09:32
Comment thread bpf/allow_port.bpf.c Outdated
return TC_ACT_OK;
}

if (ip_is_fragment(skb, l3_offset))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bypass

Comment thread bpf/allow_port.bpf.c Outdated
}

struct iphdr *ip_header = data + l3_offset;
const int l4_offset = l3_offset + sizeof(*ip_header);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should use the ihl field of the packet here instead of the fixed struct size

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup see this from past week or so: #35

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol didn't see it !!

Comment thread bpf/allow_port.bpf.c Outdated
if (data + l4_offset + 4 > data_end)
{
bpf_printk("allow_port: [l4] size length check hit: continue");
return TC_ACT_OK;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why OK here

@leodido leodido force-pushed the feat/allow-port branch from cf84def to 4bd31ad Compare May 3, 2026 20:44
@leodido leodido force-pushed the feat/allow-port branch from 75076e2 to fd418f7 Compare May 3, 2026 22:37
@leodido leodido changed the base branch from feat/allow-ip to main May 3, 2026 22:47
leodido and others added 4 commits May 4, 2026 00:36
Allowlist-mode BPF program that drops all TCP/UDP packets except those
with destination port equal to the input port number. Non-TCP/UDP
protocols (ICMP, etc.) pass through unaffected.

Co-authored-by: Ona <no-reply@ona.com>
CLI functional: allows specific port, blocks other ports, does not
block ICMP. CLI flags: missing input, invalid port. CNI functional:
allow_port via JSON config. CNI flags: missing input rejected.

7 new tests (43 total).

Co-authored-by: Ona <no-reply@ona.com>
Co-authored-by: Ona <no-reply@ona.com>
…ary checks

- Use IHL field for dynamic IP header length instead of fixed sizeof(iphdr)
- Validate IHL >= 5 and bounds-check the full IP header
- Drop subsequent fragments (no L4 headers to verify)
- Fail-closed (TC_ACT_SHOT) on all boundary check failures
- Document passthroughs: non-IPv4 and non-TCP/UDP traffic passes through

Co-authored-by: Ona <no-reply@ona.com>
@leodido leodido force-pushed the feat/allow-port branch from fd418f7 to eb08508 Compare May 4, 2026 00:42
@leodido leodido changed the base branch from main to feat/allow-ip May 4, 2026 00:42
@leodido leodido requested a review from fntlnz May 4, 2026 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants