Skip to content

Commit

Permalink
Feature/dhcpv4 bpf filter (#19)
Browse files Browse the repository at this point in the history
* ebpf filter property added to socketeer and filter added to init.

* TODO note.

Co-authored-by: ipchama <no-reply@example.com>
  • Loading branch information
ipchama and ipchama authored Mar 7, 2020
1 parent c3587c6 commit ae094f6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/dhcpv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ipchama/dhammer/socketeer"
"github.com/spf13/cobra"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
"net"
"sync"
"time"
Expand Down Expand Up @@ -246,13 +247,45 @@ func init() {
options.StatsRate = 5
}

filter := [28]unix.SockFilter{{0x28, 0, 0, 0x0000000c}, // "arp or (port 67 or port 68)"
{0x15, 24, 0, 0x00000806},
{0x15, 0, 9, 0x000086dd},
{0x30, 0, 0, 0x00000014},
{0x15, 2, 0, 0x00000084},
{0x15, 1, 0, 0x00000006},
{0x15, 0, 20, 0x00000011},
{0x28, 0, 0, 0x00000036},
{0x15, 17, 0, 0x00000043},
{0x15, 16, 0, 0x00000044},
{0x28, 0, 0, 0x00000038},
{0x15, 14, 13, 0x00000043},
{0x15, 0, 14, 0x00000800},
{0x30, 0, 0, 0x00000017},
{0x15, 2, 0, 0x00000084},
{0x15, 1, 0, 0x00000006},
{0x15, 0, 10, 0x00000011},
{0x28, 0, 0, 0x00000014},
{0x45, 8, 0, 0x00001fff},
{0xb1, 0, 0, 0x0000000e},
{0x48, 0, 0, 0x0000000e},
{0x15, 4, 0, 0x00000043},
{0x15, 3, 0, 0x00000044},
{0x48, 0, 0, 0x00000010},
{0x15, 1, 0, 0x00000043},
{0x15, 0, 1, 0x00000044},
{0x6, 0, 0, 0x00040000},
{0x6, 0, 0, 0x00000000}}

socketeerOptions.EbpfFilter = &unix.SockFprog{28, &filter[0]}

gHammer = hammer.New(socketeerOptions, options)

err = gHammer.Init(ApiAddress, ApiPort)

if err != nil {
panic(err)
}

err = gHammer.Run()

if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions config/socketeer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package config

import (
"golang.org/x/sys/unix"
"net"
)

type SocketeerOptions struct {
InterfaceName string
GatewayMAC net.HardwareAddr
PromiscuousMode bool
EbpfFilter *unix.SockFprog
}
11 changes: 11 additions & 0 deletions socketeer/socketeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"github.com/google/gopacket/layers"
"github.com/ipchama/dhammer/config"
"github.com/ipchama/dhammer/message"
"golang.org/x/sys/unix"
"net"
"runtime"
"syscall"
)

// TODO: Move syscalls from syscall package to golang.org/x/sys/unix.
// Maybe add custom port to ebpf rules.

type RawSocketeer struct {
socketFd int
IfInfo *net.Interface
Expand Down Expand Up @@ -57,6 +61,13 @@ func (s *RawSocketeer) Init() error {
return err
}

if s.options.EbpfFilter != nil {
err = unix.SetsockoptSockFprog(s.socketFd, syscall.SOL_SOCKET, syscall.SO_ATTACH_FILTER, s.options.EbpfFilter)
if err != nil {
return err
}
}

s.IfInfo, err = net.InterfaceByName(s.options.InterfaceName)

if err != nil {
Expand Down

0 comments on commit ae094f6

Please sign in to comment.