-
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 a6e838d
Showing
7 changed files
with
155 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,70 @@ | ||
package link | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cilium/ebpf" | ||
"github.com/cilium/ebpf/internal/sys" | ||
) | ||
|
||
const NFIPDefrag NetfilterAttachFlags = 0 // Enable IP packet defragmentation | ||
|
||
type NetfilterAttachFlags uint32 | ||
|
||
type NetfilterOptions struct { | ||
// Program must be a netfilter BPF program. | ||
Program *ebpf.Program | ||
// The protocol family. | ||
ProtocolFamily uint32 | ||
// The number of the hook you are interested in. | ||
HookNumber uint32 | ||
// Priority within hook | ||
Priority int32 | ||
// Extra link flags | ||
Flags uint32 | ||
// Netfilter flags | ||
NetfilterFlags NetfilterAttachFlags | ||
} | ||
|
||
type netfilterLink 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: 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 &netfilterLink{RawLink{fd, ""}}, nil | ||
} | ||
|
||
func (*netfilterLink) Update(new *ebpf.Program) error { | ||
return fmt.Errorf("netfilter update: %w", ErrNotSupported) | ||
} | ||
|
||
var _ Link = (*netfilterLink)(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,27 @@ | ||
package link | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cilium/ebpf" | ||
"github.com/cilium/ebpf/internal/testutils" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
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: unix.NFPROTO_IPV4, | ||
HookNumber: unix.NF_INET_LOCAL_OUT, | ||
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