Skip to content

Commit ae094f6

Browse files
ipchamaipchama
and
ipchama
authored
Feature/dhcpv4 bpf filter (#19)
* ebpf filter property added to socketeer and filter added to init. * TODO note. Co-authored-by: ipchama <[email protected]>
1 parent c3587c6 commit ae094f6

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

cmd/dhcpv4.go

+33
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ipchama/dhammer/socketeer"
1111
"github.com/spf13/cobra"
1212
"github.com/vishvananda/netlink"
13+
"golang.org/x/sys/unix"
1314
"net"
1415
"sync"
1516
"time"
@@ -246,13 +247,45 @@ func init() {
246247
options.StatsRate = 5
247248
}
248249

250+
filter := [28]unix.SockFilter{{0x28, 0, 0, 0x0000000c}, // "arp or (port 67 or port 68)"
251+
{0x15, 24, 0, 0x00000806},
252+
{0x15, 0, 9, 0x000086dd},
253+
{0x30, 0, 0, 0x00000014},
254+
{0x15, 2, 0, 0x00000084},
255+
{0x15, 1, 0, 0x00000006},
256+
{0x15, 0, 20, 0x00000011},
257+
{0x28, 0, 0, 0x00000036},
258+
{0x15, 17, 0, 0x00000043},
259+
{0x15, 16, 0, 0x00000044},
260+
{0x28, 0, 0, 0x00000038},
261+
{0x15, 14, 13, 0x00000043},
262+
{0x15, 0, 14, 0x00000800},
263+
{0x30, 0, 0, 0x00000017},
264+
{0x15, 2, 0, 0x00000084},
265+
{0x15, 1, 0, 0x00000006},
266+
{0x15, 0, 10, 0x00000011},
267+
{0x28, 0, 0, 0x00000014},
268+
{0x45, 8, 0, 0x00001fff},
269+
{0xb1, 0, 0, 0x0000000e},
270+
{0x48, 0, 0, 0x0000000e},
271+
{0x15, 4, 0, 0x00000043},
272+
{0x15, 3, 0, 0x00000044},
273+
{0x48, 0, 0, 0x00000010},
274+
{0x15, 1, 0, 0x00000043},
275+
{0x15, 0, 1, 0x00000044},
276+
{0x6, 0, 0, 0x00040000},
277+
{0x6, 0, 0, 0x00000000}}
278+
279+
socketeerOptions.EbpfFilter = &unix.SockFprog{28, &filter[0]}
280+
249281
gHammer = hammer.New(socketeerOptions, options)
250282

251283
err = gHammer.Init(ApiAddress, ApiPort)
252284

253285
if err != nil {
254286
panic(err)
255287
}
288+
256289
err = gHammer.Run()
257290

258291
if err != nil {

config/socketeer.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package config
22

33
import (
4+
"golang.org/x/sys/unix"
45
"net"
56
)
67

78
type SocketeerOptions struct {
89
InterfaceName string
910
GatewayMAC net.HardwareAddr
1011
PromiscuousMode bool
12+
EbpfFilter *unix.SockFprog
1113
}

socketeer/socketeer.go

+11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import (
66
"github.com/google/gopacket/layers"
77
"github.com/ipchama/dhammer/config"
88
"github.com/ipchama/dhammer/message"
9+
"golang.org/x/sys/unix"
910
"net"
1011
"runtime"
1112
"syscall"
1213
)
1314

15+
// TODO: Move syscalls from syscall package to golang.org/x/sys/unix.
16+
// Maybe add custom port to ebpf rules.
17+
1418
type RawSocketeer struct {
1519
socketFd int
1620
IfInfo *net.Interface
@@ -57,6 +61,13 @@ func (s *RawSocketeer) Init() error {
5761
return err
5862
}
5963

64+
if s.options.EbpfFilter != nil {
65+
err = unix.SetsockoptSockFprog(s.socketFd, syscall.SOL_SOCKET, syscall.SO_ATTACH_FILTER, s.options.EbpfFilter)
66+
if err != nil {
67+
return err
68+
}
69+
}
70+
6071
s.IfInfo, err = net.InterfaceByName(s.options.InterfaceName)
6172

6273
if err != nil {

0 commit comments

Comments
 (0)