2022-11-18 20:38:52 +01:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/evilsocket/opensnitch/daemon/log"
|
|
|
|
"github.com/iovisor/gobpf/elf"
|
|
|
|
)
|
|
|
|
|
2023-12-22 23:27:18 +01:00
|
|
|
// LoadEbpfModule loads the given eBPF module, from the given path if specified.
|
|
|
|
// Otherwise t'll try to load the module from several default paths.
|
|
|
|
func LoadEbpfModule(module, path string) (m *elf.Module, err error) {
|
2022-11-18 20:38:52 +01:00
|
|
|
var (
|
|
|
|
modulesDir = "/opensnitchd/ebpf"
|
|
|
|
paths = []string{
|
|
|
|
fmt.Sprint("/usr/local/lib", modulesDir),
|
|
|
|
fmt.Sprint("/usr/lib", modulesDir),
|
2023-07-23 23:35:19 +02:00
|
|
|
fmt.Sprint("/etc/opensnitchd"), // Deprecated: will be removed in future versions.
|
2022-11-18 20:38:52 +01:00
|
|
|
}
|
|
|
|
)
|
2023-12-22 23:27:18 +01:00
|
|
|
|
|
|
|
// if path has been specified, try to load the module from there.
|
|
|
|
if path != "" {
|
|
|
|
paths = []string{path}
|
|
|
|
}
|
|
|
|
|
2023-07-23 23:35:19 +02:00
|
|
|
modulePath := ""
|
|
|
|
moduleError := fmt.Errorf(`Module not found (%s) in any of the paths.
|
|
|
|
You may need to install the corresponding package`, module)
|
|
|
|
|
2022-11-18 20:38:52 +01:00
|
|
|
for _, p := range paths {
|
2023-07-23 23:35:19 +02:00
|
|
|
modulePath = fmt.Sprint(p, "/", module)
|
|
|
|
log.Debug("[eBPF] trying to load %s", modulePath)
|
|
|
|
if !Exists(modulePath) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m = elf.NewModule(modulePath)
|
2022-11-18 20:38:52 +01:00
|
|
|
|
2023-07-23 23:35:19 +02:00
|
|
|
if m.Load(nil) == nil {
|
|
|
|
log.Info("[eBPF] module loaded: %s", modulePath)
|
2022-11-18 20:38:52 +01:00
|
|
|
return m, nil
|
|
|
|
}
|
2023-07-23 23:35:19 +02:00
|
|
|
moduleError = fmt.Errorf(`
|
2022-12-16 17:09:37 +01:00
|
|
|
unable to load eBPF module (%s). Your kernel version (%s) might not be compatible.
|
|
|
|
If this error persists, change process monitor method to 'proc'`, module, GetKernelVersion())
|
2023-07-23 23:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, moduleError
|
2022-11-18 20:38:52 +01:00
|
|
|
}
|