-
Notifications
You must be signed in to change notification settings - Fork 0
/
drop.go
executable file
·121 lines (104 loc) · 2.5 KB
/
drop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"C"
"fmt"
"strconv"
"syscall"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
log "github.com/sirupsen/logrus"
)
import (
"net"
"os"
"os/signal"
"time"
"github.com/cilium/ebpf/rlimit"
)
const (
TYPE_ENTER = 1
TYPE_DROP = 2
TYPE_PASS = 3
)
func main() {
// sig := make(chan os.Signal, 1)
// signal.Notify(sig, os.Interrupt)
// Remove MEMLOCK resource limit
if err := rlimit.RemoveMemlock(); err != nil {
// Handle error
fmt.Printf("Failed to remove MEMLOCK limit: %v\n", err)
}
if len(os.Args) < 2 {
fmt.Println("please provide the network interface")
return
}
iface := os.Args[1]
allowedPort := os.Args[2]
// Parse the allowed port argument
allowedPortInt, err := strconv.Atoi(allowedPort)
if err != nil {
fmt.Printf("Invalid allowed port: %v\n", err)
return
}
spec, err := ebpf.LoadCollectionSpec("dropipv4.o")
if err != nil {
panic(err)
}
coll, _ := ebpf.NewCollection(spec)
if err != nil {
panic(fmt.Sprintf("Failed to create new collection: %v\n", err))
}
if err != nil {
panic(fmt.Sprintf("Failed to update map: %v\n", err))
}
defer coll.Close()
prog := coll.Programs["drop_packets"]
if prog == nil {
panic("No program named 'capture_packets' found in collection")
}
iface_idx, err := net.InterfaceByName(iface)
if err != nil {
panic(fmt.Sprintf("Failed to get interface %s: %v\n", iface, err))
}
opts := link.XDPOptions{
Program: prog,
Interface: iface_idx.Index,
// Flags is one of XDPAttachFlags (optional).
}
lnk, err := link.AttachXDP(opts)
if err != nil {
panic(err)
}
var lookupKey uint32 = 0
var portNum uint16 = uint16(allowedPortInt)
portMap, ok := coll.Maps["port"]
if !ok {
panic("No map named 'port'")
}
err = portMap.Update(lookupKey, portNum, ebpf.UpdateAny)
if err != nil {
panic(fmt.Sprintf("Could not update port map %v", err))
}
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
var packets_dropped []uint16
fmt.Println("Successfully loaded and attached BPF program.")
fmt.Println("Press Ctrl+c to end detach the program")
go func() {
for range ticker.C {
err := coll.Maps["rxcnt"].Lookup(lookupKey, &packets_dropped)
if err != nil {
panic(fmt.Sprintf("Could not lookup rxcnt %v", err))
}
var total_dropped uint16 = 0
for _, cpuvalue := range packets_dropped {
total_dropped += cpuvalue
}
log.Info("Dropped packets:", total_dropped)
}
}()
defer lnk.Close()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
}