-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mehrdad Arshad Rad <arshad.rad@gmail.com>
- Loading branch information
1 parent
bfbacc1
commit 68e0e76
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package link | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cilium/ebpf" | ||
"github.com/cilium/ebpf/internal/sys" | ||
) | ||
|
||
const NFIPDefrag NFAttachFlags = 0 // Enable IP packet defragmentation | ||
|
||
const ( | ||
NFINETPrerouting NFHook = iota // NF_INET_PRE_ROUTING | ||
NFINETLocalIn // NF_INET_LOCAL_IN | ||
NFINETForward // NF_INET_FORWARD | ||
NFINETLocalOut // NF_INET_LOCAL_OUT | ||
NFINETPostrouting // NF_INET_POST_ROUTING | ||
NFINETIngress // NF_INET_INGRESS | ||
) | ||
|
||
const ( | ||
ProtoUnspec NFProtoFamily = 0 // NFPROTO_UNSPEC | ||
ProtoInet NFProtoFamily = 1 // NFPROTO_INET | ||
ProtoIPv4 NFProtoFamily = 2 // NFPROTO_IPV4 | ||
ProtoARP NFProtoFamily = 3 // NFPROTO_ARP | ||
ProtoNetDev NFProtoFamily = 5 // NFPROTO_NETDEV | ||
ProtoBridge NFProtoFamily = 7 // NFPROTO_BRIDGE | ||
ProtoIPv6 NFProtoFamily = 10 // NFPROTO_IPV6 | ||
ProtoDECNet NFProtoFamily = 12 // NFPROTO_DECNET | ||
) | ||
|
||
type NFProtoFamily uint8 | ||
type NFHook uint8 | ||
type NFAttachFlags uint32 | ||
|
||
type NetfilterOptions struct { | ||
// Program must be a netfilter BPF program. | ||
Program *ebpf.Program | ||
// The protocol family. | ||
ProtocolFamily NFProtoFamily | ||
// The number of the hook you are interested in. | ||
HookNumber NFHook | ||
// Priority within hook | ||
Priority int32 | ||
// Extra link flags | ||
Flags uint32 | ||
// Netfilter flags | ||
NetfilterFlags NFAttachFlags | ||
} | ||
|
||
type nfLink struct { | ||
RawLink | ||
} | ||
|
||
// AttachNetfilter links a netfilter BPF program to a netfilter hook. | ||
func AttachNetfilter(opts NetfilterOptions) (Link, error) { | ||
if opts.Program == nil { | ||
return nil, fmt.Errorf("netfilter program is nil") | ||
} | ||
|
||
if t := opts.Program.Type(); t != ebpf.Netfilter { | ||
return nil, fmt.Errorf("invalid program type %s, expected netfilter", t) | ||
} | ||
|
||
progFd := opts.Program.FD() | ||
if progFd < 0 { | ||
return nil, fmt.Errorf("invalid program: %s", sys.ErrClosedFd) | ||
} | ||
|
||
attr := sys.LinkCreateNetfilterAttr{ | ||
ProgFd: uint32(opts.Program.FD()), | ||
AttachType: uint32(sys.BPF_NETFILTER), | ||
Flags: opts.Flags, | ||
Pf: uint32(opts.ProtocolFamily), | ||
Hooknum: uint32(opts.HookNumber), | ||
Priority: opts.Priority, | ||
NetfilterFlags: uint32(opts.NetfilterFlags), | ||
} | ||
|
||
fd, err := sys.LinkCreateNetfilter(&attr) | ||
if err != nil { | ||
return nil, fmt.Errorf("attach netfilter link: %w", err) | ||
} | ||
|
||
return &nfLink{RawLink{fd, ""}}, nil | ||
} | ||
|
||
func (*nfLink) Update(new *ebpf.Program) error { | ||
return fmt.Errorf("netfilter update: %w", ErrNotSupported) | ||
} | ||
|
||
var _ Link = (*nfLink)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package link | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cilium/ebpf" | ||
"github.com/cilium/ebpf/internal/testutils" | ||
) | ||
|
||
func TestAttachNetfilter(t *testing.T) { | ||
testutils.SkipOnOldKernel(t, "6.4", "BPF_LINK_TYPE_NETFILTER") | ||
|
||
prog := mustLoadProgram(t, ebpf.Netfilter, ebpf.AttachNetfilter, "") | ||
|
||
l, err := AttachNetfilter(NetfilterOptions{ | ||
Program: prog, | ||
ProtocolFamily: ProtoIPv4, | ||
HookNumber: NFINETLocalOut, | ||
Priority: -128, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
testLink(t, l, prog) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters